Class: PayPal::Request
- Inherits:
-
Object
- Object
- PayPal::Request
- Defined in:
- lib/paypal/request.rb
Direct Known Subclasses
Constant Summary collapse
- CA_FILE =
File.dirname(__FILE__) + "/cacert.pem"
Instance Attribute Summary collapse
-
#uri ⇒ Object
Parse current API url.
Instance Method Summary collapse
- #api_methods ⇒ Object
-
#default_headers ⇒ Object
Default header for setting user agent.
- #normalize_params(params) ⇒ Object
-
#post(params = {}, headers = {}) ⇒ Object
Post to PayPal api.
-
#prepare_headers(headers) ⇒ Object
Join headers.
-
#prepare_params(params) ⇒ Object
Join params and normalize attribute names.
-
#run(method, params = {}, headers = {}) ⇒ Object
Do a POST request to PayPal API.
Instance Attribute Details
#uri ⇒ Object
Parse current API url.
61 62 63 |
# File 'lib/paypal/request.rb', line 61 def uri @uri end |
Instance Method Details
#api_methods ⇒ Object
78 79 80 |
# File 'lib/paypal/request.rb', line 78 def api_methods {} end |
#default_headers ⇒ Object
Default header for setting user agent
47 48 49 50 51 |
# File 'lib/paypal/request.rb', line 47 def default_headers # :nodoc: { 'User-Agent' => "PayPal::Payment/0.1" } end |
#normalize_params(params) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/paypal/request.rb', line 65 def normalize_params(params) params.inject({}) do |buffer, (name, value)| attr_names = [ATTRIBUTES[name.to_sym]].flatten.compact attr_names << name if attr_names.empty? attr_names.each do |attr_name| buffer[attr_name.to_sym] = respond_to?("build_#{name}") ? send("build_#{name}", value) : value end buffer end end |
#post(params = {}, headers = {}) ⇒ Object
Post to PayPal api
30 31 32 33 34 35 36 37 |
# File 'lib/paypal/request.rb', line 30 def post(params = {}, headers = {}) Curl.post(uri, params) do |http| http.ssl_verify_host = true http.ssl_verify_peer = true http.cacert = CA_FILE headers.each { |key, value| http.headers[key] = value } end end |
#prepare_headers(headers) ⇒ Object
Join headers
41 42 43 |
# File 'lib/paypal/request.rb', line 41 def prepare_headers(headers) # :nodoc: default_headers.merge(headers) end |
#prepare_params(params) ⇒ Object
Join params and normalize attribute names.
55 56 57 |
# File 'lib/paypal/request.rb', line 55 def prepare_params(params) # :nodoc: normalize_params default_params.merge(params) end |
#run(method, params = {}, headers = {}) ⇒ Object
Do a POST request to PayPal API. The method argument is the name of the API method you want to invoke. For instance, if you want to request a new checkout token, you may want to do something like:
response = request.run(:express_checkout)
We normalize the methods name. For a list of what’s being covered, refer to PayPal::Recurring::Request::METHODS constant.
The params hash can use normalized names. For a list, check the PayPal::Recurring::Request::ATTRIBUTES constant.
21 22 23 24 25 26 |
# File 'lib/paypal/request.rb', line 21 def run(method, params = {}, headers = {}) params = prepare_params(params.merge(:method => api_methods.fetch(method, method.to_s))) headers = prepare_headers(headers) response = post(params, headers) Response.process(method, response) end |