Class: MercadoBitcoin::BaseApiCall

Inherits:
Object
  • Object
show all
Defined in:
lib/mercado_bitcoin/base_api_call.rb

Direct Known Subclasses

OrderBook, Ticker, Trade

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#coinObject

Returns the value of attribute coin.



3
4
5
# File 'lib/mercado_bitcoin/base_api_call.rb', line 3

def coin
  @coin
end

#parsedObject

Returns the value of attribute parsed.



3
4
5
# File 'lib/mercado_bitcoin/base_api_call.rb', line 3

def parsed
  @parsed
end

#parserObject

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_classObject

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

#responseObject (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_clientObject

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

#actionObject



25
26
27
# File 'lib/mercado_bitcoin/base_api_call.rb', line 25

def action
  ''
end

#base_urlObject



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

Returns:

  • (Boolean)


33
34
35
# File 'lib/mercado_bitcoin/base_api_call.rb', line 33

def bitcoin?
  coin == :bitcoin
end

#fetchObject



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

Returns:

  • (Boolean)


37
38
39
# File 'lib/mercado_bitcoin/base_api_call.rb', line 37

def litecoin?
  coin == :litecoin
end

#modelObject

Raises:

  • (NotImplementedError)


21
22
23
# File 'lib/mercado_bitcoin/base_api_call.rb', line 21

def model
  raise NotImplementedError.new
end

#paramsObject



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

#urlObject



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

Returns:

  • (Boolean)


12
13
14
# File 'lib/mercado_bitcoin/base_api_call.rb', line 12

def valid_coin?
  bitcoin? || litecoin?
end