Class: Almapi::Api

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • apikey

    : API key to be used in API call

  • uri_base

    : base URI for API call



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

#apikeyString (readonly)

Reads the key for API calls.

Returns:

  • (String)

    the API key



11
12
13
# File 'lib/almapi/almapi.rb', line 11

def apikey
  @apikey
end

#connObject (readonly)

Faraday object to handle API calls.

Returns:

  • (Object)

    the Faraday conn object



21
22
23
# File 'lib/almapi/almapi.rb', line 21

def conn
  @conn
end

#uri_baseString (readonly)

Reads the URI base for API calls.

Returns:

  • (String)

    the URI base



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.

Parameters:

  • resource (String)

    mandatory : the part of the URI specifying the access point. Must not include “?” for it adds “?apikey” automatically.

Returns:

  • (Response)

    : the resulting response. If error occurs, raises an AlmapiError



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.

Parameters:

  • resource (String)

    mandatory : the part of the URI specifying the access point. EXCEPT for analytics and barcode, it must not include “?” for it adds “?apikey” automatically. If analytics, resource must include the report name. If barcode, resource must include the barcode value.

  • resumption_token (String) (defaults to: "")

    : resumption token for an analytics call.

Returns:

  • (Response)

    : the resulting response



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.

Parameters:

  • resource (String)

    mandatory : the part of the URI specifying the access point. Must not include “?” for it adds “?apikey” automatically.

  • data (String)

    : an XML string containing data to post.

Returns:

  • (Response)

    : the resulting response



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.

Parameters:

  • resource (String)

    mandatory : the part of the URI specifying the access point. Must not include “?” for it adds “?apikey” automatically.

  • data (String)

    : an XML string containing data to put.

Returns:

  • (Response)

    : the resulting response. If error occurs, raises an AlmapiError



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