Class: Adapt::Service
- Inherits:
-
Object
- Object
- Adapt::Service
- Defined in:
- lib/adapt/service.rb
Constant Summary collapse
- METHODS =
%w[ CancelPreapproval ConvertCurrency ExecutePayment GetPaymentOptions Pay PaymentDetails Preapproval PreapprovalDetails Refund SetPaymentOptions ].freeze
Instance Attribute Summary collapse
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
-
#environment ⇒ Object
Returns the value of attribute environment.
Instance Method Summary collapse
- #defaults ⇒ Object
- #endpoint(method) ⇒ Object
- #headers ⇒ Object
-
#initialize(credentials) ⇒ Service
constructor
A new instance of Service.
- #request(method, request = {}) ⇒ Object
Constructor Details
#initialize(credentials) ⇒ Service
Returns a new instance of Service.
21 22 23 24 |
# File 'lib/adapt/service.rb', line 21 def initialize(credentials) @credentials = credentials @environment = :sandbox end |
Instance Attribute Details
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
18 19 20 |
# File 'lib/adapt/service.rb', line 18 def credentials @credentials end |
#environment ⇒ Object
Returns the value of attribute environment.
19 20 21 |
# File 'lib/adapt/service.rb', line 19 def environment @environment end |
Instance Method Details
#defaults ⇒ Object
26 27 28 29 30 31 |
# File 'lib/adapt/service.rb', line 26 def defaults { :request_format => 'JSON', :response_format => 'JSON' } end |
#endpoint(method) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/adapt/service.rb', line 57 def endpoint(method) if METHODS.include?(method) root = case environment when :live then LIVE when :beta then BETA else SANDBOX end URI.parse([root, method].join('/')) else raise ArgumentError.new("Invalid method: #{method}") end end |
#headers ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/adapt/service.rb', line 33 def headers { 'X-PAYPAL-SECURITY-USERID' => credentials[:username], 'X-PAYPAL-SECURITY-PASSWORD' => credentials[:password], 'X-PAYPAL-SECURITY-SIGNATURE' => credentials[:signature], 'X-PAYPAL-DEVICE-IPADDRESS' => credentials[:ip], 'X-PAYPAL-REQUEST-DATA-FORMAT' => defaults[:request_format], 'X-PAYPAL-RESPONSE-DATA-FORMAT' => defaults[:response_format], 'X-PAYPAL-APPLICATION-ID' => credentials[:application_id] }.tap do |parameters| if credentials[:subject] parameters['X-PAYPAL-SUBJECT'] = credentials[:subject] end if credentials[:device] parameters['X-PAYPAL-DEVICE-ID'] = credentials[:device] end if credentials[:version] parameters['X-PAYPAL-SERVICE-VERSION'] = credentials[:version] end end end |
#request(method, request = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/adapt/service.rb', line 71 def request(method, request={}) uri = endpoint(method) session = Patron::Session.new.tap do |session| session.timeout = 10 session.insecure = true session.base_url = "#{uri.scheme}://#{uri.host}:#{uri.port}" end session.post(uri.path, request.to_json, headers) end |