Class: DogecoinClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dogecoin_client/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
# File 'lib/dogecoin_client/client.rb', line 16

def initialize(options = {})
  @options = get_defaults.merge(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



25
26
27
28
29
30
# 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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/dogecoin_client/client.rb', line 14

def options
  @options
end

Instance Method Details

#http_post_request(post_body) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# 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

#valid?Boolean

Returns:

  • (Boolean)


20
21
22
23
# 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