Sha256: 80c605526aa05f6de1c7faf9c9df40353dfd12232b895691dff765ff39116f1e

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require 'cgi'
require 'json'
require 'net/http'
require 'uri'

module Xolphin
  module Api
    class Http
      LIVE_URL = "https://api.xolphin.com/v1/".freeze
      TEST_URL = "https://test-api.xolphin.com/v1/".freeze

      def initialize(username, password, options = {})
        @username = username
        @password = password
        @test = options[:test]
      end

      def get(path, params = {})
        uri = URI.parse(File.join(api_url, path))
        uri.query = URI.encode_www_form(params) unless params.empty?
        pkgver = Xolphin::Api::VERSION

        request = Net::HTTP::Get.new(uri, {'User-Agent' => "xolphin-api-ruby/#{pkgver}"})
        request.basic_auth(@username, @password)

        response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
          http.request(request)
        end

        JSON.parse(response.body)
      end

      def post(path, params = {})
        uri = URI.parse(File.join(api_url, path))
        pkgver = Xolphin::Api::VERSION

        request = Net::HTTP::Post.new(uri, {'User-Agent' => "xolphin-api-ruby/#{pkgver}"})
        request.basic_auth(@username, @password)
        request.set_form_data(params)

        response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
          http.request(request)
        end

        JSON.parse(response.body)
      end

      def download(path, params = {})
        uri = URI.parse(File.join(api_url, path))
        uri.query = URI.encode_www_form(params) unless params.empty?
        pkgver = Xolphin::Api::VERSION

        request = Net::HTTP::Get.new(uri, {'User-Agent' => "xolphin-api-ruby/#{pkgver}"})
        request.basic_auth(@username, @password)

        response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
          http.request(request)
        end

        response.body
      end

      private

      def api_url
        @test ? TEST_URL : LIVE_URL
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xolphin-api-1.9.0 lib/xolphin/api/http.rb