Class: MercadoBitcoin::BaseApiCall
- Inherits:
-
Object
- Object
- MercadoBitcoin::BaseApiCall
- Defined in:
- lib/mercado_bitcoin/base_api_call.rb
Instance Attribute Summary collapse
-
#coin ⇒ Object
Returns the value of attribute coin.
-
#parsed ⇒ Object
Returns the value of attribute parsed.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#parser_error_class ⇒ Object
Returns the value of attribute parser_error_class.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#rest_client ⇒ Object
Returns the value of attribute rest_client.
Instance Method Summary collapse
- #action ⇒ Object
- #base_url ⇒ Object
- #bitcoin? ⇒ Boolean
- #fetch ⇒ Object
- #get(url) ⇒ Object
-
#initialize(coin = :bitcoin, opts = {}) ⇒ BaseApiCall
constructor
A new instance of BaseApiCall.
- #litecoin? ⇒ Boolean
- #model ⇒ Object
- #params ⇒ Object
- #parse(body) ⇒ Object
- #url ⇒ Object
- #valid_coin? ⇒ Boolean
Constructor Details
#initialize(coin = :bitcoin, opts = {}) ⇒ BaseApiCall
Returns a new instance of BaseApiCall.
4 5 6 7 8 9 10 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 4 def initialize(coin = :bitcoin, opts = {}) @coin = coin.to_s.downcase.to_sym raise MercadoBitcoin::CoinTypeError.new("bitcoin or litecoin expected #{coin} received") unless valid_coin? @parser = opts[:parser] || JSON @rest_client = opts[:rest_client] || RestClient @parser_error_class = opts[:parser_error_class] || JSON::ParserError end |
Instance Attribute Details
#coin ⇒ Object
Returns the value of attribute coin.
3 4 5 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 3 def coin @coin end |
#parsed ⇒ Object
Returns the value of attribute parsed.
3 4 5 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 3 def parsed @parsed end |
#parser ⇒ Object
Returns the value of attribute parser.
3 4 5 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 3 def parser @parser end |
#parser_error_class ⇒ Object
Returns the value of attribute parser_error_class.
3 4 5 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 3 def parser_error_class @parser_error_class end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
53 54 55 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 53 def response @response end |
#rest_client ⇒ Object
Returns the value of attribute rest_client.
3 4 5 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 3 def rest_client @rest_client end |
Instance Method Details
#action ⇒ Object
25 26 27 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 25 def action '' end |
#base_url ⇒ Object
29 30 31 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 29 def base_url @base_url ||= "https://www.mercadobitcoin.net/api".freeze end |
#bitcoin? ⇒ Boolean
33 34 35 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 33 def bitcoin? coin == :bitcoin end |
#fetch ⇒ Object
16 17 18 19 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 16 def fetch @parsed = parse(get(url)) model.new(parsed) end |
#get(url) ⇒ Object
55 56 57 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 55 def get(url) @response = rest_client.get(url) end |
#litecoin? ⇒ Boolean
37 38 39 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 37 def litecoin? coin == :litecoin end |
#model ⇒ Object
21 22 23 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 21 def model raise NotImplementedError.new end |
#params ⇒ Object
41 42 43 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 41 def params {} end |
#parse(body) ⇒ Object
59 60 61 62 63 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 59 def parse(body) parser.parse(body) rescue parser_error_class raise MercadoBitcoin::ParserError.new("#{url} responded an invalid json data") end |
#url ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 45 def url return @url if @url ac = action.is_a?(String) && action != '' ? File.join(base_url, action) : base_url @url = URI.parse(ac) @url.query = URI.encode_www_form(params) if !params.nil? && !params.empty? @url = @url.to_s end |
#valid_coin? ⇒ Boolean
12 13 14 |
# File 'lib/mercado_bitcoin/base_api_call.rb', line 12 def valid_coin? bitcoin? || litecoin? end |