Module: Dwolla

Defined in:
lib/dwolla.rb,
lib/dwolla/json.rb,
lib/dwolla/oauth.rb,
lib/dwolla/users.rb,
lib/dwolla/balance.rb,
lib/dwolla/masspay.rb,
lib/dwolla/version.rb,
lib/dwolla/accounts.rb,
lib/dwolla/contacts.rb,
lib/dwolla/requests.rb,
lib/dwolla/exceptions.rb,
lib/dwolla/transactions.rb,
lib/dwolla/funding_sources.rb,
lib/dwolla/offsite_gateway.rb,
lib/dwolla/errors/api_error.rb,
lib/dwolla/errors/dwolla_error.rb,
lib/dwolla/errors/api_connection_error.rb,
lib/dwolla/errors/authentication_error.rb,
lib/dwolla/errors/invalid_request_error.rb,
lib/dwolla/errors/missing_parameter_error.rb

Defined Under Namespace

Modules: JSON Classes: APIConnectionError, APIError, Accounts, AuthenticationError, Balance, Contacts, DwollaError, FundingSources, InvalidRequestError, MassPay, MissingParameterError, OAuth, OffsiteGateway, RequestException, Requests, Transactions, Users

Constant Summary collapse

VERSION =
"3.0.2"
@@api_key =
nil
@@api_secret =
nil
@@token =
nil
@@api_base =
'/oauth/rest'
@@verify_ssl_certs =
true
@@api_version =
nil
@@debug =
false
@@sandbox =
false
@@scope =
'send|transactions|balance|request|contacts|accountinfofull|funding|scheduled'

Class Method Summary collapse

Class Method Details

.api_keyObject



48
49
50
# File 'lib/dwolla.rb', line 48

def self.api_key
    @@api_key
end

.api_key=(api_key) ⇒ Object



44
45
46
# File 'lib/dwolla.rb', line 44

def self.api_key=(api_key)
    @@api_key = api_key
end

.api_secretObject



56
57
58
# File 'lib/dwolla.rb', line 56

def self.api_secret
    @@api_secret
end

.api_secret=(api_secret) ⇒ Object



52
53
54
# File 'lib/dwolla.rb', line 52

def self.api_secret=(api_secret)
    @@api_secret = api_secret
end

.api_versionObject



80
81
82
# File 'lib/dwolla.rb', line 80

def self.api_version
    @@api_version
end

.api_version=(api_version) ⇒ Object



76
77
78
# File 'lib/dwolla.rb', line 76

def self.api_version=(api_version)
    @@api_version = api_version
end

.debugObject



68
69
70
# File 'lib/dwolla.rb', line 68

def self.debug
    @@debug
end

.debug=(debug) ⇒ Object



72
73
74
# File 'lib/dwolla.rb', line 72

def self.debug=(debug)
    @@debug = debug
end

.endpoint_url(endpoint) ⇒ Object



116
117
118
# File 'lib/dwolla.rb', line 116

def self.endpoint_url(endpoint)
    self.hostname + @@api_base + endpoint
end

.hostnameObject



108
109
110
111
112
113
114
# File 'lib/dwolla.rb', line 108

def self.hostname
    if not @@sandbox
        return 'https://www.dwolla.com'
    else
        return 'https://sandbox.dwolla.com'
    end
end

.request(method, url, params = {}, headers = {}, oauth = true, parse_response = true, custom_url = false) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/dwolla.rb', line 120

def self.request(method, url, params={}, headers={}, oauth=true, parse_response=true, custom_url=false)
    self.extract_authorization(params, headers, oauth)
    
    if !verify_ssl_certs
        $stderr.puts "WARNING: Running without SSL cert verification."
    else
        ssl_opts = {
            :use_ssl => true
        }
    end

    uname = (@@uname ||= RUBY_PLATFORM =~ /linux|darwin/i ? `uname -a 2>/dev/null`.strip : nil)
    lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
    ua = {
        :bindings_version => Dwolla::VERSION,
        :lang => 'ruby',
        :lang_version => lang_version,
        :platform => RUBY_PLATFORM,
        :publisher => 'dwolla',
        :uname => uname
    }

    url = self.endpoint_url(url) unless custom_url

    case method.to_s.downcase.to_sym
        when :get || :delete
            # Make params into GET/DELETE parameters
            if params && params.count > 0
                uri = Addressable::URI.new
                uri.query_values = params
                url += '?' + uri.query
            end
            payload = nil
        else
            payload = JSON.dump(params)
    end

    begin
        headers = { :x_dwolla_client_user_agent => Dwolla::JSON.dump(ua) }.merge(headers)
    rescue => e
        headers = {
            :x_dwolla_client_raw_user_agent => ua.inspect,
            :error => "#{e} (#{e.class})"
        }.merge(headers)
    end

    headers = {
        :user_agent => "Dwolla Ruby API Wrapper/#{Dwolla::VERSION}",
        :content_type => 'application/json'
    }.merge(headers)

    if self.api_version
        headers[:dwolla_version] = self.api_version
    end

    opts = {
        :method => method,
        :url => url,
        :headers => headers,
        :open_timeout => 30,
        :payload => payload,
        :timeout => 80
    }.merge(ssl_opts)

    if self.debug
        if self.sandbox
            puts "[DWOLLA SANDBOX MODE OPERATION]"
        end

        puts "Firing request with options and headers:"
        puts opts
        puts headers
    end

    begin
        response = execute_request(opts)
    rescue SocketError => e
        self.handle_restclient_error(e)
    rescue NoMethodError => e
        # Work around RestClient bug
        if e.message =~ /\WRequestFailed\W/
            e = APIConnectionError.new('Unexpected HTTP response code')
            self.handle_restclient_error(e)
        else
            raise
        end
    rescue RestClient::ExceptionWithResponse => e
        if rcode = e.http_code and rbody = e.http_body
            self.handle_api_error(rcode, rbody)
        else
            self.handle_restclient_error(e)
        end
    rescue RestClient::Exception, Errno::ECONNREFUSED => e
        self.handle_restclient_error(e)
    end

    rbody = response.body
    rcode = response.code

    if self.debug
        puts "Raw response headers received:"
        puts headers
        puts "Raw response body received:"
        puts rbody
    end

    resp = self.extract_json(rbody, rcode)

    if parse_response
        return self.parse_response(resp)
    else
        return resp
    end
end

.sandboxObject



64
65
66
# File 'lib/dwolla.rb', line 64

def self.sandbox
    @@sandbox
end

.sandbox=(sandbox) ⇒ Object



60
61
62
# File 'lib/dwolla.rb', line 60

def self.sandbox=(sandbox)
    @@sandbox = sandbox
end

.scopeObject



104
105
106
# File 'lib/dwolla.rb', line 104

def self.scope
    @@scope
end

.scope=(scope) ⇒ Object



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

def self.scope=(scope)
    @@scope = scope
end

.tokenObject



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

def self.token
    @@token
end

.token=(token) ⇒ Object



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

def self.token=(token)
    @@token = token
end

.verify_ssl_certsObject



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

def self.verify_ssl_certs
    @@verify_ssl_certs
end

.verify_ssl_certs=(verify_ssl_certs) ⇒ Object



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

def self.verify_ssl_certs=(verify_ssl_certs)
    @@verify_ssl_certs = verify_ssl_certs
end