class Guh::Base
The base class which all the specific wrapper classes inherit from. It provides some shared code to make development easier.
Public Class Methods
configure() { |self| ... }
click to toggle source
Configuration DSL helper method.
The default values are:
-
guh_ip_address
: 127.0.0.1 -
guh_port
: 1234
Example:
Guh::Base.configure do |config| config.guh_ip_address = 10.0.0.1 config.guh_port = 6789 end
# File lib/guh/base.rb, line 85 def self.configure(&block) yield self end
get(request_hash)
click to toggle source
Don’t use this unless you know what you are doing!
Send a request to guh Core and fetch the Response. This is a utility method used by the subclasses.
# File lib/guh/base.rb, line 53 def self.get(request_hash) request_string = hash_to_json(request_hash) response = nil client do |c| c.puts(request_string) response = fetch_message(c) end if response['status'] == 'success' return response['params'] else raise Guh::ResponseError, "The Request was not successful: #{response['error']}" end end
introspect()
click to toggle source
Returns everything that is going on inside guh Core
Example:
Guh::Base.introspect
# File lib/guh/base.rb, line 33 def self.introspect get({ id: generate_request_id, method: "JSONRPC.Introspect", }) end
version()
click to toggle source
# File lib/guh/base.rb, line 40 def self.version get({ id: generate_request_id, method: "JSONRPC.Version", }) end
Private Class Methods
client() { |client| ... }
click to toggle source
# File lib/guh/base.rb, line 95 def self.client(&block) client = TCPSocket.open(@@guh_ip_address, @@guh_port) connection_message = fetch_message(client) # TODO check guh-core version and raise error if incompatible with this gem's version # TODO look for timeout or connection refused errors yield client end
convert_map_to_list_of_maps(map)
click to toggle source
Converts a regular hash into a list of hashes.
Example:
power: true, pin: 1, family_code: 'A'
})
Returns:
[
{power: true}, {pin: 1}, {family_code: 'A'}
]
# File lib/guh/base.rb, line 141 def self.convert_map_to_list_of_maps(map) # make sure we have an array to work with map ||= [] # do the conversion work list = [] map.each do |key, value| list << {key => value} end return list end
fetch_message(client)
click to toggle source
# File lib/guh/base.rb, line 106 def self.fetch_message(client) end_of_message = false message = "" while (line = client.gets) message << line end_of_message = true if line.match(/^\}\n/) break if end_of_message end return JSON::parse(message) end
generate_request_id()
click to toggle source
# File lib/guh/base.rb, line 91 def self.generate_request_id 999999 end
hash_to_json(hash)
click to toggle source
# File lib/guh/base.rb, line 154 def self.hash_to_json(hash) JSON::dump(hash) + "\n" end