class DogecoinClient::Client

Attributes

options[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/dogecoin_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/dogecoin_client/client.rb, line 32
def http_post_request(post_body)
  req = Net::HTTP::Post.new(get_service_uri)
  req.basic_auth @options[:user], @options[:password]
  req.content_type = 'application/json'
  req.body = post_body

  response = Net::HTTP.start(@options[:host], @options[:port]) {|http| http.request(req) }

  return response if response.class == Net::HTTPOK or response.class == Net::HTTPInternalServerError
  raise DogecoinClient::HTTPError.new(response)
end
method_missing(name, *args) click to toggle source
# File lib/dogecoin_client/client.rb, line 25
def method_missing(name, *args)
  raise DogecoinClient::InvalidMethodError.new(name) unless DogecoinClient::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/dogecoin_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/dogecoin_client/client.rb, line 60
def de_ruby_style(method_name)
   method_name.to_s.tr('_', '').downcase.to_sym
end
get_defaults() click to toggle source
# File lib/dogecoin_client/client.rb, line 64
def get_defaults
  DogecoinClient.configuration.instance_variables.each.inject({}) {|hash, var|
    hash[var.to_s.delete('@').to_sym] = DogecoinClient.configuration.instance_variable_get(var);
    hash
  }
end
get_post_body(name, args) click to toggle source
# File lib/dogecoin_client/client.rb, line 50
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/dogecoin_client/client.rb, line 54
def get_response_data(http_ok_response)
  resp = JSON.parse( http_ok_response.body )
  raise DogecoinClient::RPCError.new(resp['error']['message']) if resp['error'] and http_ok_response.class == Net::HTTPInternalServerError
  resp['result']
end
get_service_uri() click to toggle source
# File lib/dogecoin_client/client.rb, line 46
def get_service_uri
  URI("#{@options[:protocol]}://#{@options[:host]}:#{@options[:port]}").request_uri
end