Class: ReamazeAPI::Client
- Inherits:
-
Object
- Object
- ReamazeAPI::Client
- Defined in:
- lib/reamaze_api/client.rb
Defined Under Namespace
Classes: RaiseErrorMiddleware
Constant Summary
- HTTP_METHODS =
HTTP methods used by the API
%i(get put post)
- API_URL =
URL for Reamaze API.
"https://%{brand}.reamaze.com/api/v1".freeze
Instance Attribute Summary collapse
-
#http ⇒ Object
readonly
HTTP adapter used for API requests.
Instance Method Summary collapse
-
#articles ⇒ Object
Article resource.
-
#channels ⇒ Object
Channel resource.
-
#commit(method:, path:, params: {}) ⇒ Object
Submits an HTTP request to the upstream API.
-
#contacts ⇒ Object
Contact resource.
-
#conversations ⇒ Object
Conversation resource.
-
#initialize(brand:, login:, token:) { ... } ⇒ void
constructor
Initialize a new Client instance.
-
#messages ⇒ Object
Message resource.
-
#paginate(path, resource, params = {}) ⇒ Object
Performs a GET request on the given path/resource.
Constructor Details
#initialize(brand:, login:, token:) { ... } ⇒ void
Initialize a new Client instance.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/reamaze_api/client.rb', line 47 def initialize(brand:, login:, token:) @url = URI.parse(API_URL % { brand: brand }) @http = Faraday.new(url: @url, ssl: { verify: true }) do |builder| builder.request :json builder.response :json builder.use RaiseErrorMiddleware builder.adapter Faraday.default_adapter builder.basic_auth login, token yield builder if block_given? end end |
Instance Attribute Details
#http ⇒ Object (readonly)
HTTP adapter used for API requests.
36 37 38 |
# File 'lib/reamaze_api/client.rb', line 36 def http @http end |
Instance Method Details
#articles ⇒ Object
Article resource.
64 65 66 |
# File 'lib/reamaze_api/client.rb', line 64 def articles @articles ||= Article.new(self) end |
#channels ⇒ Object
Channel resource.
71 72 73 |
# File 'lib/reamaze_api/client.rb', line 71 def channels @channels ||= Channel.new(self) end |
#commit(method:, path:, params: {}) ⇒ Object
Submits an HTTP request to the upstream API.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/reamaze_api/client.rb', line 107 def commit(method:, path:, params: {}) path = "#{@url.path}#{path}" response = @http.run_request(method, path, params, {}) Utils.symbolize_hash(success: response.success?, payload: response.body) rescue ReamazeAPI::Error => e raise if ReamazeAPI.config.exceptions Utils.error_hash(e) end |
#contacts ⇒ Object
Contact resource.
79 80 81 |
# File 'lib/reamaze_api/client.rb', line 79 def contacts @contacts ||= Contact.new(self) end |
#conversations ⇒ Object
Conversation resource.
86 87 88 |
# File 'lib/reamaze_api/client.rb', line 86 def conversations @conversations ||= Conversation.new(self) end |
#messages ⇒ Object
Message resource.
93 94 95 |
# File 'lib/reamaze_api/client.rb', line 93 def @messages ||= Message.new(self) end |
#paginate(path, resource, params = {}) ⇒ Object
Performs a GET request on the given path/resource. If results are more than one page, each additional page is fetched and added to the payload. If any page returns an error response, that response is immediately returned and no further requests are performed.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/reamaze_api/client.rb', line 132 def paginate(path, resource, params = {}) params = Utils.symbolize_hash(params) auto_paginate = params.delete(:auto_paginate) output = get(path, params) page = params.fetch(:page, 1) success = output[:success] payload = output[:payload] page_count = payload[:page_count] if success && auto_paginate && page_count && page_count > page more = paginate(path, resource, params.merge( page: page.next, auto_paginate: true )) if more[:success] && more[:payload] payload[resource].concat more[:payload][resource] else output[:success] = false output[:payload] = more[:payload] end end output end |