Class: Mynewsdesk

Inherits:
Middleman::Extension
  • Object
show all
Defined in:
lib/middleman_mynewsdesk/extension.rb

Overview

Extension namespace

Instance Method Summary collapse

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ Mynewsdesk

Returns a new instance of Mynewsdesk.



14
15
16
17
18
19
20
21
22
23
# File 'lib/middleman_mynewsdesk/extension.rb', line 14

def initialize(app, options_hash = {}, &block)
  # Call super to build options from the options_hash
  super

  # Require libraries only when activated
  require "http"
  require "json"

  @base_url = "https://www.mynewsdesk.com/services/pressroom/list/#{options.api_key}?format=json&limit=100"
end

Instance Method Details

#all_items_for(item) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/middleman_mynewsdesk/extension.rb', line 25

def all_items_for(item)
  items = []
  0.step(2000, 100) do |offset|
    i = items_for_offset(item, offset)
    return items if i.nil? # If we have offset 100 and exactly 100 posts, we get nil back
    items.concat i
    break if i.length < 100
  end
  items
end

#items_for_offset(item, offset) ⇒ Object



36
37
38
# File 'lib/middleman_mynewsdesk/extension.rb', line 36

def items_for_offset(item, offset)
  JSON.parse(HTTP.get("#{@base_url}&type_of_media=#{item}&offset=#{offset}").to_s, object_class: RecursiveOpenStruct).items.item
end

#mynewsdesk_itemsObject



40
41
42
43
44
45
46
# File 'lib/middleman_mynewsdesk/extension.rb', line 40

def mynewsdesk_items
  @items ||= options.types_of_media.map do |item|
               all_items_for(item)
             end.flatten.sort { |a, b| Time.parse(b.published_at) <=> Time.parse(a.published_at) }

  @items
end