Class: Cosmos::JsonApiObject
Overview
Used to forward all method calls to the remote server object. Before using this class ensure the remote service has been started in the server class:
json = JsonDrb.new
json.start_service('127.0.0.1', 7777, self)
Now the JsonApiObject can be used to call server methods directly:
server = JsonApiObject('http://cosmos-cmd-tlm-api:2901', 1.0)
server.cmd(*args)
Direct Known Subclasses
Constant Summary collapse
- USER_AGENT =
'Cosmos / v5 (ruby/cosmos/lib/io/json_api_object)'.freeze
Instance Attribute Summary collapse
-
#request_data ⇒ Object
readonly
Returns the value of attribute request_data.
-
#response_data ⇒ Object
readonly
Returns the value of attribute response_data.
Instance Method Summary collapse
-
#disconnect ⇒ Object
Disconnects from http server.
-
#initialize(url: ENV['COSMOS_API_URL'], timeout: 1.0, authentication: nil) ⇒ JsonApiObject
constructor
A new instance of JsonApiObject.
-
#request(*method_params, **keyword_params) ⇒ Object
Forwards all method calls to the remote service.
-
#shutdown ⇒ Object
Permanently disconnects from the http server.
Constructor Details
#initialize(url: ENV['COSMOS_API_URL'], timeout: 1.0, authentication: nil) ⇒ JsonApiObject
Returns a new instance of JsonApiObject.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cosmos/io/json_api_object.rb', line 57 def initialize(url: ENV['COSMOS_API_URL'], timeout: 1.0, authentication: nil) @http = nil @mutex = Mutex.new @request_data = "" @response_data = "" @url = url @log = [nil, nil, nil] @authentication = authentication.nil? ? CosmosAuthentication.new() : authentication @timeout = timeout @shutdown = false end |
Instance Attribute Details
#request_data ⇒ Object (readonly)
Returns the value of attribute request_data.
49 50 51 |
# File 'lib/cosmos/io/json_api_object.rb', line 49 def request_data @request_data end |
#response_data ⇒ Object (readonly)
Returns the value of attribute response_data.
50 51 52 |
# File 'lib/cosmos/io/json_api_object.rb', line 50 def response_data @response_data end |
Instance Method Details
#disconnect ⇒ Object
Disconnects from http server
92 93 94 95 |
# File 'lib/cosmos/io/json_api_object.rb', line 92 def disconnect @http.reset_all() if @http @http = nil end |
#request(*method_params, **keyword_params) ⇒ Object
Forwards all method calls to the remote service.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cosmos/io/json_api_object.rb', line 74 def request(*method_params, **keyword_params) raise JsonApiError, "Shutdown" if @shutdown method = method_params[0] endpoint = method_params[1] @mutex.synchronize do kwargs = _generate_kwargs(keyword_params) for attempt in 1..3 @log = [nil, nil, nil] connect() if !@http response = _send_request(method: method, endpoint: endpoint, kwargs: kwargs) return response unless response.code >= 500 sleep attempt end return nil end end |
#shutdown ⇒ Object
Permanently disconnects from the http server
98 99 100 101 |
# File 'lib/cosmos/io/json_api_object.rb', line 98 def shutdown @shutdown = true disconnect() end |