diff --git a/README.md b/README.md index c32a219..d987d80 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,13 @@ -# doge_api +# doge_papi ## Description -doge_api is a minimal Ruby wrapper for DogeAPI. +doge_papi is a minimal Ruby wrapper for DogeAPI and DogeChain that takes care of parsing the DogeAPI and DogeChain outputs into proper Ruby objects (integers, floats, arrays, etc.), a fork of even more minimal [tekknolagi/doge_api](https://github.com/tekknolagi/doge_api) ## Installation Just run `gem install doge_api` and you're all set. -If you want to use this as part of a package, just add: -
-gem 'doge_api'
-
-to your Gemfile. - ## Usage Check `test.rb` for some examples. ## License -This teeny-tiny project is under the MIT license. - -## Badges and things -[![Gem Version](https://badge.fury.io/rb/doge_api.png)](http://badge.fury.io/rb/doge_api) -[![Google Analytics](https://ga-beacon.appspot.com/UA-47678422-2/tekknolagi/doge_api)](https://ga-beacon.appspot.com/) \ No newline at end of file +This project is under the MIT license. \ No newline at end of file diff --git a/doge_api.gemspec b/doge_api.gemspec deleted file mode 100644 index 954eba8..0000000 --- a/doge_api.gemspec +++ /dev/null @@ -1,11 +0,0 @@ -Gem::Specification.new do |s| - s.name = 'doge_api' - s.version = '0.0.5' - s.date = '2014-01-26' - s.summary = 'Ruby wrapper for DogeAPI.com and DogeChain.info' - s.authors = ['Maxwell Bernstein'] - s.email = 'dogeapi@bernsteinbear.com' - s.files = ['lib/doge_api.rb', 'lib/doge/api.rb', 'lib/doge/chain.rb'] - s.homepage = 'http://github.com/tekknolagi/doge_api' - s.license = 'MIT' -end diff --git a/lib/doge/api.rb b/lib/doge/api.rb index 55acbef..22fdf21 100644 --- a/lib/doge/api.rb +++ b/lib/doge/api.rb @@ -1,28 +1,46 @@ -require 'uri' -require 'open-uri' - -module DogeApi +module DogePapi class DogeApi attr_accessor :api_key - def initialize(api_key) + 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] + + def initialize api_key @api_key = api_key - @base_uri = URI.parse('https://www.dogeapi.com/wow') end - def build_uri(m, args) - uri = @base_uri - params = args.merge(:a => m, :api_key => @api_key) - uri.query = URI.encode_www_form(params) + 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 - def fetch_uri(uri) + def fetch_uri uri uri.open.read end - def method_missing(m, args = {}, &block) - fetch_uri build_uri(m, args) + 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 end end diff --git a/lib/doge/chain.rb b/lib/doge/chain.rb index 0c5ef61..e3b3e7c 100644 --- a/lib/doge/chain.rb +++ b/lib/doge/chain.rb @@ -1,24 +1,33 @@ -require 'uri' -require 'open-uri' - -module DogeApi +module DogePapi class DogeChain - def initialize - @base_uri = 'http://dogechain.info/chain/Dogecoin/q' - end - def build_uri(m, params) + BASE_URI = 'http://dogechain.info/chain/Dogecoin/q' + + CALLS_RETURNING_INTEGER = [:getblockcount] + CALLS_RETURNING_FLOAT = [ + :addressbalance , + :getdifficulty , + :getreceivedbyaddress , + :getsentbyaddress , + :totalbc + ] + CALLS_RETURNING_JSON = [:transactions] + + def build_uri m, params list = params.join '/' - uri = URI.parse("#{@base_uri}/#{m}/#{list}") - uri + URI.parse "#{BASE_URI}/#{m}/#{list}" end - def fetch_uri(uri) + def fetch_uri uri uri.open.read end - def method_missing(m, *args, &block) - fetch_uri build_uri(m, args) + def method_missing m, *args, &block + output = fetch_uri build_uri(m, args) + output = output.to_i if CALLS_RETURNING_INTEGER.include? m + output = output.to_f if CALLS_RETURNING_FLOAT.include? m + output = JSON.parse(output) if CALLS_RETURNING_JSON.include? m + output end end end diff --git a/lib/doge_api.rb b/lib/doge_api.rb deleted file mode 100644 index f831b3b..0000000 --- a/lib/doge_api.rb +++ /dev/null @@ -1,2 +0,0 @@ -require_relative 'doge/api' -require_relative 'doge/chain' diff --git a/lib/doge_papi.rb b/lib/doge_papi.rb new file mode 100644 index 0000000..ae3f340 --- /dev/null +++ b/lib/doge_papi.rb @@ -0,0 +1,6 @@ +require 'uri' +require 'open-uri' +require 'json' + +require_relative 'doge/api' +require_relative 'doge/chain' \ No newline at end of file diff --git a/test.rb b/test.rb index 23b87f9..979c901 100644 --- a/test.rb +++ b/test.rb @@ -4,7 +4,7 @@ $my_address = 'DBv97MSG4gNjcGoHCMX1nfanLjzBgVERi9' $my_api_address = 'DPf8M6McXUZW5zDxwAKXAfid4A78P8o21M' -doge_api = DogeApi::DogeApi.new($my_api_key) +doge_api = DogePapi::DogeApi.new($my_api_key) doge_api.get_balance doge_api.withdraw :amount => 5, :payment_address => $my_address doge_api.get_new_address :address_label => 'my pretty address' @@ -15,7 +15,7 @@ doge_api.get_current_block doge_api.get_current_price -doge_chain = DogeApi::DogeChain.new +doge_chain = DogePapi::DogeChain.new doge_chain.addressbalance $my_address doge_chain.addresstohash $my_address doge_chain.checkaddress $my_address