Class: AmazonOrder::Parsers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon_order/parsers/base.rb

Direct Known Subclasses

Order, Product, Shipment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, options = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
# File 'lib/amazon_order/parsers/base.rb', line 6

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

Instance Attribute Details

#fetched_atObject

Returns the value of attribute fetched_at.



4
5
6
# File 'lib/amazon_order/parsers/base.rb', line 4

def fetched_at
  @fetched_at
end

Instance Method Details

#get_original_image_url(url) ⇒ Object



38
39
40
41
# File 'lib/amazon_order/parsers/base.rb', line 38

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

#inspectObject



12
13
14
# File 'lib/amazon_order/parsers/base.rb', line 12

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

#parse_date(date_text) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/amazon_order/parsers/base.rb', line 29

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

#to_hash {|hash| ... } ⇒ Object

Yields:

  • (hash)


16
17
18
19
20
21
22
23
# File 'lib/amazon_order/parsers/base.rb', line 16

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

#valuesObject



25
26
27
# File 'lib/amazon_order/parsers/base.rb', line 25

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