Class: SmartyStreets::NativeSender

Inherits:
Object
  • Object
show all
Defined in:
lib/smartystreets_ruby_sdk/native_sender.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_timeout = 10, proxy = nil, debug = false) ⇒ NativeSender

Returns a new instance of NativeSender.



7
8
9
10
11
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 7

def initialize(max_timeout = 10, proxy = nil, debug = false)
  @max_timeout = max_timeout
  @proxy = proxy
  @debug = debug
end

Class Method Details

.build_request(smarty_request) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 37

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.header, request)
  request
end

.create_query(smarty_request) ⇒ Object



77
78
79
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 77

def self.create_query(smarty_request)
  URI.encode_www_form(smarty_request.parameters)
end

.set_custom_headers(smarty_header, request) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 81

def self.set_custom_headers(smarty_header, request)
  smarty_header.each do |key, values|
    if values.respond_to? :each
      values.each do |value|
        request.add_field(key, value)
      end
    else
      request.add_field(key, values)
    end
  end
end

Instance Method Details

#build_http(request) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 62

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.set_debug_output($stdout) if @debug

  http
end

#build_smarty_response(native_response) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 54

def build_smarty_response(native_response)
  if native_response.header.nil?
    Response.new(native_response.body, native_response.code)
  else
    Response.new(native_response.body, native_response.code, native_response.header)
  end
end

#send(smarty_request) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 13

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.open_timeout = @max_timeout
    http.read_timeout = @max_timeout

    response = http.request(request)

    http.finish if http.started?
  rescue StandardError => err
    if response.nil?
      return Response.new(nil, nil, nil, err)
    else
      return Response.new(nil, nil, response.header, err)
    end
  end

  build_smarty_response(response)
end