amazon_order 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e865042b91b22077149316ff57d575168bc43635
4
- data.tar.gz: ca3967e1120cbcee525fa691d22cd7c9744bba51
3
+ metadata.gz: e80c9b230fb1e27f402524ea7f87efffb24e27c7
4
+ data.tar.gz: '09092bccb478e6cfb1a716f07150349cc401b283'
5
5
  SHA512:
6
- metadata.gz: 99679b709cfe850447a320f941550f0e7d88c23b16b9d44fd43d6df7984f6c76d80275f2278906c24a38fd8fdaf173ddbfaf9ecdbcabf58c4131cf573b00faaa
7
- data.tar.gz: 145c6fb0c6eb615b8d92e5c00f686cd7a8f6e869f209c821431504e6e9728edacbdbfe7d8fcfabed0163e379077717561884d804ff101268be208afb5fcfa41c
6
+ metadata.gz: b764bcb696977cbea344325de6839817e34189ee0a73ff6416d5851203b9c224e5276df54293be36944afe2f54c2672a074400bfda2b4cafe1762f92a5353bf3
7
+ data.tar.gz: 8b03502b90caf6d6e7f7689348fd4878a5bed2bea16363d4a783813e8280158607381f21c59e087915055c015360d9b25bb52c4e60217e4bcade73d2ad3a2e6d
@@ -65,8 +65,17 @@ module AmazonOrder
65
65
  end
66
66
 
67
67
  def go_to_amazon_order_page
68
+ if doc.css('.cvf-account-switcher').present?
69
+ log "Account switcher page was displayed"
70
+ session.first('.cvf-account-switcher-profile-details').click
71
+ wait_for_selector('#nav-main') # Wait for page loading
72
+ end
68
73
  link = links_for('a').find{|link| link =~ %r{/order-history} }
69
- session.visit link
74
+ if link.present?
75
+ session.visit link
76
+ else
77
+ log "Link for order history wasn't found in #{session.current_url}"
78
+ end
70
79
  end
71
80
 
72
81
  def fetch_orders_for_year(options = {})
@@ -6,6 +6,7 @@ module AmazonOrder
6
6
  def initialize(node, options = {})
7
7
  @node = node
8
8
  @fetched_at = options[:fetched_at]
9
+ @containing_object = options[:containing_object]
9
10
  end
10
11
 
11
12
  def inspect
@@ -3,7 +3,6 @@ module AmazonOrder
3
3
  class Order < Base
4
4
  ATTRIBUTES = %w[
5
5
  order_placed order_number order_total
6
- shipment_status shipment_note
7
6
  order_details_path
8
7
  all_products_displayed
9
8
  ]
@@ -20,33 +19,45 @@ module AmazonOrder
20
19
  @_order_total ||= @node.css('.order-info .a-col-left .a-column')[1].css('.value').text.strip.gsub(/[^\d\.]/, '').to_f
21
20
  end
22
21
 
23
- def shipment_status
24
- # class names like "shipment-is-delivered" in '.shipment' node may be useful
25
- @_shipment_status ||= @node.css('.shipment .shipment-top-row').present? ? @node.css('.shipment .shipment-top-row .a-row')[0].text.strip : nil
22
+ def order_details_path
23
+ @_order_details_path ||= @node.css('.order-info .a-col-right .a-row')[1].css('a.a-link-normal')[0].attr('href')
26
24
  end
27
25
 
28
- def shipment_note
29
- @_shipment_note ||= case order_type
30
- when :item_order
31
- @node.css('.shipment .shipment-top-row').present? ? @node.css('.shipment .shipment-top-row .a-row')[1].text.strip : nil
32
- when :service_order
33
- nil
26
+ def order_type
27
+ if @node.css('[id^=Leave-Service-Feedback]').present?
28
+ return :service_order
29
+ elsif @node.css('.shipment').present?
30
+ :shipment_order
31
+ else
32
+ :digital_order
34
33
  end
35
34
  end
36
35
 
36
+ def shipments
37
+ @_shipments ||= @node.css('.shipment')
38
+ .map do |shipment|
39
+ AmazonOrder::Parsers::Shipment.new(shipment,
40
+ containing_object: self,
41
+ fetched_at: fetched_at)
42
+ end
43
+ end
37
44
 
38
- def order_details_path
39
- @_order_details_path ||= @node.css('.order-info .a-col-right .a-row')[1].css('a.a-link-normal')[0].attr('href')
45
+ def products
46
+ @products ||= shipment_products + digital_products
40
47
  end
41
48
 
42
- def all_products_displayed
43
- @_all_products_displayed ||= @node.css('.a-box.order-info ~ .a-box .a-col-left .a-row').last.css('.a-link-emphasis').present?
49
+ def shipment_products
50
+ @shipment_products ||= shipments.flat_map(&:products)
44
51
  end
45
52
 
46
- def products
47
- @_products ||= @node.css('.a-box.order-info ~ .a-box .a-col-left .a-row')[0].css('.a-fixed-left-grid').map { |e| AmazonOrder::Parsers::Product.new(e, fetched_at: fetched_at) }
53
+ def digital_products
54
+ @_products ||= @node.css('.a-box:not(.shipment) .a-fixed-left-grid').map { |e| AmazonOrder::Parsers::Product.new(e, fetched_at: fetched_at) }
48
55
  end
49
56
 
57
+ # might be broken now that orders have multiple shipments
58
+ def all_products_displayed
59
+ @_all_products_displayed ||= @node.css('.a-box.order-info ~ .a-box .a-col-left .a-row').last.css('.a-link-emphasis').present?
60
+ end
50
61
 
51
62
  def to_hash
52
63
  super do |hash|
@@ -54,9 +65,6 @@ module AmazonOrder
54
65
  end
55
66
  end
56
67
 
57
- def order_type
58
- @node.css('[id^=Leave-Service-Feedback]').present? ? :service_order : :item_order
59
- end
60
68
  end
61
69
  end
62
70
  end
@@ -0,0 +1,38 @@
1
+ module AmazonOrder
2
+ module Parsers
3
+ class Shipment < Base
4
+ ATTRIBUTES = %w[
5
+ shipment_status
6
+ shipment_note
7
+ ]
8
+
9
+ # TODO shipment_date
10
+
11
+ def order
12
+ @containing_object
13
+ end
14
+
15
+ def shipment_status
16
+ # class names like "shipment-is-delivered" in '.shipment' node may be useful
17
+ @_shipment_status ||= @node.css('.shipment-top-row').present? ? @node.css('.shipment .shipment-top-row .a-row')[0].text.strip : nil
18
+ end
19
+
20
+ def shipment_note
21
+ @_shipment_note ||= case order.order_type
22
+ when :shipment_order
23
+ @node.css('.shipment-top-row').present? ? @node.css('.shipment .shipment-top-row .a-row')[1].text.strip : nil
24
+ when :service_order
25
+ nil
26
+ when :digital_order
27
+ nil
28
+ end
29
+ end
30
+
31
+
32
+ def products
33
+ @_products ||= @node.css('.a-fixed-left-grid').map { |e| AmazonOrder::Parsers::Product.new(e, fetched_at: fetched_at) }
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module AmazonOrder
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -9,7 +9,7 @@ module AmazonOrder
9
9
  data['orders']
10
10
  end
11
11
 
12
- def print_produts
12
+ def print_products
13
13
  data['products']
14
14
  end
15
15
 
@@ -21,7 +21,8 @@ module AmazonOrder
21
21
  csv_file = "#{@output_dir}/#{resource}#{Time.current.strftime('%Y%m%d%H%M%S')}.csv"
22
22
  puts " Writing #{csv_file}"
23
23
  CSV.open(csv_file, 'wb') do |csv|
24
- data[resource].each{|r| csv << r }
24
+ csv << attributes_for(resource)
25
+ data[resource].each { |r| csv << r }
25
26
  end
26
27
  csv_file
27
28
  end
@@ -41,5 +42,14 @@ module AmazonOrder
41
42
  data
42
43
  end
43
44
  end
45
+
46
+ def attributes_for(resource)
47
+ case resource
48
+ when 'orders'
49
+ AmazonOrder::Parsers::Order::ATTRIBUTES
50
+ when 'products'
51
+ AmazonOrder::Parsers::Product::ATTRIBUTES
52
+ end
53
+ end
44
54
  end
45
55
  end
data/lib/amazon_order.rb CHANGED
@@ -3,6 +3,7 @@ require "amazon_order/version"
3
3
  require "amazon_order/client"
4
4
  require "amazon_order/parsers/base"
5
5
  require "amazon_order/parsers/order"
6
+ require "amazon_order/parsers/shipment"
6
7
  require "amazon_order/parsers/product"
7
8
  require "amazon_order/parser"
8
9
  require "amazon_order/writer"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon_order
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kazuho Yamaguchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-30 00:00:00.000000000 Z
11
+ date: 2018-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: amazon_auth
@@ -131,6 +131,7 @@ files:
131
131
  - lib/amazon_order/parsers/base.rb
132
132
  - lib/amazon_order/parsers/order.rb
133
133
  - lib/amazon_order/parsers/product.rb
134
+ - lib/amazon_order/parsers/shipment.rb
134
135
  - lib/amazon_order/version.rb
135
136
  - lib/amazon_order/writer.rb
136
137
  homepage: https://github.com/kyamaguchi/amazon_order