Sha256: 8c3206c3e8c8b7cd155001cd53d8333296e8c1395125c5e17d82c339b64079c1

Contents?: true

Size: 1.53 KB

Versions: 17

Compression:

Stored size: 1.53 KB

Contents

require 'rest_client'

module Biro
  module CredDefense
    class Request < BaseRequest

      def production_url
        'https://www.creddefense.com/index.php/api/v2'
      end

      def development_url
        'https://test.creddefense.com/index.php/api/v2'
      end

      def required_params
        [:username, :password]
      end

      def find(document)
        params = {
          authentication: { token: token },
          advancedsearch: { identifier_code: document }
        }

        begin
          response = RestClient.post(search_path, params.to_json, headers)
          Response.new(response)
        rescue => e
          Biro.log(:warn, "Unable to process CredDefense request")
          raise "Error at CredDefense request: #{e.message}"
        end
      end

      private

      def login_path
        url + '/login'
      end

      def search_path
        url + '/advancedsearch'
      end

      def token
        @token ||= get_token
      end

      def get_token
        auth = { authentication: { username: @username, password: @password } }
        response = JSON.parse(RestClient.post(login_path, auth.to_json, headers).body)

        raise AuthenticationError.new('Invalid authentication credentials') unless success?(response)

        response.dig('authentication', 'token')
      end

      def success?(response)
        response['result'] == 'success'
      end

      def headers
        {
          'Accept' => 'application/json',
          'Content-Type' => 'application/json'
        }
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
biro-0.7.1 lib/biro/gateways/cred_defense/request.rb
biro-0.7.0 lib/biro/gateways/cred_defense/request.rb
biro-0.6.10 lib/biro/gateways/cred_defense/request.rb
biro-0.6.9 lib/biro/gateways/cred_defense/request.rb
biro-0.6.8 lib/biro/gateways/cred_defense/request.rb
biro-0.6.7 lib/biro/gateways/cred_defense/request.rb
biro-0.6.6 lib/biro/gateways/cred_defense/request.rb
biro-0.6.5 lib/biro/gateways/cred_defense/request.rb
biro-0.6.4 lib/biro/gateways/cred_defense/request.rb
biro-0.6.3 lib/biro/gateways/cred_defense/request.rb
biro-0.6.2 lib/biro/gateways/cred_defense/request.rb
biro-0.6.1 lib/biro/gateways/cred_defense/request.rb
biro-0.5.1 lib/biro/gateways/cred_defense/request.rb
biro-0.4.1 lib/biro/gateways/cred_defense/request.rb
biro-0.4.0 lib/biro/gateways/cred_defense/request.rb
biro-0.3.0 lib/biro/gateways/cred_defense/request.rb
biro-0.2.0 lib/biro/gateways/cred_defense/request.rb