From dcc4af7c8f5dabb4594871a1fae419353936985c Mon Sep 17 00:00:00 2001 From: Mariusz Date: Sat, 1 Feb 2014 23:28:54 +0800 Subject: [PATCH 1/5] get rid of some unnecessary syntax --- lib/doge/api.rb | 18 +++++++++--------- lib/doge/chain.rb | 9 ++++----- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/doge/api.rb b/lib/doge/api.rb index 55acbef..90e0939 100644 --- a/lib/doge/api.rb +++ b/lib/doge/api.rb @@ -5,23 +5,23 @@ module DogeApi class DogeApi attr_accessor :api_key - def initialize(api_key) - @api_key = api_key - @base_uri = URI.parse('https://www.dogeapi.com/wow') + 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) + def method_missing m, args = {}, &block fetch_uri build_uri(m, args) end end diff --git a/lib/doge/chain.rb b/lib/doge/chain.rb index 0c5ef61..eb3b58a 100644 --- a/lib/doge/chain.rb +++ b/lib/doge/chain.rb @@ -7,17 +7,16 @@ def initialize @base_uri = 'http://dogechain.info/chain/Dogecoin/q' end - def build_uri(m, params) + 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) + def method_missing m, *args, &block fetch_uri build_uri(m, args) end end From 4135591118e1bdddab2c525a993a70e771bdcf82 Mon Sep 17 00:00:00 2001 From: Mariusz Date: Sun, 2 Feb 2014 00:21:22 +0800 Subject: [PATCH 2/5] proper parsing of the api outputs, also: the base uri should probably be a constant --- lib/doge/api.rb | 27 ++++++++++++++++++++++++--- lib/doge/chain.rb | 24 +++++++++++++++++++----- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/doge/api.rb b/lib/doge/api.rb index 90e0939..099b531 100644 --- a/lib/doge/api.rb +++ b/lib/doge/api.rb @@ -5,9 +5,19 @@ module DogeApi class DogeApi attr_accessor :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' + @api_key = api_key end def build_uri m, args @@ -22,7 +32,18 @@ def fetch_uri uri end def method_missing m, args = {}, &block - fetch_uri build_uri(m, args) + 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 eb3b58a..5ba5386 100644 --- a/lib/doge/chain.rb +++ b/lib/doge/chain.rb @@ -1,15 +1,25 @@ require 'uri' require 'open-uri' +require 'json' module DogeApi class DogeChain - def initialize - @base_uri = 'http://dogechain.info/chain/Dogecoin/q' - end + + 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.parse "#{@base_uri}/#{m}/#{list}" + URI.parse "#{BASE_URI}/#{m}/#{list}" end def fetch_uri uri @@ -17,7 +27,11 @@ def fetch_uri uri end def method_missing m, *args, &block - fetch_uri build_uri(m, args) + 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 From 4422601b9bd4da3b09140878acf2dfa9b3a16645 Mon Sep 17 00:00:00 2001 From: Mariusz Date: Sun, 2 Feb 2014 00:28:10 +0800 Subject: [PATCH 3/5] changed readme --- README.md | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index c32a219..9285073 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,9 @@ -# doge_api +# doge_papi ## Description -doge_api is a minimal Ruby wrapper for DogeAPI. +tekknolagi/doge_api is a minimal Ruby wrapper for DogeAPI. +
+snicky/doge_papi takes care of parsing the DogeAPI and DogeChain outputs into proper Ruby objects (integers, floats, arrays, etc.) ## 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 +Just wait until tekknolagi decides to merge this into doge_api gem. \ No newline at end of file From d0bb9901b67df1405216c0e9821e99962e9ebd35 Mon Sep 17 00:00:00 2001 From: Mariusz Date: Sun, 2 Feb 2014 00:59:50 +0800 Subject: [PATCH 4/5] require json in both files --- lib/doge/api.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/doge/api.rb b/lib/doge/api.rb index 099b531..5739900 100644 --- a/lib/doge/api.rb +++ b/lib/doge/api.rb @@ -1,5 +1,6 @@ require 'uri' require 'open-uri' +require 'json' module DogeApi class DogeApi From bb98d8d05145e0844ef1ead644212baa1be03842 Mon Sep 17 00:00:00 2001 From: Mariusz Date: Sun, 2 Feb 2014 22:07:17 +0800 Subject: [PATCH 5/5] update and fixes --- README.md | 16 +++------------- doge_api.gemspec | 11 ----------- lib/doge/api.rb | 8 ++------ lib/doge/chain.rb | 6 +----- lib/doge_api.rb | 2 -- lib/doge_papi.rb | 6 ++++++ test.rb | 4 ++-- 7 files changed, 14 insertions(+), 39 deletions(-) delete mode 100644 doge_api.gemspec delete mode 100644 lib/doge_api.rb create mode 100644 lib/doge_papi.rb diff --git a/README.md b/README.md index c32a219..dfda539 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, 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 5739900..22fdf21 100644 --- a/lib/doge/api.rb +++ b/lib/doge/api.rb @@ -1,8 +1,4 @@ -require 'uri' -require 'open-uri' -require 'json' - -module DogeApi +module DogePapi class DogeApi attr_accessor :api_key @@ -22,7 +18,7 @@ def initialize api_key end def build_uri m, args - uri = @base_uri + uri = BASE_URI params = args.merge a: m, api_key: @api_key uri.query = URI.encode_www_form params uri diff --git a/lib/doge/chain.rb b/lib/doge/chain.rb index 5ba5386..e3b3e7c 100644 --- a/lib/doge/chain.rb +++ b/lib/doge/chain.rb @@ -1,8 +1,4 @@ -require 'uri' -require 'open-uri' -require 'json' - -module DogeApi +module DogePapi class DogeChain BASE_URI = 'http://dogechain.info/chain/Dogecoin/q' 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