Class: CPS::Client

Inherits:
Object
  • Object
show all
Includes:
REXML
Defined in:
lib/cps-client/client.rb

Constant Summary collapse

DEFAULT_TIMEOUT =

The Integer maximum time to run a cps query, expressed in seconds.

10
CA_FILE =

crt file - deployed by puppet!

'/etc/ssl/certs/bundle.crt'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Initializes a new Cps::Client with options.

new(options = {})

Parameters

options

Hash of options: :timeout - The Integer script timeout, expressed in seconds (default: DEFAULT_TIMEOUT). :cid - Customer number :uid - User :pwd - Password

If block is given, yields self.

Returns

Cps::Client

The client instance.

Examples

client = Cps::Client.new do |c|
  c.timeout = nil
end
client.query("google.com")


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cps-client/client.rb', line 42

def initialize(options = {})
  @cid = options[:cid]
  @uid = options[:uid]
  @pwd = options[:pwd]
  
  @production = options[:production] == false ? false : true
  
  @timeout  = options[:timeout] || DEFAULT_TIMEOUT
  @request  = ""
  @response = ""
  @data     = ""
end

Instance Method Details

#dataObject



81
82
83
# File 'lib/cps-client/client.rb', line 81

def data
  @data.to_s
end

#entity(path) ⇒ Object



89
90
91
92
# File 'lib/cps-client/client.rb', line 89

def entity(path)
  d = Document.new(@data)
  d.root.elements[path].text rescue ""
end

#get_bindingObject



107
108
109
# File 'lib/cps-client/client.rb', line 107

def get_binding
  binding
end

#query(object) ⇒ Object

Returns

true: if the call was successful false: if the call failed



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cps-client/client.rb', line 63

def query( object )
  # AlwaysVerifySSLCertificates.ca_file = CA_FILE
  url = @production == true ? URL_PRD : URL_DEV
  uri = URI.parse("#{PROTO}://#{url}:#{PORT}")
  @request = self.template(object)
  
  begin
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true if PROTO == 'https'
    req = Net::HTTP::Post.new( PATH )
    @response, @data = http.request(req, @request)
    self.result_code >= '1000' && self.result_code <= '1003' ? true : false
  rescue # OpenSSL::SSL::SSLError
    false
  end
  
end

#requestObject



85
86
87
# File 'lib/cps-client/client.rb', line 85

def request
  @request.to_s
end

#result_codeObject



94
95
96
# File 'lib/cps-client/client.rb', line 94

def result_code
  self.entity('/response/result/code')
end

#result_messageObject



98
99
100
# File 'lib/cps-client/client.rb', line 98

def result_message
  self.entity('/response/result/message')
end

#template(xml) ⇒ Object



102
103
104
105
# File 'lib/cps-client/client.rb', line 102

def template(xml)
  @xml = xml
  ErbHelper.build("base", self)
end