class JustGiving::Client

Public Class Methods

new(token, environment=nil) click to toggle source
# 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

Protected Instance Methods

get(path, query=nil) click to toggle source
# File lib/justgiving/client.rb, line 44
def get(path, query=nil)
  response = @connection.get do |request|
    request.url path
    request.params.merge! query unless query.nil?
  end
  response.body
end
head(path) click to toggle source
# File lib/justgiving/client.rb, line 68
def head(path)
  response = @connection.head do |request|
    request.url path
  end
  true # We expect Error::NoResults to be thrown if the query is unsuccessful
end
post(path, payload=nil) click to toggle source
# File lib/justgiving/client.rb, line 52
def post(path, payload=nil)
  response = @connection.post do |request|
    request.url path
    request.body = payload unless payload.nil?
  end
  response.body
end
put(path, payload=nil) click to toggle source
# File lib/justgiving/client.rb, line 60
def put(path, payload=nil)
  response = @connection.put do |request|
    request.url path
    request.body = payload unless payload.nil?
  end
  response.body
end