Class: Jekyll::Shopsavvy::PriceTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/shopsavvy/tags.rb

Overview

shopsavvy_price <identifier> % — emits a plain price string.

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ PriceTag

Returns a new instance of PriceTag.



143
144
145
146
147
# File 'lib/jekyll/shopsavvy/tags.rb', line 143

def initialize(tag_name, markup, tokens)
  super
  positional, _named = ArgsParser.parse(markup)
  @identifier = positional.first
end

Instance Method Details

#render(context) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/jekyll/shopsavvy/tags.rb', line 149

def render(context)
  client = SiteHelpers.client_for(context)
  return "" unless client && @identifier
  identifier = Liquid::Template.parse(@identifier).render(context)
  offers = client.current_offers(identifier)
  cheapest = (offers["data"] || []).min_by { |o| o["price"].to_f }
  cheapest ? "$#{cheapest["price"]}" : ""
end