Class: 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 = 10000) ⇒ NativeSender

Returns a new instance of NativeSender.



6
7
8
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 6

def initialize(max_timeout=10000)
  @max_timeout = max_timeout
end

Class Method Details

.build_request(smarty_request) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 30

def self.build_request(smarty_request)
  query = create_query(smarty_request)
  request = Net::HTTP::Post.new(URI.parse("#{smarty_request.url_prefix}?#{query}"))
  request.content_type = 'application/json'
  request.body = smarty_request.payload
  request['User-Agent'] = "smartystreets (sdk:ruby@#{SmartystreetsRubySdk::VERSION})"
  request['Referer'] = smarty_request.referer if smarty_request.referer != nil
  set_custom_headers(smarty_request.headers, request)
  request
end

.create_query(smarty_request) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 45

def self.create_query(smarty_request)
  query_string = ''

  smarty_request.parameters.each do |key, value|
    query_string.concat("&#{key}=#{value}")
  end

  query_string[0] = ''
  query_string
end

.set_custom_headers(smarty_headers, request) ⇒ Object



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

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_smarty_response(native_response) ⇒ Object



41
42
43
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 41

def build_smarty_response(native_response)
  Response.new(native_response.body, native_response.code)
end

#send(smarty_request) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/smartystreets_ruby_sdk/native_sender.rb', line 10

def send(smarty_request)
  request = self.class.build_request(smarty_request)
  uri = request.uri

  begin
    http = Net::HTTP.new(uri.hostname, uri.port)
    http.use_ssl = true
    http.ssl_version = :TLSv1_2
    http.read_timeout = @max_timeout

    response = http.request(request)

    http.finish if http.started?
  rescue Exception => err
    return Response.new(nil, nil, err)
  end

  build_smarty_response(response)
end