Class: NationBuilder::Client
- Inherits:
-
Object
- Object
- NationBuilder::Client
- Defined in:
- lib/nationbuilder/client.rb
Constant Summary collapse
- REQUIRED_ATTRIBUTES =
%i[slug token refresh_token token_expires_at].freeze
Instance Method Summary collapse
- #call(action, path, body = {}) ⇒ Object
-
#initialize(nation, options = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(nation, options = {}) ⇒ Client
Returns a new instance of Client.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/nationbuilder/client.rb', line 6 def initialize(nation, = {}) REQUIRED_ATTRIBUTES.each do |attribute| next if nation[attribute] raise ArgumentError, "NationBuilder::Client nation must respond to #{attribute}" end slug = nation[:slug] token = nation[:token] refresh_token = nation[:refresh_token] token_expires_at = nation[:token_expires_at] @options = @nation = { slug:, token:, refresh_token:, token_expires_at: } end |
Instance Method Details
#call(action, path, body = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/nationbuilder/client.rb', line 28 def call(action, path, body = {}) url = NationBuilder::Utils::UrlBuilder.call(@nation, path) response = HTTParty.send( action, url, body: body, headers: {Accept: "application/json", "Content-type": "application/json"}.merge(@options.fetch(:headers, {})), timeout: @options.fetch(:timeout, 30), uri_adapter: @options.fetch(:uri_adapter, Addressable::URI) ) response_body = JSON.parse(response.body || "{}") if response.success? {status: response_status(response.code), code: response.code, body: response_body} elsif response.code == 429 && response.headers["retry-after"] sleep(response.headers["retry-after"].to_i + 1) call(action, path, body) elsif expired_token_error?(response_body) && refresh_oauth_token call(action, path, body) else raise OAuth2::Error.new(response) end end |