lib/hamburglar/gateways/max_mind.rb in hamburglar-0.1.3 vs lib/hamburglar/gateways/max_mind.rb in hamburglar-0.1.4
- old
+ new
@@ -1,17 +1,59 @@
+require 'faraday_middleware/response_middleware'
+
module Hamburglar
module Gateways
# The MaxMind module contains classes for working
# with MaxMind's minFraud and Telephone Verification APIs
module MaxMind
+ class Base < ::Hamburglar::Gateways::Base
+ # Parses a MaxMind text response to a hash.
+ class ResponseParser < ::FaradayMiddleware::ResponseMiddleware
+ define_parser do |body|
+ unless body.strip.empty?
+ # MaxMind returns the response encoded with ISO-8859-1, so we normalize
+ # it to UTF-8.
+ # http://dev.maxmind.com/minfraud/#Output
+ body = body.force_encoding("ISO-8859-1").encode("UTF-8").split(';')
+
+ body.each_with_object({}) do |line, hash|
+ key, val = line.split('=')
+
+ if key.to_s != "" && val.to_s != ""
+ hash[key.to_sym] = val
+ end
+ end
+ end
+ end
+ end
+
+ Faraday.register_middleware :response,
+ maxmind: ::Hamburglar::Gateways::MaxMind::Base::ResponseParser
+
+ # Submit a request upstream to generate a fraud report
+ def submit
+ if valid?
+ @response = Faraday.new(:url => self.class::API_URL, ssl: {
+ verify: true, ca_file: File.expand_path('../../../cacert.pem', __FILE__)
+ }) do |c|
+ c.request :url_encoded
+ c.response :raise_error
+ c.response :follow_redirects, :limit => 10
+ c.response :maxmind
+ c.adapter :net_http
+ end.post(self.class::API_PATH, @params).body
+ end
+ end
+ end
+
# The MinFraud class handles fraud verification
# through MaxMind's minFraud API.
#
# See: http://www.maxmind.com/app/ccv
class MinFraud < Base
- # The MaxMind API URL
- set_api_url "https://minfraud2.maxmind.com/app/ccv2r"
+ API_URL = "https://minfraud2.maxmind.com"
+ API_PATH = "/app/ccv2r"
# Required parameters for a minFraud API call
set_required_params :i, :city, :region, :postal, :country, :license_key
def initialize(params = {})
@@ -23,20 +65,19 @@
# The TelephoneVerification class handles fraud verification
# through MaxMind's Telephone Verification API
#
# See: http://www.maxmind.com/app/telephone_api
class TelephoneVerification < Base
- # The MaxMind Telephone Verification API URL
- set_api_url "https://www.maxmind.com/app/telephone_http"
+ API_URL = "https://www.maxmind.com"
+ API_PATH = "/app/telephone_http"
# Required parameters for a Telephone Verification API call
set_required_params :l, :phone
def initialize(params = {})
super
self.params[:l] = self.params.delete(:license_key) if self.params[:license_key]
end
end
-
end
end
end