Class: FaviconGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/favicon_generator.rb

Constant Summary collapse

API_KEY =
'04641dc33598f5463c2f1ff49dd9e4a617559f4b'
PATH_UNIQUE_KEY =
'Dfv87ZbNh2'

Instance Method Summary collapse

Instance Method Details

#generate_faviconObject



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
# File 'lib/generators/favicon_generator.rb', line 12

def generate_favicon
  req = JSON.parse File.read('config/favicon.json')

  req['api_key'] = API_KEY

  req['files_location'] = Hash.new
  req['files_location']['type'] = 'path'
  req['files_location']['path'] = PATH_UNIQUE_KEY

  master_pic = File.expand_path(".") + '/' + req['master_picture']
  req['master_picture'] = Hash.new
  req['master_picture']['type'] = 'inline'
  req['master_picture']['content'] = Base64.encode64(File.read(master_pic))

  response = RestClient.post("https://realfavicongenerator.net/api/favicon",
    {favicon_generation: req}.to_json, content_type: :json)
  resp = JSON.parse(response)

  zip = resp['favicon_generation_result']['favicon']['package_url']
  FileUtils.mkdir_p('app/assets/images/favicon')

  Dir.mktmpdir 'rfg' do |tmp_dir|
    download_package zip, tmp_dir
    Dir["#{tmp_dir}/*.*"].each do |file|
      content = File.read(file)
      new_ext = ''
      if File.extname(file) == '.json' or File.extname(file) == '.xml'
        content = replace_url_by_asset_path content
        new_ext = '.erb'
      end
      create_file "app/assets/images/favicon/#{File.basename file}#{new_ext}", content
    end
  end

  create_file "app/views/application/_favicon.html.erb",
    replace_url_by_asset_path(resp['favicon_generation_result']['favicon']['html_code'])
end