class Buckaruby::Request
Base class for any request.
Public Class Methods
new(config)
click to toggle source
# File lib/buckaruby/request.rb, line 13 def initialize(config) @config = config @logger = config.logger end
Public Instance Methods
build_request_params(_options)
click to toggle source
# File lib/buckaruby/request.rb, line 29 def build_request_params(_options) raise NotImplementedError end
execute(options)
click to toggle source
# File lib/buckaruby/request.rb, line 18 def execute(options) uri = URI.parse(@config.api_url) uri.query = "op=#{options[:operation]}" if options[:operation] response = post_buckaroo(uri, build_request_data(options)) # @logger.debug("[execute] response: #{response.inspect}") response end
Private Instance Methods
build_additional_params(options)
click to toggle source
# File lib/buckaruby/request.rb, line 76 def build_additional_params(options) options.map { |key, value| [:"add_#{key}", value] }.to_h end
build_custom_params(options)
click to toggle source
# File lib/buckaruby/request.rb, line 72 def build_custom_params(options) options.map { |key, value| [:"cust_#{key}", value] }.to_h end
build_request_data(options)
click to toggle source
# File lib/buckaruby/request.rb, line 57 def build_request_data(options) params = { brq_websitekey: @config.website } params.merge!(build_request_params(options)) params.merge!(build_custom_params(options[:custom])) if options[:custom] params.merge!(build_additional_params(options[:additional])) if options[:additional] params[:add_buckaruby] = "Buckaruby #{Buckaruby::VERSION}" # Sign the data with our secret key. params[:brq_signature] = Signature.generate_signature(params, @config) params end
post_buckaroo(uri, params)
click to toggle source
# File lib/buckaruby/request.rb, line 35 def post_buckaroo(uri, params) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == "https" http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end raw_response = http.post(uri.request_uri, post_data(params)) unless raw_response.is_a?(Net::HTTPSuccess) raise InvalidResponseException, raw_response end raw_response.body # Try to catch some common exceptions Net::HTTP might raise rescue Errno::ETIMEDOUT, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, IOError, SocketError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::OpenTimeout, Net::ProtocolError, Net::ReadTimeout, OpenSSL::SSL::SSLError => e raise ConnectionException, e end
post_data(params)
click to toggle source
# File lib/buckaruby/request.rb, line 80 def post_data(params) params.map { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join("&") end