Class: PushPay::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/pushpay/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client



11
12
13
14
15
16
# File 'lib/pushpay/client.rb', line 11

def initialize(configuration)
  @configuration = configuration
  validate_configuration!
  @access_token = nil
  @token_expires_at = nil
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



9
10
11
# File 'lib/pushpay/client.rb', line 9

def access_token
  @access_token
end

#configurationObject (readonly)

Returns the value of attribute configuration.



9
10
11
# File 'lib/pushpay/client.rb', line 9

def configuration
  @configuration
end

#token_expires_atObject (readonly)

Returns the value of attribute token_expires_at.



9
10
11
# File 'lib/pushpay/client.rb', line 9

def token_expires_at
  @token_expires_at
end

Instance Method Details

#authenticate!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pushpay/client.rb', line 18

def authenticate!
  credentials = Base64.strict_encode64("#{configuration.client_id}:#{configuration.client_secret}")

  scopes = configuration.scopes.empty? ? ["read"] : configuration.scopes
  body = { grant_type: 'client_credentials', scope: scopes.join(' ') }

  response = self.class.post(
    configuration.auth_url,
    body: body,
    headers: {
      'Authorization' => "Basic #{credentials}",
      'Content-Type' => 'application/x-www-form-urlencoded'
    },
    timeout: configuration.timeout
  )

  unless response.success?
    raise AuthenticationError, "Failed to authenticate: #{response.code} - #{response.body}"
  end

  @access_token = response['access_token']
  @token_expires_at = Time.now + (response['expires_in'] || 3600)
  @access_token
end

#delete(path) ⇒ Object



59
60
61
# File 'lib/pushpay/client.rb', line 59

def delete(path)
  request(:delete, path)
end

#get(path, params = {}) ⇒ Object



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

def get(path, params = {})
  request(:get, path, query: params)
end

#patch(path, data = {}) ⇒ Object



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

def patch(path, data = {})
  request(:patch, path, body: data.to_json)
end

#post(path, data = {}) ⇒ Object



47
48
49
# File 'lib/pushpay/client.rb', line 47

def post(path, data = {})
  request(:post, path, body: data.to_json)
end

#put(path, data = {}) ⇒ Object



51
52
53
# File 'lib/pushpay/client.rb', line 51

def put(path, data = {})
  request(:put, path, body: data.to_json)
end