Class: DogePapi::DogeApi

Inherits:
Object
  • Object
show all
Defined in:
lib/doge/api.rb

Constant Summary collapse

BASE_URI =
URI.parse 'https://www.dogeapi.com/wow'
CALLS_RETURNING_INTEGER =
[:get_current_block]
CALLS_RETURNING_FLOAT =
[
  :get_balance          ,
  :get_address_received ,
  :get_difficulty       ,
  :get_current_price
]
CALLS_RETURNING_JSON =
[:get_my_addresses]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ DogeApi

Returns a new instance of DogeApi.



16
17
18
# File 'lib/doge/api.rb', line 16

def initialize api_key
  @api_key = api_key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, args = {}, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/doge/api.rb', line 31

def method_missing m, args = {}, &block
  output = fetch_uri build_uri(m, args)
  if CALLS_RETURNING_JSON.include? m
    output = JSON.parse output
  else
    # Doge API wraps everything in double quotes,
    # so we need to get rid of them first.
    #
    output.tr! '"', ''
    output = output.to_i if CALLS_RETURNING_INTEGER.include? m
    output = output.to_f if CALLS_RETURNING_FLOAT.include? m
  end
  output
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/doge/api.rb', line 3

def api_key
  @api_key
end

Instance Method Details

#build_uri(m, args) ⇒ Object



20
21
22
23
24
25
# File 'lib/doge/api.rb', line 20

def build_uri m, args
  uri       = @base_uri
  params    = args.merge a: m, api_key: @api_key
  uri.query = URI.encode_www_form params
  uri
end

#fetch_uri(uri) ⇒ Object



27
28
29
# File 'lib/doge/api.rb', line 27

def fetch_uri uri
  uri.open.read
end