lib/wp/hmac/client.rb in wp-hmac-0.2.2 vs lib/wp/hmac/client.rb in wp-hmac-0.2.3
- old
+ new
@@ -1,24 +1,36 @@
module WP
module HMAC
+ # = HMAC Client
+ # This client uses EY::ApiHMAC to hash a request with a secret key in order
+ # to authenticate the client.
+ #
+ # See here for the implementation details:
+ # https://github.com/engineyard/ey_api_hmac
class Client
class UnsuccessfulResponse < StandardError; end
- def initialize(url = nil, app = Rack::Client::Handler::NetHTTP)
- build_rack_client(url, app)
+ @rack_app = Rack::Client::Handler::NetHTTP
+ # Enable injection of another Rack app for testing
+ class << self
+ attr_accessor :rack_app
end
- def build_rack_client(url, app)
+ def initialize(url = nil)
+ build_rack_client(url)
+ end
+
+ def build_rack_client(url)
id = key_cabinet.id
auth_key = key_cabinet.auth_key
@client = Rack::Client.new(url) do
use Rack::Config do |env|
env['HTTP_DATE'] = Time.now.httpdate
end
use EY::ApiHMAC::ApiAuth::Client, id, auth_key
- run app
+ run Client.rack_app
end
@client
end
def key_cabinet
@@ -29,10 +41,10 @@
# client = WP::HMAC::Client.new('https://www.example.com')
# client.get('api/staff')
%i(delete get head options post put patch).each do |method|
define_method(method) do |*args|
response = @client.send(method, *args)
- raise UnsuccessfulResponse unless /2\d\d/.match("#{response.status}")
+ fail UnsuccessfulResponse unless /2\d\d/.match("#{response.status}")
response
end
end
# Supports: