Class: PayPalHttp::HttpClient
- Inherits:
-
Object
- Object
- PayPalHttp::HttpClient
- Defined in:
- lib/paypalhttp/http_client.rb
Instance Attribute Summary collapse
-
#encoder ⇒ Object
Returns the value of attribute encoder.
-
#environment ⇒ Object
Returns the value of attribute environment.
Instance Method Summary collapse
- #_parse_response(response) ⇒ Object
- #_parse_values(values) ⇒ Object
- #add_injector(&block) ⇒ Object
- #execute(req) ⇒ Object
- #format_headers(headers) ⇒ Object
- #has_body(request) ⇒ Object
-
#initialize(environment) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #map_headers(raw_headers, formatted_headers) ⇒ Object
- #user_agent ⇒ Object
Constructor Details
#initialize(environment) ⇒ HttpClient
Returns a new instance of HttpClient.
10 11 12 13 14 |
# File 'lib/paypalhttp/http_client.rb', line 10 def initialize(environment) @environment = environment @injectors = [] @encoder = Encoder.new end |
Instance Attribute Details
#encoder ⇒ Object
Returns the value of attribute encoder.
8 9 10 |
# File 'lib/paypalhttp/http_client.rb', line 8 def encoder @encoder end |
#environment ⇒ Object
Returns the value of attribute environment.
8 9 10 |
# File 'lib/paypalhttp/http_client.rb', line 8 def environment @environment end |
Instance Method Details
#_parse_response(response) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/paypalhttp/http_client.rb', line 91 def _parse_response(response) status_code = response.code.to_i result = response.body headers = response.to_hash if result && !result.empty? deserialized = @encoder.deserialize_response(response.body, format_headers(headers)) if deserialized.is_a?(String) || deserialized.is_a?(Array) result = deserialized else result = _parse_values(deserialized) end else result = nil end obj = OpenStruct.new({ :status_code => status_code, :result => result, :headers => response.to_hash, }) if status_code >= 200 and status_code < 300 return obj elsif raise HttpError.new(obj.status_code, obj.result, obj.headers) end end |
#_parse_values(values) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/paypalhttp/http_client.rb', line 120 def _parse_values(values) obj = nil if values.is_a?(Array) obj = [] values.each do |v| obj << _parse_values(v) end elsif values.is_a?(Hash) obj = OpenStruct.new() values.each do |k, v| obj[k] = _parse_values(v) end else obj = values end obj end |
#add_injector(&block) ⇒ Object
20 21 22 |
# File 'lib/paypalhttp/http_client.rb', line 20 def add_injector(&block) @injectors << block end |
#execute(req) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/paypalhttp/http_client.rb', line 51 def execute(req) headers = req.headers || {} request = OpenStruct.new({ :verb => req.verb, :path => req.path, :headers => headers.clone, :body => req.body, }) if !request.headers request.headers = {} end @injectors.each do |injector| injector.call(request) end formatted_headers = format_headers(request.headers) if !formatted_headers["user-agent"] || formatted_headers["user-agent"] == "Ruby" request.headers["user-agent"] = user_agent end body = nil if has_body(request) raw_headers = request.headers request.headers = formatted_headers body = @encoder.serialize_request(request) request.headers = map_headers(raw_headers, request.headers) end http_request = Net::HTTPGenericRequest.new(request.verb, body != nil, true, request.path, request.headers) http_request.body = body uri = URI(@environment.base_url) Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| _parse_response(http.request(http_request)) end end |
#format_headers(headers) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/paypalhttp/http_client.rb', line 28 def format_headers(headers) formatted_headers = {} headers.each do |key, value| # TODO: Since header is treated as a hash, val is in an array. # Will this cause an issue when accessing and modifying val # Ensure this is the case and will not propegate access issues/errors if key.casecmp("content-type") == 0 value[0].downcase! end formatted_headers[key.downcase] = value end formatted_headers end |
#has_body(request) ⇒ Object
24 25 26 |
# File 'lib/paypalhttp/http_client.rb', line 24 def has_body(request) request.respond_to?(:body) and request.body end |
#map_headers(raw_headers, formatted_headers) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/paypalhttp/http_client.rb', line 42 def map_headers(raw_headers , formatted_headers) raw_headers.each do |key, value| if formatted_headers.key?(key.downcase) == true raw_headers[key] = formatted_headers[key.downcase] end end raw_headers end |
#user_agent ⇒ Object
16 17 18 |
# File 'lib/paypalhttp/http_client.rb', line 16 def user_agent "PayPalHttp-Ruby HTTP/1.1" end |