Class: Pin::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/pin_up/base.rb

Overview

This class sets up a few things like the base URL and provides a few utility methods to be shared between classes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = '', env = :live, timeout = 1800) ⇒ Base

Create a new Pin instance Args:

key: Your Pin secret key
env: The environment you want to use.
  Leave blank for live and pass :test for test

An error is raised if an invalid env is passed in.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pin_up/base.rb', line 16

def initialize(key = '', env = :live, timeout = 1800)
  @key = key
  env = env.to_sym
  @@auth = { username: @key, password: '' }
  @@timeout = timeout
  @@base_url = if env == :live
                 'https://api.pinpayments.com/1/'
               elsif env == :test
                 'https://test-api.pinpayments.com/1/'
               else
                 fail "'env' option must be :live or :test. Leave blank for live payments"
               end
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/pin_up/base.rb', line 8

def key
  @key
end

Class Method Details

.build_collection_response(response, pagination = false) ⇒ Object

Builds a response of a collection if the response code is 200

otherwise an empty array is returned


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pin_up/base.rb', line 59

def self.build_collection_response(response, pagination = false)
  models = []
  if response.code == 200
    if pagination
      response.parsed_response['response'].each do |model|
        models << model
      end
      return {
        response: models,
        pagination: response.parsed_response['pagination']
      }
    else
      response.parsed_response['response'].each do |model|
        models << model
      end
    end
  elsif response.code >= 400
    Pin::PinError.handle_error(response.code, response.parsed_response)
  end
  # models
end

.build_response(response) ⇒ Object

Builds a response of a single object



46
47
48
49
50
51
52
53
54
# File 'lib/pin_up/base.rb', line 46

def self.build_response(response)
  if response.code >= 400
    Pin::PinError.handle_error(response.code, response.parsed_response)
  elsif response.code == 204
    response
  else
    response.parsed_response['response']
  end
end

.make_request(method, args) ⇒ Object

Sends an authenticated request to pin’s server args: method (Symbol), args (Hash) eg. args => { url: ‘cards’, options: { … } }



40
41
42
# File 'lib/pin_up/base.rb', line 40

def self.make_request(method, args)
  Pin::Client.new(method, args, @@base_url, @@auth, @@timeout).make_request
end

Instance Method Details

#base_uriObject

Provides access to the base URL if needed



32
33
34
# File 'lib/pin_up/base.rb', line 32

def base_uri
  @@base_url
end