Class: JustGiving::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/justgiving/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(token, environment = nil) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
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
# File 'lib/justgiving/client.rb', line 9

def initialize(token, environment=nil)
  if environment.nil? || environment == :production
    environment = :production
    @base_url = "https://api.justgiving.com/#{@token}/v1/"
  elsif environment == :staging
    @base_url = "https://api-staging.justgiving.com/#{@token}/v1/"
  elsif environment == :sandbox
    @base_url = "https://api-sandbox.justgiving.com/#{@token}/v1/"
  else
    raise Error::InvalidEnvironment
  end

  @token = token
  @environment = environment
  @connection_defaults = {
    url: @base_url,
    headers: {
      'User-Agent' => 'Ruby REST client',
      'Content-Type' => 'application/json'
    }
  }

  @connection = Faraday.new(@connection_defaults) do |connection|
    connection.request  :json

    connection.response :json, :content_type => /\bjson$/

    connection.use      Error::RaiseError
    connection.use      Logger
    connection.adapter  Faraday.default_adapter
  end
end