Sha256: b2b291082c16619f337910cbe61693ac5b74a97484c0a05116a36cee482d53eb

Contents?: true

Size: 1.01 KB

Versions: 10

Compression:

Stored size: 1.01 KB

Contents

require 'base64'

module MtGox
  module Request
    def get(path, options={})
      request(:get, path, options)
    end

    def post(path, options={})
      request(:post, path, options)
    end

    private

    def request(method, path, options)
      response = connection.send(method) do |request|
        case method
        when :get
          request.url(path, options)
        when :post
          request.path = path
          request.body = body_from_options(options)
          request.headers = headers(request.body)
        end
      end
      response.body
    end

    def headers(request)
      signature = Base64.strict_encode64(
        OpenSSL::HMAC.digest 'sha512',
        Base64.decode64(MtGox.secret),
        request
      )
      {'Rest-Key' => MtGox.key, 'Rest-Sign' => signature}
    end

    def body_from_options(options)
      add_nonce(options).collect{|k, v| "#{k}=#{v}"} * '&'
    end

    def add_nonce(options)
      options.merge!({:nonce => (Time.now.to_f * 1000000).to_i})
    end
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
mtgox-0.8.0 lib/mtgox/request.rb
mtgox-0.7.5 lib/mtgox/request.rb
guten-mtgox-0.8.0 lib/mtgox/request.rb
mtgox-0.7.4 lib/mtgox/request.rb
mtgox-0.7.3 lib/mtgox/request.rb
mt_gox-0.7.7 lib/mtgox/request.rb
mt_gox-0.7.6 lib/mtgox/request.rb
mt_gox-0.7.5 lib/mtgox/request.rb
mt_gox-0.7.3 lib/mtgox/request.rb
mtgox-0.7.2 lib/mtgox/request.rb