rockauto_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RockautoApi
4
+ module Parsers
5
+ class FitmentParser
6
+ def self.parse(html, part_number:, brand:)
7
+ fitments = []
8
+
9
+ if html && !html.empty?
10
+ doc = Nokogiri::HTML(html)
11
+ doc.css("table tr").each do |row|
12
+ cells = row.css("td, th").map { |c| c.text.strip }
13
+ next if cells.size < 3
14
+ next if cells.first.match?(/\A\s*(?:Year|Make|Model)\s*\z/i)
15
+
16
+ year = cells[0].to_i
17
+ next if year.zero?
18
+
19
+ fitments << Models::FitmentInfo.new(
20
+ year: year,
21
+ make: cells[1] || "Unknown",
22
+ model: cells[2] || "Unknown",
23
+ engine: cells[3],
24
+ transmission: cells[4],
25
+ drivetrain: cells[5],
26
+ notes: cells[6]
27
+ )
28
+ end
29
+ end
30
+
31
+ Models::BuyersGuideResult.new(
32
+ part_number: part_number,
33
+ brand: brand,
34
+ fitments: fitments,
35
+ count: fitments.size
36
+ )
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RockautoApi
4
+ module Parsers
5
+ module HtmlHelpers
6
+ BASE_URL = "https://www.rockauto.com"
7
+
8
+ module_function
9
+
10
+ def select_options(html_or_doc, select_id_or_css)
11
+ doc = html_or_doc.is_a?(Nokogiri::HTML::Document) ? html_or_doc : Nokogiri::HTML(html_or_doc)
12
+ select = doc.at_css("select##{select_id_or_css}") || doc.at_css(select_id_or_css)
13
+ return [] unless select
14
+
15
+ select.css("option").map { |opt|
16
+ value = opt["value"]
17
+ text = opt.text.strip
18
+ next nil if value.nil? || value.empty? || text.empty?
19
+ { value: value, text: text }
20
+ }.compact
21
+ end
22
+
23
+ def extract_csrf_token(html_or_doc, name = "_nck")
24
+ doc = html_or_doc.is_a?(Nokogiri::HTML::Document) ? html_or_doc : Nokogiri::HTML(html_or_doc)
25
+ input = doc.at_css("input[name='#{name}']")
26
+ input&.attr("value")
27
+ end
28
+
29
+ def extract_javascript_variable(html, var_name)
30
+ html.match(/window\.#{Regexp.escape(var_name)}\s*=\s*"([^"]+)"/)&.captures&.first
31
+ end
32
+
33
+ def make_absolute_url(href)
34
+ return nil if href.nil? || href.empty?
35
+ return href if href.start_with?("http")
36
+ href.start_with?("/") ? "#{BASE_URL}#{href}" : "#{BASE_URL}/#{href}"
37
+ end
38
+
39
+ def table_rows(html_or_doc, min_cells: 2)
40
+ doc = html_or_doc.is_a?(Nokogiri::HTML::Document) ? html_or_doc : Nokogiri::HTML(html_or_doc)
41
+ doc.css("table tr").select { |row| row.css("td").size >= min_cells }.map { |row|
42
+ row.css("td, th").map { |cell| cell.text.strip }
43
+ }
44
+ end
45
+
46
+ def find_links(html_or_doc, pattern = nil)
47
+ doc = html_or_doc.is_a?(Nokogiri::HTML::Document) ? html_or_doc : Nokogiri::HTML(html_or_doc)
48
+ links = doc.css("a")
49
+ if pattern
50
+ links = links.select { |a| a.text.strip.match?(pattern) }
51
+ end
52
+ links.map { |a| { text: a.text.strip, href: make_absolute_url(a["href"]) } }
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RockautoApi
4
+ module Parsers
5
+ class OrderParser
6
+ def self.parse(html)
7
+ doc = Nokogiri::HTML(html)
8
+
9
+ {
10
+ order_number: extract_field(doc, /Order\s*#?/i),
11
+ order_date: extract_field(doc, /Date/i),
12
+ status: extract_field(doc, /Status/i),
13
+ items: parse_items(doc),
14
+ billing: parse_billing(doc),
15
+ shipping: parse_shipping(doc)
16
+ }
17
+ end
18
+
19
+ def self.extract_field(doc, pattern)
20
+ doc.css("td, th, div, span").each do |el|
21
+ if el.text.strip.match?(pattern)
22
+ next_el = el.next_element || el.parent&.next_element
23
+ return next_el.text.strip if next_el
24
+ end
25
+ end
26
+ nil
27
+ end
28
+
29
+ def self.parse_items(doc)
30
+ doc.css("table tr").map { |row|
31
+ cells = row.css("td").map { |c| c.text.strip }
32
+ next nil if cells.size < 3
33
+ Models::OrderItem.new(
34
+ part_number: cells[0] || "",
35
+ description: cells[1] || "",
36
+ brand: cells[2],
37
+ quantity: cells[3],
38
+ unit_price: cells[4],
39
+ total_price: cells[5],
40
+ status: cells[6],
41
+ tracking_number: cells[7]
42
+ )
43
+ }.compact
44
+ rescue StandardError
45
+ []
46
+ end
47
+
48
+ def self.parse_billing(doc)
49
+ Models::BillingInfo.new(
50
+ subtotal: extract_field(doc, /Subtotal/i),
51
+ shipping_cost: extract_field(doc, /Shipping/i),
52
+ tax: extract_field(doc, /Tax/i),
53
+ total: extract_field(doc, /Total/i)
54
+ )
55
+ end
56
+
57
+ def self.parse_shipping(doc)
58
+ Models::ShippingInfo.new(
59
+ method: extract_field(doc, /Method/i),
60
+ carrier: extract_field(doc, /Carrier/i),
61
+ tracking_number: extract_field(doc, /Tracking/i)
62
+ )
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RockautoApi
4
+ module Parsers
5
+ class PartExtractor
6
+ KNOWN_BRANDS = %w[
7
+ HONDA TOYOTA FORD CHEVROLET DODGE NISSAN BMW MERCEDES VOLKSWAGEN
8
+ SUBARU MAZDA HYUNDAI KIA AUDI LEXUS JEEP GMC RAM CHRYSLER
9
+ BOSCH DENSO NGK ACDELCO MOTORCRAFT DELPHI WALKER MONROE
10
+ GATES DAYCO CONTINENTAL TIMKEN SKF FEL-PRO MAHLE VICTOR
11
+ REINZ MOOG DORMAN CARDONE STANDARD BECK/ARNLEY MEVOTECH
12
+ ].freeze
13
+
14
+ PART_NUMBER_REGEX = /[A-Z0-9]{6,}/
15
+ PRICE_REGEX = /\$[\d,.]+/
16
+
17
+ def self.extract_from_row(row_node)
18
+ cells = row_node.css("td")
19
+ return nil if cells.empty?
20
+
21
+ texts = cells.map { |c| c.text.strip }.reject(&:empty?)
22
+
23
+ price = texts.find { |t| t.match?(PRICE_REGEX) }
24
+ part_number = texts.find { |t| t.match?(PART_NUMBER_REGEX) && !t.match?(PRICE_REGEX) }
25
+
26
+ brand = nil
27
+ KNOWN_BRANDS.each do |b|
28
+ if texts.any? { |t| t.upcase.include?(b) }
29
+ brand = b
30
+ break
31
+ end
32
+ end
33
+
34
+ name_candidates = texts.reject { |t|
35
+ t == price || t == part_number || (brand && t.upcase.include?(brand))
36
+ }
37
+ name = name_candidates.max_by(&:length) || texts.first || "Unknown"
38
+
39
+ links = row_node.css("a")
40
+ url = nil
41
+ image_url = nil
42
+ info_url = nil
43
+
44
+ links.each do |link|
45
+ href = link["href"]
46
+ next unless href
47
+ if href.include?("moreinfo")
48
+ info_url = Parsers::HtmlHelpers.make_absolute_url(href)
49
+ elsif href.include?("catalog") && !href.include?("moreinfo")
50
+ url = Parsers::HtmlHelpers.make_absolute_url(href)
51
+ end
52
+ end
53
+
54
+ img = row_node.at_css("img")
55
+ image_url = Parsers::HtmlHelpers.make_absolute_url(img["src"]) if img && img["src"]
56
+
57
+ Models::PartInfo.new(
58
+ name: clean_name(name, price, part_number, brand),
59
+ part_number: part_number || "Unknown",
60
+ brand: brand,
61
+ price: price,
62
+ url: url,
63
+ image_url: image_url,
64
+ info_url: info_url
65
+ )
66
+ rescue StandardError
67
+ nil
68
+ end
69
+
70
+ def self.clean_name(text, price, part_number, brand)
71
+ result = text.to_s.dup
72
+ result = result.gsub(price, "") if price
73
+ result = result.gsub(part_number, "") if part_number
74
+ result = result.gsub(/#{Regexp.escape(brand)}/i, "") if brand
75
+ result.gsub(/\s+/, " ").strip
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RockautoApi
4
+ class Railtie < ::Rails::Railtie
5
+ config.rockauto_api = RockautoApi::Configuration.new
6
+
7
+ initializer "rockauto_api.configure" do |app|
8
+ RockautoApi.configure do |config|
9
+ config.cache = Rails.cache
10
+ end
11
+ end
12
+
13
+ generators do
14
+ require "rails/generators"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RockautoApi
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "faraday-cookie_jar"
5
+ require "nokogiri"
6
+ require "dry-types"
7
+ require "dry-struct"
8
+ require "json"
9
+ require "cgi"
10
+
11
+ require_relative "rockauto_api/version"
12
+ require_relative "rockauto_api/configuration"
13
+ require_relative "rockauto_api/errors"
14
+ require_relative "rockauto_api/cache"
15
+ require_relative "rockauto_api/models/vehicle"
16
+ require_relative "rockauto_api/models/part"
17
+ require_relative "rockauto_api/models/fitment"
18
+ require_relative "rockauto_api/models/order"
19
+ require_relative "rockauto_api/models/account"
20
+ require_relative "rockauto_api/models/tool"
21
+ require_relative "rockauto_api/parsers/html_helpers"
22
+ require_relative "rockauto_api/parsers/part_extractor"
23
+ require_relative "rockauto_api/parsers/fitment_parser"
24
+ require_relative "rockauto_api/parsers/order_parser"
25
+ require_relative "rockauto_api/endpoints/vehicles"
26
+ require_relative "rockauto_api/endpoints/part_categories"
27
+ require_relative "rockauto_api/endpoints/part_search"
28
+ require_relative "rockauto_api/endpoints/fitment"
29
+ require_relative "rockauto_api/endpoints/tools"
30
+ require_relative "rockauto_api/endpoints/orders"
31
+ require_relative "rockauto_api/endpoints/account"
32
+ require_relative "rockauto_api/client"
33
+ require_relative "rockauto_api/railtie" if defined?(Rails)
34
+
35
+ module RockautoApi
36
+ class << self
37
+ attr_accessor :configuration
38
+
39
+ def configure
40
+ self.configuration ||= Configuration.new
41
+ yield(configuration) if block_given?
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rockauto_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben D'Angelo
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: faraday
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: faraday-cookie_jar
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: nokogiri
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.15'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.15'
54
+ - !ruby/object:Gem::Dependency
55
+ name: dry-types
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.7'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.7'
68
+ - !ruby/object:Gem::Dependency
69
+ name: dry-struct
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.6'
75
+ type: :runtime
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.6'
82
+ - !ruby/object:Gem::Dependency
83
+ name: irb
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 1.18.0
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 1.18.0
96
+ - !ruby/object:Gem::Dependency
97
+ name: rake
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '13.0'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '13.0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: webmock
112
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '3.0'
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '3.0'
124
+ - !ruby/object:Gem::Dependency
125
+ name: vcr
126
+ requirement: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '6.0'
131
+ type: :development
132
+ prerelease: false
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '6.0'
138
+ description: Browse vehicle catalogs, search parts by number, get vehicle fitments,
139
+ track orders, and manage RockAuto accounts. Designed for Rails integration.
140
+ executables: []
141
+ extensions: []
142
+ extra_rdoc_files: []
143
+ files:
144
+ - lib/rockauto_api.rb
145
+ - lib/rockauto_api/cache.rb
146
+ - lib/rockauto_api/client.rb
147
+ - lib/rockauto_api/configuration.rb
148
+ - lib/rockauto_api/endpoints/account.rb
149
+ - lib/rockauto_api/endpoints/fitment.rb
150
+ - lib/rockauto_api/endpoints/orders.rb
151
+ - lib/rockauto_api/endpoints/part_categories.rb
152
+ - lib/rockauto_api/endpoints/part_search.rb
153
+ - lib/rockauto_api/endpoints/tools.rb
154
+ - lib/rockauto_api/endpoints/vehicles.rb
155
+ - lib/rockauto_api/errors.rb
156
+ - lib/rockauto_api/models/account.rb
157
+ - lib/rockauto_api/models/fitment.rb
158
+ - lib/rockauto_api/models/order.rb
159
+ - lib/rockauto_api/models/part.rb
160
+ - lib/rockauto_api/models/tool.rb
161
+ - lib/rockauto_api/models/vehicle.rb
162
+ - lib/rockauto_api/parsers/fitment_parser.rb
163
+ - lib/rockauto_api/parsers/html_helpers.rb
164
+ - lib/rockauto_api/parsers/order_parser.rb
165
+ - lib/rockauto_api/parsers/part_extractor.rb
166
+ - lib/rockauto_api/railtie.rb
167
+ - lib/rockauto_api/version.rb
168
+ homepage: https://github.com/bendangelo/rockauto_api
169
+ licenses:
170
+ - MIT
171
+ metadata:
172
+ homepage_uri: https://github.com/bendangelo/rockauto_api
173
+ source_code_uri: https://github.com/bendangelo/rockauto_api
174
+ changelog_uri: https://github.com/bendangelo/rockauto_api/blob/master/CHANGELOG.md
175
+ rdoc_options: []
176
+ require_paths:
177
+ - lib
178
+ required_ruby_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '3.0'
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ requirements: []
189
+ rubygems_version: 4.0.10
190
+ specification_version: 4
191
+ summary: Comprehensive Ruby API client for RockAuto.com
192
+ test_files: []