Class: ReamazeAPI::Client
- Inherits:
-
Object
- Object
- ReamazeAPI::Client
- Defined in:
- lib/reamaze_api/client.rb
Defined Under Namespace
Classes: RaiseErrorMiddleware
Constant Summary collapse
- HTTP_METHODS =
Public: HTTP methods used by the API
Returns an Array.
%i(get put post)
- API_URL =
Public: URL for Reamaze API.
Returns a String.
"https://%{brand}.reamaze.com/api/v1".freeze
Instance Attribute Summary collapse
-
#http ⇒ Object
readonly
Public: HTTP adapter used for API requests.
Instance Method Summary collapse
-
#articles ⇒ Object
Public: Article resource.
-
#channels ⇒ Object
Public: Channel resource.
-
#commit(method:, path:, params: {}) ⇒ Object
Public: Submits an HTTP request to the upstream API.
-
#contacts ⇒ Object
Public: Contact resource.
-
#conversations ⇒ Object
Public: Conversation resource.
-
#initialize(brand:, login:, token:) ⇒ Client
constructor
Public: Initialize a new Client instance.
-
#messages ⇒ Object
Public: Message resource.
-
#paginate(path, resource, params = {}) ⇒ Object
Public: Performs a GET request on the given path/resource.
Constructor Details
#initialize(brand:, login:, token:) ⇒ Client
Public: Initialize a new Client instance.
brand - Brand name (subdomain from your Reamaze URL) login - Reamaze login token - Reamaze API token
Yields a Faraday::Connection if a block is given.
Returns nothing.
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)
Public: HTTP adapter used for API requests.
Returns a Faraday::Connection.
36 37 38 |
# File 'lib/reamaze_api/client.rb', line 36 def http @http end |
Instance Method Details
#articles ⇒ Object
Public: Article resource.
Returns an Article instance.
64 65 66 |
# File 'lib/reamaze_api/client.rb', line 64 def articles @articles ||= Article.new(self) end |
#channels ⇒ Object
Public: Channel resource.
Returns a Channel instance.
71 72 73 |
# File 'lib/reamaze_api/client.rb', line 71 def channels @channels ||= Channel.new(self) end |
#commit(method:, path:, params: {}) ⇒ Object
Public: Submits an HTTP request to the upstream API.
method - HTTP method (eg: :get, :post) path - API path (without /api/v1 prefix, eg: “/messages”) params - Hash of parameters to send with the request (default: {})
Raises a ReamazeAPI::Error for any HTTP response code 400-599 unless ReamazeAPI.config.exceptions is false.
Returns a Hash.
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
Public: Contact resource.
Returns a Contact instance.
79 80 81 |
# File 'lib/reamaze_api/client.rb', line 79 def contacts @contacts ||= Contact.new(self) end |
#conversations ⇒ Object
Public: Conversation resource.
Returns a Conversation instance.
86 87 88 |
# File 'lib/reamaze_api/client.rb', line 86 def conversations @conversations ||= Conversation.new(self) end |
#messages ⇒ Object
Public: Message resource.
Returns a Message instance.
93 94 95 |
# File 'lib/reamaze_api/client.rb', line 93 def @messages ||= Message.new(self) end |
#paginate(path, resource, params = {}) ⇒ Object
Public: 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.
NOTE: Beware of API rate limiting when using this method with large datasets.
path - API path (without /api/v1 prefix, eg: “/messages”) resource - ReamazeAPI resource name (eg: :messages) params - Hash of parameters to send with the request (default: {})
Returns a Hash.
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 |