lib/mcoin/market/base.rb in mcoin-0.5.2 vs lib/mcoin/market/base.rb in mcoin-0.6.0

- old
+ new

@@ -1,41 +1,66 @@ # frozen_string_literal: true require 'pp' +require 'set' require 'json' +require 'bigdecimal' require 'net/http' module Mcoin module Market # :nodoc: class Base - def initialize(type, currency) - @type = type - @currency = currency + attr_reader :results + + def initialize + @pairs = Set.new + @results = [] @retries = 0 end + def watch(type, currency) + @pairs.add({ type: type.to_s.upcase, currency: currency.to_s.upcase }) + end + def name self.class.name.split('::').last end def fetch - @data ||= JSON.parse(Net::HTTP.get(uri)) + @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 nil if @retries >= 3 + return self if @retries >= 3 @retries += 1 retry end def to_ticker - raise NotImplementedError + @results.map do |pair, response| + build_ticker(pair, response) + end end - def uri - options = { type: @type.upcase, currency: @currency.upcase } - uri = format(self.class.const_get(:ENDPOINT), options) - URI(uri) + 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 + + private + + def build_ticker(_pair, _response) + raise NotImplementedError end end end end