Class: RMC::Connection
- Inherits:
-
Object
- Object
- RMC::Connection
- Defined in:
- lib/rmc/connection.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#version ⇒ Object
writeonly
Sets the attribute version.
Instance Method Summary collapse
-
#initialize(url, username, password, verify_ssl = true, version = 1) ⇒ Connection
constructor
A new instance of Connection.
- #request(params) ⇒ Object
- #wait_for_task(id) ⇒ Object
Constructor Details
#initialize(url, username, password, verify_ssl = true, version = 1) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rmc/connection.rb', line 11 def initialize(url, username, password, verify_ssl = true, version = 1) @url = "#{url}/rest/rm-central/" @version = version @verify_ssl = verify_ssl @logger = Logger.new('/dev/null') Oj. = { :mode => :compat } create_token(username, password) end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
9 10 11 |
# File 'lib/rmc/connection.rb', line 9 def logger @logger end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
6 7 8 |
# File 'lib/rmc/connection.rb', line 6 def token @token end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
5 6 7 |
# File 'lib/rmc/connection.rb', line 5 def url @url end |
#version=(value) ⇒ Object (writeonly)
Sets the attribute version
7 8 9 |
# File 'lib/rmc/connection.rb', line 7 def version=(value) @version = value end |
Instance Method Details
#request(params) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rmc/connection.rb', line 23 def request(params) data = { method: :get, timeout: 10, verify_ssl: @verify_ssl, }.merge(params) data[:url] = "#{@url}v#{@version}#{data[:url]}" unless data.has_key?(:headers) data[:headers] = {} end data[:headers] = { 'Content-Type' => 'application/json', 'Accept' => 'application/json', } # If payload provided, encode it if data[:payload] data[:payload] = Oj.dump(data[:payload]) end if @token data[:headers]['X-Auth-Token'] = @token end @logger.info("RMC: Request: #{data}") begin result = Oj.safe_load(RestClient::Request.execute(data)) @logger.info("RMC: Result: #{result}") result rescue RestClient::Exception => e if e.http_code == 401 raise RMC::LoginException, e.http_body end if e.http_code == 404 raise RMC::NotFoundException, e.http_body end raise RMC::Exception, e.http_body rescue Oj::Error => e raise RMC::Exception, e.to_s end end |
#wait_for_task(id) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rmc/connection.rb', line 72 def wait_for_task(id) @version = 2 begin Timeout.timeout(1800) do while true begin result = request( :url => "/tasks/#{id}" )['task'] break rescue RMC::NotFoundException @logger.info("RMC: Task #{id} not found, waiting...") sleep(5) end end state = result['taskState'] while state == 'Running' sleep(1) @logger.debug("RMC: Checking Task #{id} State...") result = request( :url => "/tasks/#{id}" )['task'] state = result['taskState'] end if state == 'Completed' @logger.debug("RMC: Task #{id} completed...") return result end raise RMC::Exception, "Task #{id} failed, got state #{state}" end rescue TimeoutError raise RMC::Exception, "Task #{id} timeout" ensure @version = 1 end end |