class Givepulse::Client

Class that will call any methods for retrieving data from the Givepulse API

Attributes

connection[R]
consumer_key[W]
consumer_secret[W]
user_email[W]
user_password[W]

Public Class Methods

new(credentials = nil) click to toggle source
# File lib/givepulse/client.rb, line 16
def initialize(credentials = nil)
    # Initialize connection object
    @connection = Givepulse::Connection.new

    return if credentials.nil?
    @consumer_key ||= credentials[:consumer_key]
    @consumer_secret ||= credentials[:consumer_secret]
    @user_email ||= credentials[:user_email]
    @user_password ||= credentials[:user_password]
    @authorization_expiration ||= nil
end

Public Instance Methods

authorize!() click to toggle source
# File lib/givepulse/client.rb, line 35
def authorize!
    return false unless @consumer_key && @consumer_secret && @user_email && @user_password
    custom_headers = {
        'Authorization' => "Basic #{generate_header_string}"
    }
    @connection.with_headers(custom_headers) do |connection|
        response = connection.post('/auth', nil)
        return false if response['error']
        @connection.authorization_token = response['token']
        @authorization_expiration = Time.new + (60 * 60 * 2)
    end
    true
end
authorized?() click to toggle source
# File lib/givepulse/client.rb, line 49
def authorized?
    return false unless @authorization_expiration
    # Reset the authorization token if it's expired
    @connection.authorization_token = nil if Time.now > @authorization_expiration
    Time.now < @authorization_expiration
end
credentials(credentials) click to toggle source
# File lib/givepulse/client.rb, line 28
def credentials(credentials)
    @consumer_key ||= credentials[:consumer_key]
    @consumer_secret ||= credentials[:consumer_secret]
    @user_email ||= credentials[:user_email]
    @user_password ||= credentials[:user_password]
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/givepulse/client.rb, line 56
def method_missing(method_name, *args, &block)
    resource_class = Givepulse::ResourceMap.get_resource_class(method_name)
    if resource_class
        resource_class.new(self)
    else
        super
    end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/givepulse/client.rb, line 65
def respond_to_missing?(method_name, include_private = false)
    Givepulse::ResourceMap.get_resource_class(method_name) || super
end

Private Instance Methods

generate_header_string() click to toggle source
# File lib/givepulse/client.rb, line 71
def generate_header_string
    escaped_values = [
        CGI.escape(@consumer_key),
        CGI.escape(@consumer_secret),
        CGI.escape(@user_email),
        CGI.escape(@user_password)
    ]
    Base64.strict_encode64(escaped_values.join(':'))
end