class BinaryEdge::Client::Base
Constants
- BASE_URL
- CONVERT_TABLE
- HOST
- VERSION
Attributes
api_key[R]
Public Class Methods
new(api_key)
click to toggle source
# File lib/binaryedge/clients/base.rb, line 16 def initialize(api_key) @api_key = api_key end
Private Instance Methods
_get(path, params = {}, &block)
click to toggle source
# File lib/binaryedge/clients/base.rb, line 79 def _get(path, params = {}, &block) request = build_request(type: "GET", path: path, params: params) make_request(request, &block) end
_post(path, params = {}, &block)
click to toggle source
# File lib/binaryedge/clients/base.rb, line 84 def _post(path, params = {}, &block) request = build_request(type: "POST", path: path, params: params) make_request(request, &block) end
base_path()
click to toggle source
# File lib/binaryedge/clients/base.rb, line 102 def base_path name = CONVERT_TABLE.fetch(klass) ["query", name].compact.join("/") end
build_request(type: "GET", path:, params: {})
click to toggle source
# File lib/binaryedge/clients/base.rb, line 62 def build_request(type: "GET", path:, params: {}) uri = url_for(path) uri.query = URI.encode_www_form(params) if type == "GET" request = case type when "GET" Net::HTTP::Get.new(uri, default_headers) when "POST" Net::HTTP::Post.new(uri, default_headers) else raise ArgumentError, "#{type} HTTP method is not supported" end request.body = JSON.generate(params) unless type == "GET" request end
default_headers()
click to toggle source
# File lib/binaryedge/clients/base.rb, line 40 def default_headers { "X-Key": api_key } end
https_options()
click to toggle source
# File lib/binaryedge/clients/base.rb, line 26 def https_options if proxy = ENV["HTTPS_PROXY"] || ENV["https_proxy"] uri = URI(proxy) { proxy_address: uri.hostname, proxy_port: uri.port, proxy_from_env: false, use_ssl: true } else { use_ssl: true } end end
klass()
click to toggle source
# File lib/binaryedge/clients/base.rb, line 98 def klass self.class.to_s.split("::").last.to_s.downcase.to_sym end
make_request(req) { |json| ... }
click to toggle source
# File lib/binaryedge/clients/base.rb, line 44 def make_request(req) Net::HTTP.start(HOST, 443, https_options) do |http| response = http.request(req) code = response.code.to_i body = response.body json = JSON.parse(body) case code when 200 yield json else error = json.dig("message") || body raise Error, "Unsupported response code returned: #{code} - #{error}" end end end
url_for(path)
click to toggle source
# File lib/binaryedge/clients/base.rb, line 22 def url_for(path) URI(BASE_URL + path) end