Class: Bitflyer::HTTP::Authentication

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/bitflyer/http.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, key, secret) ⇒ Authentication

Returns a new instance of Authentication.



26
27
28
29
30
# File 'lib/bitflyer/http.rb', line 26

def initialize(app, key, secret)
  super(app)
  @key = key
  @secret = secret
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bitflyer/http.rb', line 32

def call(env)
  timestamp = Time.now.to_i.to_s
  method = env[:method].to_s.upcase
  path = env[:url].path + (env[:url].query ? '?' + env[:url].query : '')
  body = env[:body] || ''
  signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @secret, timestamp +  method + path + body)
  env[:request_headers]['ACCESS-KEY'] = @key if @key
  env[:request_headers]['ACCESS-TIMESTAMP'] = timestamp
  env[:request_headers]['ACCESS-SIGN'] = signature
  @app.call env
end