Sha256: fd9f10a8ca8dddcf1a4685d90e0ed6a239278f0896ace7ff8ddecf036b767dfc

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

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

      @rack_app = Rack::Client::Handler::NetHTTP
      # Enable injection of another Rack app for testing
      class << self
        attr_accessor :rack_app
      end

      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 Client.rack_app
        end
        @client
      end

      def key_cabinet
        @key_cabinet ||= HMAC::KeyCabinet.find_by_auth_id(HMAC.auth_id)
      end

      # Supports:
      # 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)
          fail UnsuccessfulResponse unless /2\d\d/.match("#{response.status}")
          response
        end
      end

      # Supports:
      # WP::HMAC::Client.get('https://www.example.com/api/staff')
      class << self
        %i(delete get head options post put patch).each do |method|
          define_method(method) do |*args|
            client = Client.new
            client.send(method, *args)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wp-hmac-0.2.4 lib/wp/hmac/client.rb
wp-hmac-0.2.3 lib/wp/hmac/client.rb