Class: Mcoin::Market::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mcoin/market/base.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



15
16
17
18
19
# File 'lib/mcoin/market/base.rb', line 15

def initialize
  @pairs = Set.new
  @results = []
  @retries = 0
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



13
14
15
# File 'lib/mcoin/market/base.rb', line 13

def results
  @results
end

Instance Method Details

#establish_connection(&block) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/mcoin/market/base.rb', line 51

def establish_connection(&block)
  @base_uri ||= URI(format(self.class.const_get(:ENDPOINT), type: '', currency: ''))
  use_ssl = @base_uri.scheme == 'https'
  Net::HTTP.start(@base_uri.host, @base_uri.port, use_ssl: use_ssl) do |http|
    yield http
  end
end

#fetchObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mcoin/market/base.rb', line 29

def fetch
  @results = []
  establish_connection do |http|
    @pairs.each do |pair|
      uri = URI(format(self.class.const_get(:ENDPOINT), pair))
      request = Net::HTTP::Get.new(uri)
      @results << [pair, JSON.parse(http.request(request)&.body)]
    end
  end
  self
rescue JSON::ParserError
  return self if @retries >= 3
  @retries += 1
  retry
end

#nameObject



25
26
27
# File 'lib/mcoin/market/base.rb', line 25

def name
  self.class.name.split('::').last
end

#to_tickerObject



45
46
47
48
49
# File 'lib/mcoin/market/base.rb', line 45

def to_ticker
  @results.map do |pair, response|
    build_ticker(pair, response)
  end
end

#watch(type, currency) ⇒ Object



21
22
23
# File 'lib/mcoin/market/base.rb', line 21

def watch(type, currency)
  @pairs.add({ type: type.to_s.upcase, currency: currency.to_s.upcase })
end