Class: PolyListEmbed

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-polylist.rb

Instance Method Summary collapse

Constructor Details

#initialize(tagName, content, tokens) ⇒ PolyListEmbed

Returns a new instance of PolyListEmbed.



9
10
11
12
13
14
# File 'lib/jekyll-polylist.rb', line 9

def initialize(tagName, content, tokens)
  super
  @content = content
  @renderer = Jekyll::PolyEmbed.new()
  @apikey = Jekyll.configuration({})['google_poly']['API_key']
end

Instance Method Details

#get_assets(nextPageToken = "") ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jekyll-polylist.rb', line 16

def get_assets(nextPageToken = "")
  unless nextPageToken.nil?
    pageToken = "&pageToken=#{ nextPageToken }"
  else
    pageToken = ""
  end
  url = "https://poly.googleapis.com/v1/assets?key=#{ @apikey }&keywords=#{ @keyword }&pageSize=100" + pageToken
  response = Faraday.new(url).get
  if response.status == 200
    body = JSON.parse(response.body)
    body["assets"].each do |asset|
      @assets << asset
    end
    unless body["nextPageToken"].nil?
      get_assets(nextPageToken = body["nextPageToken"])
    end
  end
end

#render(context) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jekyll-polylist.rb', line 35

def render(context)
  @keyword = "#{context[@content.strip]}"
  get_assets()
  output = "<div>\n"
  @assets.each do |asset|
    if asset["name"][/assets\/([^\?]*)/]
      poly_id = $1
      iframe = @renderer.generate(context, poly_id)
      output += %Q{
      <div class="poly-asset-container">
        <h3 class="poly-asset-title">#{ asset["displayName"] }</h3>
        <div class="poly-asset-iframe">
          #{ iframe }
        </div>
      </div>
       }
    end
  end
end