Created
April 2, 2017 00:43
-
-
Save skord/35bd2f58487410bd7c4945f4ce928e9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Mettlr | |
class Connection | |
def self.connection | |
@connection = Faraday.new('http://api.mettl.com/v1') do |c| | |
c.options.params_encoder = Faraday::FlatParamsEncoder | |
c.request :url_encoded | |
# c.request :json | |
c.response :mashify | |
c.response :json, content_type: /\bjson$/ | |
c.response :logger | |
c.use :instrumentation | |
c.adapter :net_http | |
end | |
end | |
def self.get(path) | |
connection.get do |req| | |
req.path = path | |
req.params['ak'] = Mettlr::METTL_PUBLIC_KEY | |
req.params['ts'] = Time.now.to_i.to_s | |
req.params = Hash[req.params.sort_by {|k,v| k.downcase}] | |
req.params['asgn'] = Mettlr::Signature.new(req).signature | |
end | |
end | |
def self.post(path, options) | |
connection.post do |req| | |
req.path = path | |
req.params['ak'] = Mettlr::METTL_PUBLIC_KEY | |
req.params['ts'] = Time.now.to_i.to_s | |
options.each do |k,v| | |
if v.is_a?(Hash) | |
req.params[k] = v.to_json | |
else | |
req.params[k] = v | |
end | |
end | |
req.params = Hash[req.params.sort_by {|k,v| k.downcase}] | |
req.params['asgn'] = Mettlr::Signature.new(req).signature | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Mettlr | |
class Signature | |
def initialize(request) | |
@request = request | |
end | |
def concat_string | |
@request.method.to_s.upcase + | |
"http://api.mettl.com" + | |
@request.path + | |
param_values | |
end | |
def param_values | |
"\n" + @request.params.values.join("\n") | |
end | |
def signature | |
hmac = OpenSSL::HMAC.digest('sha1', Mettlr::METTL_PRIVATE_KEY, concat_string) | |
Faraday::Utils.escape(Base64.encode64(hmac).chomp) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment