Class: RubyOmx::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_omx/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ruby_omx/connection.rb', line 7

def initialize(params = {})
  
  # These values are essential to establishing a connection
  @udi_auth_token   = params['udi_auth_token']
  @server           = (params['server'] || RubyOmx::DEFAULT_HOST) + "?UDIAuthToken=#{@udi_auth_token}"
  @http_biz_id 			= params['http_biz_id']
			@path = URI.parse(@server).request_uri

  raise MissingConnectionOptions if [@udi_auth_token, @http_biz_id].any? {|option| option.nil?}
  @http = connect
end

Class Method Details

.connect(options = {}) ⇒ Object



5
# File 'lib/ruby_omx/connection.rb', line 5

def self.connect(options = {}); new(options) end

Instance Method Details

#connectObject

Create the Net::HTTP object to use for this connection



20
21
22
23
24
25
26
# File 'lib/ruby_omx/connection.rb', line 20

def connect
  uri 							= URI.parse(@server)
  http             	= Net::HTTP.new(uri.host, uri.port)
  http.use_ssl  	  = true
  http.verify_mode 	= OpenSSL::SSL::VERIFY_NONE
  return http
end

#request(verb, body = nil, attempts = 0, &block) ⇒ Object

Make the request, based on the appropriate request object Called from RubyOmx::Base



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_omx/connection.rb', line 30

def request(verb, body = nil, attempts = 0, &block)
  request = Net::HTTP.const_get(verb.to_s.capitalize).new(@path, {'User-Agent' => 'ruby_omx ' + RubyOmx::VERSION})
  request.body = body
  request.content_length = request.body.size
			request.content_type = "text/xml; charset=utf-8"
  @http.request(request)
  
rescue Errno::EPIPE, Timeout::Error, Errno::EINVAL, EOFError
  @http = connect
  attempts == 3 ? raise : (attempts += 1; retry)
end