Sha256: 1584296600532d8372aa5b5feb2de9be5a61b2ce97b28fc6244921a262636467

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require "json"
require "net/https"
require "uri"

module Spyse
  module Client
    class Base
      HOST = "api.spyse.com"
      VERSION = "v3"
      BASE_URL = "https://#{HOST}/#{VERSION}/data"

      def initialize(api_key)
        @api_key = api_key
      end

      private

      def url_for(path)
        URI(BASE_URL + path)
      end

      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

      def request(req)
        Net::HTTP.start(HOST, 443, https_options) do |http|
          req["Authorization"] = "Bearer #{@api_key}"

          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("error", "message") || body
            raise Error, "Unsupported response code returned: #{code} - #{error}"
          end
        end
      end

      def _get(path, params = {}, &block)
        uri = url_for(path)
        uri.query = URI.encode_www_form(params)
        get = Net::HTTP::Get.new(uri)

        request(get, &block)
      end

      def _post(path, params = {}, &block)
        post = Net::HTTP::Post.new(url_for(path))
        post.body = JSON.generate(params)
        post["Content-Type"] = "application/json"

        request(post, &block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spysex-0.1.0 lib/spyse/clients/base.rb