Class: Nanook::Rpc

Inherits:
Object
  • Object
show all
Defined in:
lib/nanook/rpc.rb

Constant Summary collapse

DEFAULT_URI =
"http://localhost:7076"
DEFAULT_TIMEOUT =

seconds

500

Instance Method Summary collapse

Constructor Details

#initialize(uri = DEFAULT_URI, timeout: DEFAULT_TIMEOUT) ⇒ Rpc

Returns a new instance of Rpc.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/nanook/rpc.rb', line 10

def initialize(uri=DEFAULT_URI, timeout:DEFAULT_TIMEOUT)
  rpc_server = URI(uri)

  unless ['http', 'https'].include?(rpc_server.scheme)
    raise ArgumentError.new("URI must have http or https in it. Was given: #{uri}")
  end

  @http = Net::HTTP.new(rpc_server.host, rpc_server.port)
  @http.read_timeout = timeout
  @request = Net::HTTP::Post.new(rpc_server.request_uri, {"user-agent" => "Ruby nanook gem"})
  @request.content_type = "application/json"
end

Instance Method Details

#call(action, params = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nanook/rpc.rb', line 23

def call(action, params={})
  # Stringify param values
  params = Hash[params.map {|k, v| [k, v.to_s] }]

  @request.body = { action: action }.merge(params).to_json

  response = @http.request(@request)

  if response.is_a?(Net::HTTPSuccess)
    hash = JSON.parse(response.body)
    process_hash(hash)
  else
    raise Nanook::Error.new("Encountered net/http error #{response.code}: #{response.class.name}")
  end
end

#inspectObject

:nodoc:



39
40
41
# File 'lib/nanook/rpc.rb', line 39

def inspect # :nodoc:
  "#{self.class.name}(host: #{@http.address}, timeout: #{@http.read_timeout} object_id: \"#{"0x00%x" % (object_id << 1)}\")"
end