Sha256: f19be7c7c372f68cedc210c6bbb9bfd296a050f07d06225bc7e55940401a79ab

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'json'
require 'httparty'
require 'active_support/hash_with_indifferent_access'
require 'active_support/core_ext/hash'
require 'thread'

require 'kraken-io/http_multi_part'
require 'kraken-io/response'

module Kraken
  class API
    include HTTParty
    extend  HTTPMultiPart

    base_uri 'https://api.kraken.io/v1'

    attr_accessor :api_key, :api_secret

    def initialize(options = {})
      @api_key    = options.fetch(:api_key)
      @api_secret = options.fetch(:api_secret)
    end

    def async
      @async = true
      self
    end

    def sync
      @async = false
      self
    end

    def url(url, params = {})
      params = normalized_params(params).merge!(auth_hash)
      params[:url] = url
      call_kraken do
        res = self.class.post('/url', body: params.to_json)
        res = Kraken::Response.new(res)
        yield res if block_given? or return res
      end
    end

    def callback_url(url)
      @callback_url = url
      self
    end

    def upload(file_name, params = {})
      params = normalized_params(params).merge!(auth_hash)
      call_kraken do
        res = self.class.multipart_post('/upload', file: file_name, body: params.to_json)
        res = Kraken::Response.new(res)
        yield res if block_given? or return res
      end
    end

    private
    def call_kraken(&block)
      if @async
        call_async(&block)
      else
        yield
      end
    end

    def call_async(&block)
      Thread.abort_on_exception = false
      Thread.new do |t|
        block.call
      end
      nil
    end

    def normalized_params(params)
      params = params.with_indifferent_access

      if params.key?(:callback_url) || @callback_url
        params[:callback_url] = params['callback_url'] || @callback_url
      else
        params[:wait] = true
      end

      params
    end

    def auth_hash
      {
        auth: {
          api_key: api_key,
          api_secret: api_secret
        }
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kraken-io-0.1.2 lib/kraken-io.rb