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(inj) ⇒ 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.
10 11 12 13 14 |
# File 'lib/braintreehttp/http_client.rb', line 10 def initialize(environment) @environment = environment @injectors = [] @encoder = Encoder.new end |
Instance Attribute Details
#environment ⇒ Object
Returns the value of attribute environment.
8 9 10 |
# File 'lib/braintreehttp/http_client.rb', line 8 def environment @environment end |
Instance Method Details
#_add_file_part(key, file) ⇒ Object
84 85 86 87 88 |
# File 'lib/braintreehttp/http_client.rb', line 84 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
80 81 82 |
# File 'lib/braintreehttp/http_client.rb', line 80 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
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/braintreehttp/http_client.rb', line 90 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
103 104 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 |
# File 'lib/braintreehttp/http_client.rb', line 103 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 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 ServiceIOError.new(obj.status_code, obj.result, obj.headers) end end |
#add_injector(inj) ⇒ Object
20 21 22 |
# File 'lib/braintreehttp/http_client.rb', line 20 def add_injector(inj) @injectors << inj end |
#deserialize_response(response_body, headers) ⇒ Object
76 77 78 |
# File 'lib/braintreehttp/http_client.rb', line 76 def deserialize_response(response_body, headers) @encoder.deserialize_response(response_body, headers) end |
#execute(request) ⇒ Object
28 29 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 |
# File 'lib/braintreehttp/http_client.rb', line 28 def execute(request) if !request.headers request.headers = {} end @injectors.each do |injector| injector.inject(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
24 25 26 |
# File 'lib/braintreehttp/http_client.rb', line 24 def has_body(request) request.respond_to?(:body) and request.body end |
#serialize_request(request) ⇒ Object
72 73 74 |
# File 'lib/braintreehttp/http_client.rb', line 72 def serialize_request(request) @encoder.serialize_request(request) end |
#user_agent ⇒ Object
16 17 18 |
# File 'lib/braintreehttp/http_client.rb', line 16 def user_agent "BraintreeHttp-Ruby HTTP/1.1" end |