Class: PackwizardParser::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/packwizard_parser/parser.rb

Overview

Parser for extracting data from PackWizard JSON

Parser fetches data from the PackWizard API by shareable ID or URL Accepts full URL or just the ID. In case of full URL insert, we extract the ID and add it to the API_URL. Delegate parsing to ListParser, CategoryParser, and ItemParser

Constant Summary collapse

API_URL =
'https://www.packwizard.com/api/packs/getSharedPackWithId'

Instance Method Summary collapse

Constructor Details

#initialize(shareable_id:) ⇒ Parser

Returns a new instance of Parser.



14
15
16
17
18
19
20
# File 'lib/packwizard_parser/parser.rb', line 14

def initialize(shareable_id:)
  @shareable_id = extract_id(shareable_id)

  @item_parser = ItemParser.new
  @category_parser = CategoryParser.new
  @list_parser = ListParser.new
end

Instance Method Details

#parseObject



22
23
24
25
26
27
28
# File 'lib/packwizard_parser/parser.rb', line 22

def parse
  response = HTTParty.get(API_URL, query: { shareableId: @shareable_id }, timeout: 30)
  raise "Failed to fetch #{response.code}" unless response.success?

  data = JSON.parse(response.body)
  @list_parser.parse(data, category_parser: @category_parser, item_parser: @item_parser)
end