Class: JekyllGetBooks::GetBooksGenerator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-get-books/converter.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



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
# File 'lib/jekyll-get-books/converter.rb', line 12

def generate(site)

  config = site.config['jekyll_get_books']
  if !config
    warn "No config".yellow
    return
  end
  if !config.kind_of?(Array)
    config = [config]
  end

  config.each do |d|
    begin
      target = site.data[d['data']]
      source = d['json']
      file = d['file']
      results = {}
      if !(File.file?(file))
        warn "File does not exist / Path is incorrect".yellow
      end
      CSV.foreach((file), headers: true, col_sep: ",") do |row|
        begin
          connection = URI.open(source+row['isbn'])
          meta = connection.status[1]
          if meta == "OK"
            warn "JSON fetched succesfully"
          else
            warn "An HTTP erro occurred while fetching the JSON (" +meta+")"
          end
          output = JSON.load(connection)
        rescue => e
          case e
            when OpenURI::HTTPError
            warn "A HTTP Error occurred while accessing the API".yellow
            when SocketError
            warn "A Socket Error occurred while accessing the API".yellow
          end
        end
        results.deep_merge(output)
      end 
      site.data[d['data']] = results
    rescue
      next
    end
  end
end