class OpalcoinClient::Client
Attributes
options[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/opalcoin_client/client.rb, line 16 def initialize(options = {}) @options = get_defaults.merge(options) end
Public Instance Methods
http_post_request(post_body)
click to toggle source
# File lib/opalcoin_client/client.rb, line 32 def http_post_request(post_body) url = URI.parse "#{@options[:protocol]}://#{@options[:user]}:#{@options[:password]}@#{@options[:host]}:#{@options[:port]}/" http = Net::HTTP.new(url.host, url.port) http.use_ssl = (url.scheme == 'https') request = Net::HTTP::Post.new(url.path) request.basic_auth url.user, url.password request.content_type = 'application/json' request.body = post_body response = http.request(request) return response if response.class == Net::HTTPOK or response.class == Net::HTTPInternalServerError raise OpalcoinClient::HTTPError.new(response) end
method_missing(name, *args)
click to toggle source
# File lib/opalcoin_client/client.rb, line 25 def method_missing(name, *args) raise OpalcoinClient::InvalidMethodError.new(name) unless OpalcoinClient::METHODS.include?(name.to_s) response = http_post_request( get_post_body(name, args) ) get_response_data(response) end
valid?()
click to toggle source
# File lib/opalcoin_client/client.rb, line 20 def valid? post_body = { method: 'getinfo', id: Time.now.to_i }.to_json http_post_request(post_body).class == Net::HTTPOK rescue false end
Private Instance Methods
de_ruby_style(method_name)
click to toggle source
# File lib/opalcoin_client/client.rb, line 61 def de_ruby_style(method_name) method_name.to_s.tr('_', '').downcase.to_sym end
get_defaults()
click to toggle source
# File lib/opalcoin_client/client.rb, line 65 def get_defaults OpalcoinClient.configuration.instance_variables.each.inject({}) {|hash, var| hash[var.to_s.delete('@').to_sym] = OpalcoinClient.configuration.instance_variable_get(var); hash } end
get_post_body(name, args)
click to toggle source
# File lib/opalcoin_client/client.rb, line 51 def get_post_body(name, args) { method: de_ruby_style(name), params: args, id: Time.now.to_i }.to_json end
get_response_data(http_ok_response)
click to toggle source
# File lib/opalcoin_client/client.rb, line 55 def get_response_data(http_ok_response) resp = JSON.parse( http_ok_response.body ) raise OpalcoinClient::RPCError.new(resp['error']['message']) if resp['error'] and http_ok_response.class == Net::HTTPInternalServerError resp['result'] end