class RubyPaypalNvp::Fetcher::Base

Public Class Methods

call(options) click to toggle source
# File lib/ruby_paypal_nvp/fetcher/base.rb, line 34
def self.call(options)
  new(options).call
end
new(opts) click to toggle source

Input options:

date_from
date_to
currency
subject
transaction_class

rubocop:disable Metrics/AbcSize

# File lib/ruby_paypal_nvp/fetcher/base.rb, line 17
def initialize(opts)
  @start_date = opts.fetch(:date_from, Time.zone.now).beginning_of_day.utc.iso8601
  @end_date = opts.fetch(:date_to, Time.zone.now).end_of_day.utc.iso8601
  @currency = opts.fetch(:currency, 'CZK')
  @subject = opts[:subject] || RubyPaypalNvp.configuration.subject
  @transaction_class = opts.fetch(:transaction_class, 'BalanceAffecting')
  @resulting_hash = default_hash
end

Public Instance Methods

call() click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/ruby_paypal_nvp/fetcher/base.rb, line 27
def call
  result = load_response
  process_loaded_data(result)
rescue NoMethodError
  raise "Error processing #{self.class} for #{@subject}"
end
load_api_response(options) click to toggle source
# File lib/ruby_paypal_nvp/fetcher/base.rb, line 38
def load_api_response(options)
  uri = URI(RubyPaypalNvp.configuration.api_url)
  req = Net::HTTP::Post.new(uri)
  req.set_form_data(options)
  res = Net::HTTP.start(uri.hostname, uri.port,
                        use_ssl: uri.scheme == 'https') do |http|
    http.open_timeout = 6000
    http.read_timeout = 6000
    http.request(req)
  end
  pretty_json(res.body)
end

Private Instance Methods

api_method() click to toggle source

Needs to be specific in each child

# File lib/ruby_paypal_nvp/fetcher/base.rb, line 101
def api_method
  ''
end
default_hash() click to toggle source
# File lib/ruby_paypal_nvp/fetcher/base.rb, line 109
def default_hash
  { meta: {}, values: {} }
end
parse(body, options = {}) click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

# File lib/ruby_paypal_nvp/fetcher/base.rb, line 55
def parse(body, options = {})
  @resulting_hash[:values].tap do |parsed_response|
    param_type = self.class::RESPONSE_PARAMS.first
    all_for_params_type = body.select { |k, _v| k.start_with?(param_type) }
    all_for_params_type.each_with_index do |(key, value), index|
      real_index = index + options[:increment]
      parsed_response[real_index] ||= {}
      parsed_response[real_index] = parsed_response[real_index]
                                    .merge(param_type.to_s => value)
      key = key.gsub(param_type, '')
      self.class::RESPONSE_PARAMS.drop(1).each do |attribute_name|
        pair = { attribute_name => body["#{attribute_name}#{key}"] }
        parsed_response[real_index] = parsed_response[real_index].merge pair
      end
    end
  end
end
pretty_json(body) click to toggle source
# File lib/ruby_paypal_nvp/fetcher/base.rb, line 105
def pretty_json(body)
  Hash[URI.decode_www_form(body)]
end
process_loaded_data(_result) click to toggle source

Implement in each child to save/send data

# File lib/ruby_paypal_nvp/fetcher/base.rb, line 76
def process_loaded_data(_result)
  true
end
request_options(_options = {}) click to toggle source
# File lib/ruby_paypal_nvp/fetcher/base.rb, line 89
def request_options(_options = {})
  {
    method: api_method,
    version: RubyPaypalNvp.configuration.version,
    user: RubyPaypalNvp.configuration.user,
    pwd: RubyPaypalNvp.configuration.password,
    signature: RubyPaypalNvp.configuration.signature,
    subject: @subject
  }
end
result_with_meta(options = {}) click to toggle source
# File lib/ruby_paypal_nvp/fetcher/base.rb, line 80
def result_with_meta(options = {})
  @resulting_hash[:meta]['timestamp'] = options[:timestamp]
  @resulting_hash[:meta]['start_date'] = @start_date
  @resulting_hash[:meta]['end_date'] = @end_date
  @resulting_hash[:meta]['subject'] = @subject
  @resulting_hash[:meta]['currency_code'] = @currency
  @resulting_hash
end