Class: Backendless

Inherits:
Object
  • Object
show all
Defined in:
lib/backendless.rb

Overview

Provides communication with Backendless API that is used to store data about the users

Class Method Summary collapse

Class Method Details

.build_login_body(login, password) ⇒ Hash

Builds the body of login request with email and password

Parameters:

  • login (String)

    email of the user

  • password (String)

    password of the user

Returns:

  • (Hash)

    the body for login



85
86
87
88
89
90
91
# File 'lib/backendless.rb', line 85

def self.(, password)
  body = {
    login: ,
    password: password
  }
  body
end

.build_registration_body(email, password) ⇒ Hash

Builds the body of registration request with email and password

Parameters:

  • email (String)

    email of the user

  • password (String)

    password of the user

Returns:

  • (Hash)

    the body for registration



71
72
73
74
75
76
77
# File 'lib/backendless.rb', line 71

def self.build_registration_body(email, password)
  body = {
    email: email,
    password: password
  }
  body
end

.generic_connectionFaraday connection

Creates the generic connection to Backendless and add mandatory headers

Returns:

  • (Faraday connection)

    generic connection



18
19
20
21
22
23
# File 'lib/backendless.rb', line 18

def self.generic_connection
  conn = Faraday.new(url: @uri)
  conn.headers['Content-Type'] = 'application/json'
  conn.headers['application-type'] = 'REST'
  conn
end

.last(user_object_id) ⇒ String

Provides the last food of user

Parameters:

  • user_object_id (String)

    ObjectID of the user data

Returns:

  • (String)

    The food id of last recipe



97
98
99
100
101
# File 'lib/backendless.rb', line 97

def self.last(user_object_id)
  conn = generic_connection
  response = conn.get "users/#{user_object_id}"
  Parser.extract_food_id response.body
end

.login(email, password) ⇒ String?

Login the user in the Backendless database and provides the response

Parameters:

  • email (String)

    email of the user

  • password (String)

    password of the user

Returns:

  • (String, nil)

    The response of login



44
45
46
47
48
49
# File 'lib/backendless.rb', line 44

def self.(email, password)
  conn = generic_connection
  body =  email, password
  response = conn.post 'users/login', body.to_json
  process_response response
end

.process_response(response) ⇒ String?

Process the response of request according to status

Parameters:

  • response (Faraday response)

    the response of request

Returns:

  • (String, nil)

    The response body



55
56
57
58
59
60
61
62
63
# File 'lib/backendless.rb', line 55

def self.process_response(response)
  if response.status.equal? 200
    response.body
  elsif [409, 400, 401].include? response.status
    nil
  else
    abort('I can not communicate with the user data. Set the BACKENDLESS_KEY')
  end
end

.register(email, password) ⇒ String?

Register the user in the Backendless database and provides response

Parameters:

  • email (String)

    email of the user

  • password (String)

    password of the user

Returns:

  • (String, nil)

    The response of registration



31
32
33
34
35
36
# File 'lib/backendless.rb', line 31

def self.register(email, password)
  conn = generic_connection
  body = build_registration_body email, password
  response = conn.post 'users/register', body.to_json
  process_response response
end

.update(object_id, user_token, key, value) ⇒ Faraday response?

Update the property in userdata

Parameters:

  • object_id (String)

    the object ID of user data

  • user_token (String)

    token receved from login operation

Returns:

  • (Faraday response, nil)

    The body of the response



108
109
110
111
112
113
114
115
# File 'lib/backendless.rb', line 108

def self.update(object_id, user_token, key, value)
  conn = generic_connection
  conn.headers['user-token'] = user_token
  body = {}
  body[key] = value
  response = conn.put "data/users/#{object_id}", body.to_json
  process_response response
end