Class: Givepulse::Client
- Inherits:
-
Object
- Object
- Givepulse::Client
- Defined in:
- lib/givepulse/client.rb
Overview
Class that will call any methods for retrieving data from the Givepulse API
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#consumer_key ⇒ Object
writeonly
Sets the attribute consumer_key.
-
#consumer_secret ⇒ Object
writeonly
Sets the attribute consumer_secret.
-
#user_email ⇒ Object
writeonly
Sets the attribute user_email.
-
#user_password ⇒ Object
writeonly
Sets the attribute user_password.
Instance Method Summary collapse
- #authorize! ⇒ Object
- #authorized? ⇒ Boolean
- #credentials(credentials) ⇒ Object
-
#initialize(credentials = nil) ⇒ Client
constructor
A new instance of Client.
- #method_missing(method_name, *args, &block) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
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
#connection ⇒ Object (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
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
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
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
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 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. = response['token'] @authorization_expiration = Time.new + (60 * 60 * 2) end true end |
#authorized? ⇒ Boolean
49 50 51 52 53 54 |
# File 'lib/givepulse/client.rb', line 49 def return false unless @authorization_expiration # Reset the authorization token if it's expired @connection. = 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
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 |