Class: JekyllOpenSdgPlugins::FetchRemoteData

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-open-sdg-plugins/fetch_remote_data.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jekyll-open-sdg-plugins/fetch_remote_data.rb', line 11

def generate(site)
  # Backwards compatibility - only do this if 'jekyll_get_json' is absent.
  # (This is the older style of remote data fetching.)
  if !site.config['jekyll_get_json']
    # First try to grab the remote data.
    if site.config['remote_data_prefix']
      prefix = site.config['remote_data_prefix']
      # Set the required remotedatabaseurl config setting.
      site.config['remotedatabaseurl'] = prefix
      # Fetch remote data for each of our endpoints.
      endpoints = {
        'meta' => 'meta/all.json',
        'headlines' => 'headline/all.json',
        'schema' => 'meta/schema.json',
        'reporting' => 'stats/reporting.json'
      }
      endpoints.each do |key, value|
        target = site.data[key]
        endpoint = prefix + '/' + value
        begin
          source = JSON.load(open(endpoint))
          if target
            target.deep_merge(source)
          else
            site.data[key] = source
          end
        rescue StandardError => e
          puts e.message
          abort 'Unable to fetch remote data from: ' + endpoint
        end
      end
    end

    # Next try to grab any remote translations.
    if site.config['remote_translations']
      key = 'translations'
      target = site.data[key]
      site.config['remote_translations'].each do |endpoint|
        begin
          source = JSON.load(open(endpoint))
          if target
            target.deep_merge(source)
          else
            site.data[key] = source
          end
        rescue StandardError => e
          puts e.message
          abort 'Unable to fetch remote translation from: ' + endpoint
        end
      end
    end
  end
end