Class: DwollaV2::Client

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

Constant Summary collapse

ENVIRONMENTS =
{
  :production => {
    :auth_url  => "https://www.dwolla.com/oauth/v2/authenticate",
    :token_url => "https://www.dwolla.com/oauth/v2/token",
    :api_url   => "https://api.dwolla.com"
  },
  :sandbox => {
    :auth_url  => "https://sandbox.dwolla.com/oauth/v2/authenticate",
    :token_url => "https://sandbox.dwolla.com/oauth/v2/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:



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dwolla_v2/client.rb', line 19

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]
  yield self if block_given?
  conn
  @auths = Portal.new self, Auth
  @tokens = Portal.new self, Token
  freeze
end

Instance Attribute Details

#authsObject (readonly)

Returns the value of attribute auths.



16
17
18
# File 'lib/dwolla_v2/client.rb', line 16

def auths
  @auths
end

#idObject (readonly) Also known as: key

Returns the value of attribute id.



16
17
18
# File 'lib/dwolla_v2/client.rb', line 16

def id
  @id
end

#secretObject (readonly)

Returns the value of attribute secret.



16
17
18
# File 'lib/dwolla_v2/client.rb', line 16

def secret
  @secret
end

#tokensObject (readonly)

Returns the value of attribute tokens.



16
17
18
# File 'lib/dwolla_v2/client.rb', line 16

def tokens
  @tokens
end

Instance Method Details

#api_urlObject



75
76
77
# File 'lib/dwolla_v2/client.rb', line 75

def api_url
  ENVIRONMENTS[environment][:api_url]
end

#auth_urlObject



67
68
69
# File 'lib/dwolla_v2/client.rb', line 67

def auth_url
  ENVIRONMENTS[environment][:auth_url]
end

#connObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dwolla_v2/client.rb', line 53

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



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

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

#environment=(env) ⇒ Object



32
33
34
35
36
# File 'lib/dwolla_v2/client.rb', line 32

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

#faraday(&block) ⇒ Object



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

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

#inspectObject



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

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

#on_grant(&callback) ⇒ Object



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

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

#token_urlObject



71
72
73
# File 'lib/dwolla_v2/client.rb', line 71

def token_url
  ENVIRONMENTS[environment][:token_url]
end