Class: Virtuous::Client
- Inherits:
-
Object
- Object
- Virtuous::Client
- Includes:
- Contact, ContactAddress, Gift, GiftDesignation, Individual, Project, RecurringGift
- Defined in:
- lib/virtuous/client.rb,
lib/virtuous/client/gift.rb,
lib/virtuous/client/contact.rb,
lib/virtuous/client/project.rb,
lib/virtuous/client/individual.rb,
lib/virtuous/client/recurring_gift.rb,
lib/virtuous/client/contact_address.rb,
lib/virtuous/client/gift_designation.rb
Overview
An API client for Virtuous. See #initialize for a full list of supported configuration options.
Authentication
Api key auth
To generate an api key you need to visit the
virtuous connect dashboard.
Then you can use the key by setting the api_key
param while creating the client or
by setting the VIRTUOUS_KEY
environment variable beforehand.
client = Virtuous::Client.new(
api_key: api_key,
# ...
)
Oauth
First, an access token needs to be fetched by providing a user's email and password. This will return an access token that lasts for 15 days, and a refresh token that should be stored and used to create clients in the future. The client will use the expiry date of the access token to automatically determine when a new one needs to be fetched.
client = Virtuous::Client.new
client.authenticate(email, password)
user.update(
access_token: client.access_token, refresh_token: client.refresh_token,
token_expiration: client.expires_at
)
# Afterwards
client = Virtuous::Client.new(
access_token: user.access_token, refresh_token: user.refresh_token,
expires_at: user.token_expiration
)
# Use client
if client.refreshed
# Update values if they changed
user.update(
access_token: client.access_token, refresh_token: client.refresh_token,
token_expiration: client.expires_at
)
end
Two-Factor Authentication
client = Virtuous::Client.new
response = client.authenticate(email, password)
if response[:requires_otp]
# Prompt user for OTP
client.authenticate(email, password, otp)
end
Check resource modules to see available client methods:
Defined Under Namespace
Modules: Contact, ContactAddress, Gift, GiftDesignation, Individual, Project, RecurringGift
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Access token used for OAuth authentication.
-
#expires_at ⇒ Object
readonly
Expiration date of the access token.
-
#refresh_token ⇒ Object
readonly
Token used to refresh the access token when it has expired.
-
#refreshed ⇒ Object
readonly
True if the access token has been refreshed.
Instance Method Summary collapse
-
#authenticate(email, password, otp = nil) ⇒ Hash
Send a request to get an access token using the email and password of a user.
-
#delete(path, body = {}) ⇒ Object
Makes a
DELETE
request to the path. -
#get(path, body = {}) ⇒ Object
Makes a
GET
request to the path. -
#initialize(**config) ⇒ Client
constructor
A new instance of Client.
-
#patch(path, body = {}) ⇒ Object
Makes a
PATCH
request to the path. -
#post(path, body = {}) ⇒ Object
Makes a
POST
request to the path. -
#put(path, body = {}) ⇒ Object
Makes a
PUT
request to the path.
Methods included from Project
#project_query_options, #query_projects
Methods included from GiftDesignation
#gift_designation_query_options, #query_gift_designations
Methods included from RecurringGift
#create_recurring_gift, #get_recurring_gift, #update_recurring_gift
Methods included from Gift
#create_gift, #create_gifts, #delete_gift, #find_gift_by_transaction_id, #get_contact_gifts, #get_gift, #import_gift, #import_gifts, #update_gift
Methods included from Individual
#create_individual, #delete_individual, #find_individual_by_email, #get_individual, #update_individual
Methods included from ContactAddress
#create_contact_address, #get_contact_addresses, #update_contact_address
Methods included from Contact
#create_contact, #find_contact_by_email, #get_contact, #import_contact, #update_contact
Constructor Details
#initialize(**config) ⇒ Client
Returns a new instance of Client.
103 104 105 106 107 |
# File 'lib/virtuous/client.rb', line 103 def initialize(**config) read_config(config) @refreshed = false end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Access token used for OAuth authentication.
82 83 84 |
# File 'lib/virtuous/client.rb', line 82 def access_token @access_token end |
#expires_at ⇒ Object (readonly)
Expiration date of the access token.
88 89 90 |
# File 'lib/virtuous/client.rb', line 88 def expires_at @expires_at end |
#refresh_token ⇒ Object (readonly)
Token used to refresh the access token when it has expired.
85 86 87 |
# File 'lib/virtuous/client.rb', line 85 def refresh_token @refresh_token end |
#refreshed ⇒ Object (readonly)
True if the access token has been refreshed.
91 92 93 |
# File 'lib/virtuous/client.rb', line 91 def refreshed @refreshed end |
Instance Method Details
#authenticate(email, password, otp = nil) ⇒ Hash
Send a request to get an access token using the email and password of a user.
179 180 181 182 183 |
# File 'lib/virtuous/client.rb', line 179 def authenticate(email, password, otp = nil) data = { grant_type: 'password', username: email, password: password } data[:otp] = otp unless otp.nil? get_access_token(data) end |
#delete(path, body = {}) ⇒ Object
Makes a DELETE
request to the path.
|
# File 'lib/virtuous/client.rb', line 127
|
#get(path, body = {}) ⇒ Object
Makes a GET
request to the path.
|
# File 'lib/virtuous/client.rb', line 109
|
#patch(path, body = {}) ⇒ Object
Makes a PATCH
request to the path.
|
# File 'lib/virtuous/client.rb', line 136
|
#post(path, body = {}) ⇒ Object
Makes a POST
request to the path.
|
# File 'lib/virtuous/client.rb', line 118
|
#put(path, body = {}) ⇒ Object
Makes a PUT
request to the path.
153 154 155 156 157 |
# File 'lib/virtuous/client.rb', line 153 [:get, :post, :delete, :patch, :put].each do |http_method| define_method(http_method) do |path, body = {}| connection.public_send(http_method, path, body).body end end |