Class: DwollaV2::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dwolla_v2/client.rb

Constant Summary collapse

ENVIRONMENTS =
{
  :production => {
    :auth_url  => "https://accounts.dwolla.com/auth",
    :token_url => "https://api.dwolla.com/token",
    :api_url   => "https://api.dwolla.com"
  },
  :sandbox => {
    :auth_url  => "https://accounts-sandbox.dwolla.com/auth",
    :token_url => "https://api-sandbox.dwolla.com/token",
    :api_url   => "https://api-sandbox.dwolla.com"
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dwolla_v2/client.rb', line 23

def initialize **opts
  opts[:id] ||= opts[:key]
  raise ArgumentError.new ":key is required" unless opts[:id].is_a? String
  raise ArgumentError.new ":secret is required" unless opts[:secret].is_a? String
  @id = opts[:id]
  @secret = opts[:secret]
  self.environment = opts[:environment] if opts.has_key?(:environment)
  yield self if block_given?
  conn
  @auths = Portal.new self, Auth
  @tokens = Portal.new self, Token
  @token_manager = TokenManager.new(self)
  freeze
end

Instance Attribute Details

#authsObject (readonly)

Returns the value of attribute auths.



18
19
20
# File 'lib/dwolla_v2/client.rb', line 18

def auths
  @auths
end

#idObject (readonly) Also known as: key

Returns the value of attribute id.



18
19
20
# File 'lib/dwolla_v2/client.rb', line 18

def id
  @id
end

#secretObject (readonly)

Returns the value of attribute secret.



18
19
20
# File 'lib/dwolla_v2/client.rb', line 18

def secret
  @secret
end

#tokensObject (readonly)

Returns the value of attribute tokens.



18
19
20
# File 'lib/dwolla_v2/client.rb', line 18

def tokens
  @tokens
end

Instance Method Details

#api_urlObject



96
97
98
# File 'lib/dwolla_v2/client.rb', line 96

def api_url
  ENVIRONMENTS[environment][:api_url]
end

#auth(params = {}) ⇒ Object



73
74
75
# File 'lib/dwolla_v2/client.rb', line 73

def auth(params = {})
  DwollaV2::Auth.new(self, params)
end

#auth_urlObject



88
89
90
# File 'lib/dwolla_v2/client.rb', line 88

def auth_url
  ENVIRONMENTS[environment][:auth_url]
end

#connObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dwolla_v2/client.rb', line 59

def conn
  @conn ||= Faraday.new do |f|
    f.request :basic_auth, id, secret
    f.request :url_encoded
    f.use SetUserAgent
    f.use HandleErrors
    f.use DeepSuperHasherizeResponseBody
    f.use DeepParseIso8601ResponseBody
    f.response :json, :content_type => /\bjson$/
    faraday.call(f) if faraday
    f.adapter Faraday.default_adapter unless faraday
  end
end

#environment(env = nil) ⇒ Object



44
45
46
47
# File 'lib/dwolla_v2/client.rb', line 44

def environment env = nil
  self.environment = env unless env.nil?
  @environment || :production
end

#environment=(env) ⇒ Object



38
39
40
41
42
# File 'lib/dwolla_v2/client.rb', line 38

def environment= env
  env = :"#{env}"
  raise ArgumentError.new "invalid environment" unless ENVIRONMENTS.has_key? env
  @environment = env
end

#faraday(&block) ⇒ Object



54
55
56
57
# File 'lib/dwolla_v2/client.rb', line 54

def faraday &block
  @faraday = block if block
  @faraday
end

#inspectObject



100
101
102
# File 'lib/dwolla_v2/client.rb', line 100

def inspect
  Util.pretty_inspect self.class.name, key: id, environment: environment
end

#on_grant(&callback) ⇒ Object



49
50
51
52
# File 'lib/dwolla_v2/client.rb', line 49

def on_grant &callback
  @on_grant = callback if callback
  @on_grant
end

#refresh_token(params = {}) ⇒ Object



77
78
79
80
81
82
# File 'lib/dwolla_v2/client.rb', line 77

def refresh_token(params = {})
  unless params.is_a?(Hash) && params.has_key?(:refresh_token)
    raise ArgumentError.new(":refresh_token is required")
  end
  auths.refresh(params, params)
end

#token(params = {}) ⇒ Object



84
85
86
# File 'lib/dwolla_v2/client.rb', line 84

def token(params = {})
  tokens.new(params)
end

#token_urlObject



92
93
94
# File 'lib/dwolla_v2/client.rb', line 92

def token_url
  ENVIRONMENTS[environment][:token_url]
end