Class: Givepulse::Client

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials = nil) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



56
57
58
59
60
61
62
63
# 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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/givepulse/client.rb', line 14

def connection
  @connection
end

#consumer_key=(value) ⇒ Object (writeonly)

Sets the attribute consumer_key

Parameters:

  • value

    the value to set the attribute consumer_key to.



9
10
11
# File 'lib/givepulse/client.rb', line 9

def consumer_key=(value)
  @consumer_key = value
end

#consumer_secret=(value) ⇒ Object (writeonly)

Sets the attribute consumer_secret

Parameters:

  • value

    the value to set the attribute consumer_secret to.



10
11
12
# File 'lib/givepulse/client.rb', line 10

def consumer_secret=(value)
  @consumer_secret = value
end

#user_email=(value) ⇒ Object (writeonly)

Sets the attribute user_email

Parameters:

  • value

    the value to set the attribute user_email to.



11
12
13
# File 'lib/givepulse/client.rb', line 11

def user_email=(value)
  @user_email = value
end

#user_password=(value) ⇒ Object (writeonly)

Sets the attribute user_password

Parameters:

  • value

    the value to set the attribute user_password to.



12
13
14
# File 'lib/givepulse/client.rb', line 12

def user_password=(value)
  @user_password = value
end

Instance Method Details

#authorize!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# 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?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
# 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) ⇒ Object



28
29
30
31
32
33
# 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

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# 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