Class: HeartlandRetail::Client
- Inherits:
-
Object
- Object
- HeartlandRetail::Client
- Defined in:
- lib/heartland/client.rb,
lib/heartland/client/uri.rb,
lib/heartland/client/body.rb,
lib/heartland/client/errors.rb,
lib/heartland/client/resource.rb,
lib/heartland/client/response.rb,
lib/heartland/client/collection.rb
Overview
The main point of interaction for the Heartland Retail Client library.
Client code must successfully authenticate with the API via the #auth method before calling any HTTP methods or the API will return authorization errors.
Provides direct access to the URI-oriented interface via the HTTP methods. Provides access to the URI-oriented interface via the #[] method.
Direct Known Subclasses
Defined Under Namespace
Modules: Collection Classes: AuthFailed, Body, BodyError, RequestFailed, Resource, Response, URI
Constant Summary collapse
- DEFAULT_PER_PAGE =
Default number of records per page when iterating over collection resources
20
- DEFAULT_TIMEOUT =
Default request timeout in seconds
60
- DEFAULT_CONNECT_TIMEOUT =
Default connection timeout in seconds
10
Instance Attribute Summary collapse
-
#base_uri ⇒ URI
readonly
The client’s base URI.
-
#connection ⇒ Faraday::Connection
readonly
Faraday’s connection.
Instance Method Summary collapse
-
#[](uri) ⇒ Resource
Returns a Resource for the given URI path.
-
#auth(opts = {}) ⇒ true
deprecated
Deprecated.
see #initialize.
-
#count(uri) ⇒ Integer
Returns a count of subordinate resources of the given collection resource URI.
-
#debug=(debug) ⇒ String, Boolean
Set to true to enable debugging to STDOUT or a string to write to the file at that path.
-
#delete(uri, headers = false) ⇒ Response
Performs a DELETE request against the given URI and returns the Response.
-
#delete!(uri, headers = false) ⇒ Response
Performs a DELETE request against the given URI.
-
#each(uri) ⇒ Object
Iterates over each subordinate resource of the given collection resource URI and yields its representation to the given block.
-
#each_page(uri) ⇒ Object
Iterates over each page of subordinate resources of the given collection resource URI and yields the Response to the block.
-
#get(uri, headers = false) ⇒ Response
Performs a GET request against the given URI and returns the Response.
-
#get!(uri, headers = false) ⇒ Response
Performs a GET request against the given URI.
-
#head(uri, headers = false) ⇒ Response
Performs a HEAD request against the given URI and returns the Response.
-
#head!(uri, headers = false) ⇒ Response
Performs a HEAD request against the given URI.
-
#initialize(base_uri, opts = {}) ⇒ Client
constructor
A new instance of Client.
-
#post(uri, body, headers = false) ⇒ Response
Performs a POST request against the given URI and returns the Response.
-
#post!(uri, body, headers = false) ⇒ Response
Performs a POST request against the given URI.
-
#put(uri, body, headers = false) ⇒ Response
Performs a PUT request against the given URI and returns the Response.
-
#put!(uri, body, headers = false) ⇒ Response
Performs a PUT request against the given URI.
Constructor Details
Instance Attribute Details
#base_uri ⇒ URI (readonly)
Returns The client’s base URI.
35 36 37 |
# File 'lib/heartland/client.rb', line 35 def base_uri @base_uri end |
#connection ⇒ Faraday::Connection (readonly)
Returns Faraday’s connection.
39 40 41 |
# File 'lib/heartland/client.rb', line 39 def connection @connection end |
Instance Method Details
#[](uri) ⇒ Resource
Returns a Resource for the given URI path.
173 174 175 |
# File 'lib/heartland/client.rb', line 173 def [](uri) Resource.new(self, uri) end |
#auth(opts = {}) ⇒ true
see #initialize.
Passes the given credentials to the server, storing the session token on success.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/heartland/client.rb', line 74 def auth(opts={}) warn "[DEPRECATION] `auth` is deprecated. Please use `HeartlandRetail::Client.new '#{base_uri}', token: 'secret_token'` instead." unless opts[:username] && opts[:password] raise "Must specify :username and :password" end body = ::URI.encode_www_form \ :auth_key => opts[:username], :password => opts[:password] response = post '/auth/identity/callback', body, 'Content-Type' => 'application/x-www-form-urlencoded' if response.success? @session_cookie = response.headers['set-cookie'] return true else raise AuthFailed, "Heartland Retail auth failed" end end |
#count(uri) ⇒ Integer
Returns a count of subordinate resources of the given collection resource URI.
212 213 214 215 216 |
# File 'lib/heartland/client.rb', line 212 def count(uri) uri = URI.parse(uri) uri.merge_query_values! 'page' => 1, 'per_page' => 1 get!(uri)['total'] end |
#debug=(debug) ⇒ String, Boolean
Set to true to enable debugging to STDOUT or a string to write to the file at that path.
59 60 61 62 |
# File 'lib/heartland/client.rb', line 59 def debug=(debug) @opts[:debug] = debug configure_connection! end |
#delete(uri, headers = false) ⇒ Response
Performs a DELETE request against the given URI and returns the Response.
128 |
# File 'lib/heartland/client.rb', line 128 def delete(uri, headers=false); make_request(:delete, uri, headers); end |
#delete!(uri, headers = false) ⇒ Response
Performs a DELETE request against the given URI. Returns the Response on success and raises a RequestFailed on failure.
137 |
# File 'lib/heartland/client.rb', line 137 def delete!(uri, headers=false); raise_on_fail delete(uri, headers); end |
#each(uri) ⇒ Object
Iterates over each subordinate resource of the given collection resource URI and yields its representation to the given block.
197 198 199 200 201 202 203 |
# File 'lib/heartland/client.rb', line 197 def each(uri) each_page(uri) do |page| page['results'].each do |result| yield result end end end |
#each_page(uri) ⇒ Object
Iterates over each page of subordinate resources of the given collection resource URI and yields the Response to the block.
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/heartland/client.rb', line 180 def each_page(uri) uri = URI.parse(uri) total_pages = nil page = 1 uri.query_values = {'per_page' => DEFAULT_PER_PAGE}.merge(uri.query_values || {}) while total_pages.nil? or page <= total_pages uri.merge_query_values! 'page' => page response = get!(uri) yield response total_pages ||= response['pages'] page += 1 end end |
#get(uri, headers = false) ⇒ Response
Performs a GET request against the given URI and returns the Response.
113 |
# File 'lib/heartland/client.rb', line 113 def get(uri, headers=false); make_request(:get, uri, headers); end |
#get!(uri, headers = false) ⇒ Response
Performs a GET request against the given URI. Returns the Response on success and raises a RequestFailed on failure.
122 |
# File 'lib/heartland/client.rb', line 122 def get!(uri, headers=false); raise_on_fail get(uri, headers); end |
#head(uri, headers = false) ⇒ Response
Performs a HEAD request against the given URI and returns the Response.
98 |
# File 'lib/heartland/client.rb', line 98 def head(uri, headers=false); make_request(:head, uri, headers); end |
#head!(uri, headers = false) ⇒ Response
Performs a HEAD request against the given URI. Returns the Response on success and raises a RequestFailed on failure.
107 |
# File 'lib/heartland/client.rb', line 107 def head!(uri, headers=false); raise_on_fail head(uri, headers); end |
#post(uri, body, headers = false) ⇒ Response
Performs a POST request against the given URI and returns the Response.
158 |
# File 'lib/heartland/client.rb', line 158 def post(uri, body, headers=false); make_request(:post, uri, headers, body); end |
#post!(uri, body, headers = false) ⇒ Response
Performs a POST request against the given URI. Returns the Response on success and raises a RequestFailed on failure.
167 |
# File 'lib/heartland/client.rb', line 167 def post!(uri, body, headers=false); raise_on_fail post(uri, body, headers); end |
#put(uri, body, headers = false) ⇒ Response
Performs a PUT request against the given URI and returns the Response.
143 |
# File 'lib/heartland/client.rb', line 143 def put(uri, body, headers=false); make_request(:put, uri, headers, body); end |
#put!(uri, body, headers = false) ⇒ Response
Performs a PUT request against the given URI. Returns the Response on success and raises a RequestFailed on failure.
152 |
# File 'lib/heartland/client.rb', line 152 def put!(uri, body, headers=false); raise_on_fail put(uri, body, headers); end |