Class: GeniusReferrals::Client
- Inherits:
-
Object
- Object
- GeniusReferrals::Client
- Defined in:
- lib/genius_referrals/client.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#accounts ⇒ Object
readonly
Returns the value of attribute accounts.
-
#advocates ⇒ Object
readonly
Returns the value of attribute advocates.
-
#auth_token ⇒ Object
readonly
Returns the value of attribute auth_token.
-
#authentications ⇒ Object
readonly
Returns the value of attribute authentications.
-
#backoff_factor ⇒ Object
readonly
Returns the value of attribute backoff_factor.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#bonuses ⇒ Object
readonly
Returns the value of attribute bonuses.
-
#campaigns ⇒ Object
readonly
Returns the value of attribute campaigns.
-
#max_retries ⇒ Object
readonly
Returns the value of attribute max_retries.
-
#products ⇒ Object
readonly
Returns the value of attribute products.
-
#redemption_requests ⇒ Object
readonly
Returns the value of attribute redemption_requests.
-
#referrals ⇒ Object
readonly
Returns the value of attribute referrals.
-
#reports ⇒ Object
readonly
Returns the value of attribute reports.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
-
#utilities ⇒ Object
readonly
Returns the value of attribute utilities.
-
#vouchers ⇒ Object
readonly
Returns the value of attribute vouchers.
-
#widgets_packages ⇒ Object
readonly
Returns the value of attribute widgets_packages.
Instance Method Summary collapse
-
#build_request(method, path, params: nil, json: nil) ⇒ Object
Build an authenticated JSON request hash.
-
#initialize(auth_token:, base_url: DEFAULT_BASE_URL, timeout: 30, max_retries: 2, backoff_factor: 0.25, transport: nil, user_agent: "geniusreferrals-ruby/#{VERSION}") ⇒ Client
constructor
Client for the Genius Referrals public API.
-
#request(method, path, params: nil, json: nil) ⇒ Object
Send a request to an API path and return parsed response content.
Constructor Details
#initialize(auth_token:, base_url: DEFAULT_BASE_URL, timeout: 30, max_retries: 2, backoff_factor: 0.25, transport: nil, user_agent: "geniusreferrals-ruby/#{VERSION}") ⇒ Client
Client for the Genius Referrals public API.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/genius_referrals/client.rb', line 20 def initialize(auth_token:, base_url: DEFAULT_BASE_URL, timeout: 30, max_retries: 2, backoff_factor: 0.25, transport: nil, user_agent: "geniusreferrals-ruby/#{VERSION}") raise ArgumentError, 'auth_token is required' if auth_token.to_s.empty? @auth_token = auth_token @base_url = base_url.to_s.sub(%r{/+\z}, '') @timeout = timeout @max_retries = max_retries @backoff_factor = backoff_factor @transport = transport || NetHTTPTransport.new @user_agent = user_agent @accounts = AccountsResource.new(self) @advocates = AdvocatesResource.new(self) @authentications = AuthenticationsResource.new(self) @bonuses = BonusesResource.new(self) @campaigns = CampaignsResource.new(self) @products = ProductsResource.new(self) @redemption_requests = RedemptionRequestsResource.new(self) @referrals = ReferralsResource.new(self) @reports = ReportsResource.new(self) @root = RootResource.new(self) @tags = TagsResource.new(self) @utilities = UtilitiesResource.new(self) @vouchers = VouchersResource.new(self) @widgets_packages = WidgetsPackagesResource.new(self) end |
Instance Attribute Details
#accounts ⇒ Object (readonly)
Returns the value of attribute accounts.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def accounts @accounts end |
#advocates ⇒ Object (readonly)
Returns the value of attribute advocates.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def advocates @advocates end |
#auth_token ⇒ Object (readonly)
Returns the value of attribute auth_token.
9 10 11 |
# File 'lib/genius_referrals/client.rb', line 9 def auth_token @auth_token end |
#authentications ⇒ Object (readonly)
Returns the value of attribute authentications.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def authentications @authentications end |
#backoff_factor ⇒ Object (readonly)
Returns the value of attribute backoff_factor.
9 10 11 |
# File 'lib/genius_referrals/client.rb', line 9 def backoff_factor @backoff_factor end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/genius_referrals/client.rb', line 9 def base_url @base_url end |
#bonuses ⇒ Object (readonly)
Returns the value of attribute bonuses.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def bonuses @bonuses end |
#campaigns ⇒ Object (readonly)
Returns the value of attribute campaigns.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def campaigns @campaigns end |
#max_retries ⇒ Object (readonly)
Returns the value of attribute max_retries.
9 10 11 |
# File 'lib/genius_referrals/client.rb', line 9 def max_retries @max_retries end |
#products ⇒ Object (readonly)
Returns the value of attribute products.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def products @products end |
#redemption_requests ⇒ Object (readonly)
Returns the value of attribute redemption_requests.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def redemption_requests @redemption_requests end |
#referrals ⇒ Object (readonly)
Returns the value of attribute referrals.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def referrals @referrals end |
#reports ⇒ Object (readonly)
Returns the value of attribute reports.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def reports @reports end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def root @root end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def @tags end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
9 10 11 |
# File 'lib/genius_referrals/client.rb', line 9 def timeout @timeout end |
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
9 10 11 |
# File 'lib/genius_referrals/client.rb', line 9 def transport @transport end |
#user_agent ⇒ Object (readonly)
Returns the value of attribute user_agent.
9 10 11 |
# File 'lib/genius_referrals/client.rb', line 9 def user_agent @user_agent end |
#utilities ⇒ Object (readonly)
Returns the value of attribute utilities.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def utilities @utilities end |
#vouchers ⇒ Object (readonly)
Returns the value of attribute vouchers.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def vouchers @vouchers end |
#widgets_packages ⇒ Object (readonly)
Returns the value of attribute widgets_packages.
49 50 51 |
# File 'lib/genius_referrals/client.rb', line 49 def @widgets_packages end |
Instance Method Details
#build_request(method, path, params: nil, json: nil) ⇒ Object
Build an authenticated JSON request hash.
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/genius_referrals/client.rb', line 97 def build_request(method, path, params: nil, json: nil) clean_path = path.start_with?('/') ? path : "/#{path}" url = "#{base_url}#{clean_path}#{query_string(params || {})}" body = json.nil? ? nil : JSON.generate(json) headers = { 'Accept' => 'application/json', 'User-Agent' => user_agent, 'X-Auth-Token' => auth_token } headers['Content-Type'] = 'application/json' unless json.nil? { method: method.to_s.upcase, url: url, headers: headers, body: body } end |
#request(method, path, params: nil, json: nil) ⇒ Object
Send a request to an API path and return parsed response content.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/genius_referrals/client.rb', line 54 def request(method, path, params: nil, json: nil) request = build_request(method, path, params: params, json: json) attempt = 0 loop do begin response = transport.request( request[:method], request[:url], headers: request[:headers], body: request[:body], timeout: timeout ) rescue StandardError => e raise TransportError, e. if attempt >= max_retries sleep_for_retry(attempt) attempt += 1 next end return parse_success(response.status_code, response.body) if response.status_code < 400 if RETRYABLE_STATUS_CODES.include?(response.status_code) && attempt < max_retries sleep_for_retry(attempt) attempt += 1 next end parsed = parse_json(response.body) error_class = ERROR_BY_STATUS.fetch(response.status_code, APIError) raise error_class.new( (response.status_code, parsed, response.body), status_code: response.status_code, response: parsed, body: response.body, request: request, secrets: [auth_token] ) end end |