Sha256: 1f42c59623753f534753f9d20ec2ab7f378fd04d9ec5d33510eedbb188f1fb0d

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

module AmazonOrder
  module Parsers
    class Base
      attr_accessor :fetched_at

      def initialize(node, options = {})
        @node = node
        @fetched_at = options[:fetched_at]
      end

      def inspect
        "#<#{self.class.name}:#{self.object_id} #{self.to_hash}>"
      end

      def to_hash
        hash = {}
        self.class::ATTRIBUTES.each do |f|
          hash[f] = send(f)
        end
        yield(hash) if block_given?
        hash
      end

      def values
        self.class::ATTRIBUTES.map{|a| send(a) }
      end

      def parse_date(date_text)
        begin
          Date.parse(date_text)
        rescue ArgumentError => e
          m = date_text.match(/\A(?<year>\d{4})年(?<month>\d{1,2})月(?<day>\d{1,2})日\z/)
          Date.new(m[:year].to_i, m[:month].to_i, m[:day].to_i)
        end
      end

      def get_original_image_url(url)
        parts = url.split('/')
        (parts[0..-2] + [parts.last.split('.').values_at(0,-1).join('.')]).join('/')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
amazon_order-0.1.2 lib/amazon_order/parsers/base.rb
amazon_order-0.1.1 lib/amazon_order/parsers/base.rb
amazon_order-0.1.0 lib/amazon_order/parsers/base.rb