Class: Jekyll::ShieldsIO::ShieldFactory
- Inherits:
-
Object
- Object
- Jekyll::ShieldsIO::ShieldFactory
- Defined in:
- lib/jekyll-shields_io.rb
Overview
Factory for generating Shields.IO’s shield
Instance Method Summary collapse
-
#get_shield(config) ⇒ Shield
Fetches the shields from the service or retrieves one from cache.
-
#initialize(context) ⇒ ShieldFactory
constructor
A new instance of ShieldFactory.
-
#queue_shield(shield) ⇒ Object
Queue given Shield for this Jekyll site’s static files.
- #target_dir(for_local = false) ⇒ Object
Constructor Details
#initialize(context) ⇒ ShieldFactory
Returns a new instance of ShieldFactory.
12 13 14 15 16 17 18 |
# File 'lib/jekyll-shields_io.rb', line 12 def initialize(context) # @type [Jekyll::Site] @site = context.registers[:site] # This Jekyll site's "source" config # @type [String] @source_dir = File.absolute_path context.registers[:site].config["source"], Dir.pwd end |
Instance Method Details
#get_shield(config) ⇒ Shield
Fetches the shields from the service or retrieves one from cache
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 |
# File 'lib/jekyll-shields_io.rb', line 24 def get_shield(config) href = config[:href] alt = config[:alt] cls = config[:class] query = hash_to_query(config, [:href, :alt, :class]) unless File.exist? cache_dir FileUtils.mkdir_p cache_dir log "Cache directory #{cache_dir} was made for Shields.IO tags." end cache_file = "#{Digest::MD5.hexdigest query}.svg" cache_path = File.join cache_dir, cache_file # Consult the cache first if File.exist? cache_path log "Cache hit for query: #{query} => #{cache_path}" # Good news: Shields.IO outputs SVG, which is just XML, and it makes our job very easy! image_xml = Nokogiri::XML(File.read(cache_path)) else log "Cache missed for query: #{query}" # If the cache does not exist, we need to get the file. response = HTTParty.get "https://img.shields.io/static/v1?#{query}" unless response.code.div(100) == 2 raise ShieldFetchError.new "Shields.io refused our request with response code #{response.code}" end img = response.body File.write cache_path, img image_xml = Nokogiri::XML(img) log "Cached shield for #{query} => #{cache_path}" end width = image_xml.root["width"].to_i height = image_xml.root["height"].to_i Shield.new(width, height, cache_path, href, alt, cls) end |
#queue_shield(shield) ⇒ Object
Queue given Shield for this Jekyll site’s static files
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/jekyll-shields_io.rb', line 63 def queue_shield(shield) unless File.exist? shield.path raise ShieldFileError.new end if @site.static_files.select { |f| f.is_a? StaticShieldFile }.select { |s| s.name == shield.basename }.any? log "#{shield.basename} already queued for static files" return end # Polyglot compatibility if @site.respond_to?(:active_lang) log "Detected Polyglot" unless @site.active_lang == @site.default_lang log "Skipping copy because of non-default lang site is being built (active lang = #{@site.active_lang})" return end end @site.static_files << StaticShieldFile.new(@site, @site.source, File.join("_cache", "shields_io"), shield.basename, target_dir(true)) log "Cached shield queued for copying" end |
#target_dir(for_local = false) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/jekyll-shields_io.rb', line 85 def target_dir(for_local = false) if for_local File.join "assets", "img", "shields" end "assets/img/shields" end |