Class: SmartyStreets::NativeSender
- Inherits:
-
Object
- Object
- SmartyStreets::NativeSender
- Defined in:
- lib/smartystreets_ruby_sdk/native_sender.rb
Class Method Summary collapse
- .build_request(smarty_request) ⇒ Object
- .create_query(smarty_request) ⇒ Object
- .set_custom_headers(smarty_headers, request) ⇒ Object
Instance Method Summary collapse
- #build_http(request) ⇒ Object
- #build_smarty_response(native_response) ⇒ Object
-
#initialize(max_timeout = 10, proxy = nil) ⇒ NativeSender
constructor
A new instance of NativeSender.
- #send(smarty_request) ⇒ Object
Constructor Details
#initialize(max_timeout = 10, proxy = nil) ⇒ NativeSender
Returns a new instance of NativeSender.
7 8 9 10 |
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 7 def initialize(max_timeout = 10, proxy = nil) @max_timeout = max_timeout @proxy = proxy end |
Class Method Details
.build_request(smarty_request) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 31 def self.build_request(smarty_request) query = create_query(smarty_request) if smarty_request.payload.nil? request = Net::HTTP::Get.new(URI.parse("#{smarty_request.url_prefix}?#{query}")) else request = Net::HTTP::Post.new(URI.parse("#{smarty_request.url_prefix}?#{query}")) end request.content_type = 'application/json' request.body = smarty_request.payload request['User-Agent'] = "smartystreets (sdk:ruby@#{SmartyStreets::VERSION})" request['Referer'] = smarty_request.referer unless smarty_request.referer.nil? set_custom_headers(smarty_request.headers, request) request end |
.create_query(smarty_request) ⇒ Object
65 66 67 |
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 65 def self.create_query(smarty_request) URI.encode_www_form(smarty_request.parameters) end |
.set_custom_headers(smarty_headers, request) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 69 def self.set_custom_headers(smarty_headers, request) smarty_headers.each do |key, values| values.each do |value| request.add_field(key, value) end end end |
Instance Method Details
#build_http(request) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 52 def build_http(request) uri = request.uri if @proxy.nil? http = Net::HTTP.new(uri.hostname, uri.port) else http = Net::HTTP.new(uri.hostname, uri.port, @proxy.host, @proxy.port, @proxy.username, @proxy.password) end http end |
#build_smarty_response(native_response) ⇒ Object
48 49 50 |
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 48 def build_smarty_response(native_response) Response.new(native_response.body, native_response.code) end |
#send(smarty_request) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 12 def send(smarty_request) request = self.class.build_request(smarty_request) begin http = build_http(request) http.use_ssl = true http.ssl_version = :TLSv1_2 http.read_timeout = @max_timeout response = http.request(request) http.finish if http.started? rescue StandardError => err return Response.new(nil, nil, err) end build_smarty_response(response) end |