Class: Backendless
- Inherits:
-
Object
- Object
- Backendless
- Defined in:
- lib/backendless.rb
Overview
Provides communication with Backendless API that is used to store data about the users
Class Method Summary collapse
-
.build_login_body(login, password) ⇒ Hash
Builds the body of login request with email and password.
-
.build_registration_body(email, password) ⇒ Hash
Builds the body of registration request with email and password.
-
.generic_connection ⇒ Faraday connection
Creates the generic connection to Backendless and add mandatory headers.
-
.last(user_object_id) ⇒ String
Provides the last food of user.
-
.login(email, password) ⇒ String?
Login the user in the Backendless database and provides the response.
-
.process_response(response) ⇒ String?
Process the response of request according to status.
-
.register(email, password) ⇒ String?
Register the user in the Backendless database and provides response.
-
.update(object_id, user_token, key, value) ⇒ Faraday response?
Update the property in userdata.
Class Method Details
.build_login_body(login, password) ⇒ Hash
Builds the body of login request with email and password
85 86 87 88 89 90 91 |
# File 'lib/backendless.rb', line 85 def self.build_login_body(login, password) body = { login: login, password: password } body end |
.build_registration_body(email, password) ⇒ Hash
Builds the body of registration request with email and password
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_connection ⇒ Faraday connection
Creates the generic connection to Backendless and add mandatory headers
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
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
44 45 46 47 48 49 |
# File 'lib/backendless.rb', line 44 def self.login(email, password) conn = generic_connection body = build_login_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
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
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
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 |