Class: SiebelDonations::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/siebel_donations/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
# File 'lib/siebel_donations/base.rb', line 8

def initialize(json = {})
  json.each do |key, value|
    instance_variable_set("@#{key.underscore}", value)
  end
end

Class Method Details

.find(params) ⇒ Object



45
46
47
# File 'lib/siebel_donations/base.rb', line 45

def self.find(params)
  get(path, params).collect { |json| new(json) }
end

.get(path, params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/siebel_donations/base.rb', line 14

def self.get(path, params)
  raise 'You need to configure SiebelDonations with your oauth_token.' unless SiebelDonations.oauth_token

  request_timeout = params[:timeout] || SiebelDonations.default_timeout
  params[:response_timeout] ||= request_timeout
  retry_count = params.delete(:retry_count) || SiebelDonations.default_retry_count

  url = SiebelDonations.base_url + path
  headers = { params: params, authorization: "Bearer #{SiebelDonations.oauth_token}" }

  retryable_errors = [RestClient::InternalServerError, Timeout::Error, Errno::ECONNRESET]

  Retryable.retryable on: retryable_errors, times: retry_count, sleep: 20 do
    request_params = { method: :get, url: url, headers: headers, timeout: request_timeout }
    RestClient::Request.execute(request_params) do |response, request, result, &block|
      case response.code
      when 200
        Oj.load(response)
      when 400
        raise RestClient::BadRequest, response
      when 500
        raise RestClient::InternalServerError, response
      else
        puts response.inspect
        puts request.inspect
        raise result.inspect
      end
    end
  end
end