Class: BraintreeHttp::HttpClient
- Inherits:
-
Object
- Object
- BraintreeHttp::HttpClient
- Defined in:
- lib/braintreehttp/http_client.rb
Instance Attribute Summary collapse
-
#environment ⇒ Object
Returns the value of attribute environment.
Instance Method Summary collapse
- #_add_file_part(key, file) ⇒ Object
- #_add_form_field(key, value) ⇒ Object
- #_mime_type_for_file_name(filename) ⇒ Object
- #_parse_response(response) ⇒ Object
- #add_injector(&block) ⇒ Object
- #deserialize_response(response_body, headers) ⇒ Object
- #execute(request) ⇒ Object
- #has_body(request) ⇒ Object
-
#initialize(environment) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #serialize_request(request) ⇒ Object
- #user_agent ⇒ Object
Constructor Details
#initialize(environment) ⇒ HttpClient
Returns a new instance of HttpClient.
12 13 14 15 16 |
# File 'lib/braintreehttp/http_client.rb', line 12 def initialize(environment) @environment = environment @injectors = [] @encoder = Encoder.new end |
Instance Attribute Details
#environment ⇒ Object
Returns the value of attribute environment.
10 11 12 |
# File 'lib/braintreehttp/http_client.rb', line 10 def environment @environment end |
Instance Method Details
#_add_file_part(key, file) ⇒ Object
86 87 88 89 90 |
# File 'lib/braintreehttp/http_client.rb', line 86 def _add_file_part(key, file) mime_type = _mime_type_for_file_name(file.path) return "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{File.basename(file.path)}\"#{LINE_FEED}" + "Content-Type: #{mime_type}#{LINE_FEED}#{LINE_FEED}#{file.read}#{LINE_FEED}" end |
#_add_form_field(key, value) ⇒ Object
82 83 84 |
# File 'lib/braintreehttp/http_client.rb', line 82 def _add_form_field(key, value) return "Content-Disposition: form-data; name=\"#{key}\"#{LINE_FEED}#{LINE_FEED}#{value}#{LINE_FEED}" end |
#_mime_type_for_file_name(filename) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/braintreehttp/http_client.rb', line 92 def _mime_type_for_file_name(filename) file_extension = File.extname(filename).strip.downcase[1..-1] if file_extension == "jpeg" || file_extension == "jpg" return "image/jpeg" elsif file_extension == "png" return "image/png" elsif file_extension == "pdf" return "application/pdf" else return "application/octet-stream" end end |
#_parse_response(response) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/braintreehttp/http_client.rb', line 105 def _parse_response(response) status_code = response.code.to_i result = response.body headers = response.to_hash if result && !result.empty? deserialized = deserialize_response(response.body, headers) if deserialized.is_a?(String) || deserialized.is_a?(Array) result = deserialized else result = OpenStruct.new(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 |
#add_injector(&block) ⇒ Object
22 23 24 |
# File 'lib/braintreehttp/http_client.rb', line 22 def add_injector(&block) @injectors << block end |
#deserialize_response(response_body, headers) ⇒ Object
78 79 80 |
# File 'lib/braintreehttp/http_client.rb', line 78 def deserialize_response(response_body, headers) @encoder.deserialize_response(response_body, headers) end |
#execute(request) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/braintreehttp/http_client.rb', line 30 def execute(request) if !request.headers request.headers = {} end @injectors.each do |injector| injector.call(request) end if !request.headers["User-Agent"] || request.headers["User-Agent"] == "Ruby" request.headers["User-Agent"] = user_agent end httpRequest = Net::HTTPGenericRequest.new(request.verb, true, true, request.path, request.headers) content_type = request.headers["Content-Type"] if content_type && content_type.start_with?("multipart/") boundary = DateTime.now.strftime("%Q") httpRequest.set_content_type("multipart/form-data; boundary=#{boundary}") form_params = [] request.body.each do |k, v| if v.is_a? File form_params.push(_add_file_part(k, v)) else form_params.push(_add_form_field(k, v)) end end httpRequest.body = form_params.collect {|p| "--" + boundary + "#{LINE_FEED}" + p}.join("") + "--" + boundary + "--" elsif has_body(request) if request.body.is_a? String httpRequest.body = request.body else httpRequest.body = serialize_request(request) end end uri = URI(@environment.base_url) Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| _parse_response(http.request(httpRequest)) end end |
#has_body(request) ⇒ Object
26 27 28 |
# File 'lib/braintreehttp/http_client.rb', line 26 def has_body(request) request.respond_to?(:body) and request.body end |
#serialize_request(request) ⇒ Object
74 75 76 |
# File 'lib/braintreehttp/http_client.rb', line 74 def serialize_request(request) @encoder.serialize_request(request) end |
#user_agent ⇒ Object
18 19 20 |
# File 'lib/braintreehttp/http_client.rb', line 18 def user_agent "BraintreeHttp-Ruby HTTP/1.1" end |