module StaticiceSearch
Fetch and parse staticICE (staticice.com.au) hardware price comparison results. Pure parsing/formatting only — HTTP is injected via a fetch lambda for tests.
Constants
- BASE_URL
- DEFAULT_LIMIT
- SORT_POSITION
-
spos is staticICE’s “sort position” hint; 3 keeps the cheapest-first ordering.
- USER_AGENT
-
staticICE serves empty results to obvious bot user-agents; use a plain browser UA.
Public Instance Methods
Source
# File lib/staticice_search.rb, line 20 def build_url(query, spos: SORT_POSITION) "#{BASE_URL}?q=#{CGI.escape(query.to_s.strip)}&spos=#{spos}" end
Source
# File lib/staticice_search.rb, line 88 def cheapest(html, limit: DEFAULT_LIMIT) parse_results(html).sort_by { |result| result[:price] }.first(limit) end
Source
# File lib/staticice_search.rb, line 38 def decode_redirect(href) href = href.to_s.strip return href if href.empty? query = URI.parse(href).query return href unless query new_url = URI.decode_www_form(query).to_h["newurl"] new_url && !new_url.empty? ? new_url : href rescue URI::InvalidURIError href end
Source
# File lib/staticice_search.rb, line 82 def extract_description(detail_cell) node = detail_cell.dup node.css("font").remove node.text.to_s.gsub(/\s+/, " ").strip end
Source
# File lib/staticice_search.rb, line 77 def extract_store(detail_cell) store = detail_cell.at_css('font[color="#0080CC"]')&.text.to_s.strip store.empty? ? "Unknown store" : store end
Source
# File lib/staticice_search.rb, line 99 def fetch_html(url) RabbotHttp.fetch(url, user_agent: USER_AGENT) end
Source
# File lib/staticice_search.rb, line 34 def format_price(value) format("$%.2f", value.to_f) end
Source
# File lib/staticice_search.rb, line 24 def parse_price(text) cleaned = text.to_s.gsub(/[^0-9.]/, "") return nil if cleaned.empty? value = Float(cleaned) value.positive? ? value : nil rescue ArgumentError nil end
Source
# File lib/staticice_search.rb, line 51 def parse_results(html) doc = Nokogiri::HTML(html.to_s) doc.css('tr[valign="top"]').filter_map { |row| parse_row(row) } rescue StandardError [] end
Source
# File lib/staticice_search.rb, line 58 def parse_row(row) price_link = row.at_css('td a[href*="redirect.cgi"]') return nil unless price_link price = parse_price(price_link.text) return nil unless price cells = row.css("td") detail_cell = cells[1] return nil unless detail_cell { price: price, store: extract_store(detail_cell), description: extract_description(detail_cell), url: decode_redirect(price_link["href"]) } end
Source
# File lib/staticice_search.rb, line 92 def search(query, fetch:, limit: DEFAULT_LIMIT) html = fetch.call(build_url(query)) cheapest(html, limit: limit) rescue StandardError [] end