Class: Almapi::Api
- Inherits:
-
Object
- Object
- Almapi::Api
- Defined in:
- lib/almapi/almapi.rb
Instance Attribute Summary collapse
-
#apikey ⇒ String
readonly
Reads the key for API calls.
-
#conn ⇒ Object
readonly
Faraday object to handle API calls.
-
#uri_base ⇒ String
readonly
Reads the URI base for API calls.
Instance Method Summary collapse
-
#delete(resource) ⇒ Response
Handles a DELETE request creating the complete URI.
-
#get(resource, resumption_token = "") ⇒ Response
Handles a GET request creating the complete URI.
-
#initialize(apikey, uri_base) ⇒ Api
constructor
Initializes a new Almapi object and instance variable @apikey, @uri_base and @conn that is a Faraday connexion.
-
#post(resource, data) ⇒ Response
Handles a POST request creating the complete URI.
-
#put(resource, data) ⇒ Response
Handles a PUT request creating the complete URI.
Constructor Details
#initialize(apikey, uri_base) ⇒ Api
Initializes a new Almapi object and instance variable @apikey, @uri_base and @conn that is a Faraday connexion.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/almapi/almapi.rb', line 29 def initialize(apikey, uri_base) @apikey = apikey @uri_base = uri_base headers = { "Authorization" => "apikey #{@apikey}", "Content-Type" => "application/xml", } @conn = Faraday.new(@uri_base, headers: headers) do |f| f.response :follow_redirects end end |
Instance Attribute Details
#apikey ⇒ String (readonly)
Reads the key for API calls.
11 12 13 |
# File 'lib/almapi/almapi.rb', line 11 def apikey @apikey end |
#conn ⇒ Object (readonly)
Faraday object to handle API calls.
21 22 23 |
# File 'lib/almapi/almapi.rb', line 21 def conn @conn end |
#uri_base ⇒ String (readonly)
Reads the URI base for API calls.
16 17 18 |
# File 'lib/almapi/almapi.rb', line 16 def uri_base @uri_base end |
Instance Method Details
#delete(resource) ⇒ Response
Handles a DELETE request creating the complete URI.
97 98 99 100 101 |
# File 'lib/almapi/almapi.rb', line 97 def delete(resource) url_api = "#{@uri_base}#{resource}" puts "[Almapi::Api.delete] INFO URL #{url_api}" handle_response(@conn.delete(url_api), "DELETE") end |
#get(resource, resumption_token = "") ⇒ Response
Handles a GET request creating the complete URI. It handles the general case, the case of barcode use for items and the case of analytics. If analytics, handles the resumption_token and limit is set to 1000 which is the maximum.
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/almapi/almapi.rb', line 54 def get(resource, resumption_token = "") url_api = if resource.include?("analytics") # API call to Analytics entry point "#{@uri_base}{resource}&limit=1000&#{resumption_token}" # elsif resource.include?("barcode") # API call to items entry point with barcode # "#{@uri_base}#{resource}" else "#{@uri_base}#{resource}" # All other cases end puts "[Almapi::Api.get] INFO URL #{url_api}" handle_response(@conn.get(url_api), "GET") end |
#post(resource, data) ⇒ Response
Handles a POST request creating the complete URI.
74 75 76 77 78 |
# File 'lib/almapi/almapi.rb', line 74 def post(resource, data) url_api = "#{@uri_base}#{resource}" puts "[Almapi::Api.post] INFO URL #{url_api}" handle_response(@conn.post(url_api, data.to_s), "POST") end |
#put(resource, data) ⇒ Response
Handles a PUT request creating the complete URI.
86 87 88 89 90 |
# File 'lib/almapi/almapi.rb', line 86 def put(resource, data) url_api = "#{@uri_base}#{resource}" puts "[Almapi::Api.put] INFO URL #{url_api}" handle_response(@conn.put(url_api, data.to_s), "PUT") end |