Module: RockautoApi::Endpoints::PartCategories

Included in:
Client
Defined in:
lib/rockauto_api/endpoints/part_categories.rb

Instance Method Summary collapse

Instance Method Details

#get_part_categories(make, year, model, carcode) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rockauto_api/endpoints/part_categories.rb', line 6

def get_part_categories(make, year, model, carcode)
  payload = navnode_fetch_payload(
    make: make,
    nodetype: "model",
    label: model,
    href: "#{Client::BASE_URL}/en/catalog/#{make.downcase},#{year},#{model.downcase}",
    year: year,
    model: model,
    carcode: carcode
  )

  response = call_catalog_api("navnode_fetch", payload)
  html = response.dig("html_fill_sections", "navchildren[]") || ""

  categories = parse_categories_from_html(html)

  Models::VehiclePartCategories.new(
    make: make,
    year: year,
    model: model,
    carcode: carcode,
    categories: categories,
    count: categories.size
  )
end

#get_parts_by_category(make, year, model, carcode, category_group_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rockauto_api/endpoints/part_categories.rb', line 32

def get_parts_by_category(make, year, model, carcode, category_group_name)
  categories_result = get_part_categories(make, year, model, carcode)
  category = categories_result.categories.find { |c| c.group_name == category_group_name }

  return Models::VehiclePartsResult.new(
    make: make, year: year, model: model, carcode: carcode,
    category: category_group_name, parts: [], count: 0
  ) unless category&.href

  html = get(category.href)
  doc = Nokogiri::HTML(html)
  parts = parse_parts_from_table(doc)

  Models::VehiclePartsResult.new(
    make: make, year: year, model: model, carcode: carcode,
    category: category_group_name, parts: parts, count: parts.size
  )
end