Sha256: 9db87d69ceef1a393d4fd99e1015d73cc2248504ce46c57f3a5dec60b6ab411f

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'httparty'
require 'rss'

module HaravanTheme
  FEED_URL = 'https://github.com/Haravan/Timber/releases.atom'
  ZIP_URL  = 'https://github.com/Haravan/Timber/archive/%s.zip'

  class Releases
    class VersionError < StandardError; end
    Release = Struct.new(:version) do
      def zip_url
        ZIP_URL % version
      end
    end

    def fetch!
      response = HTTParty.get(FEED_URL)
      raise "Could not retrieve feed from #{FEED_URL}" if response.code != 200
      @feed = RSS::Parser.parse(response.body)
    end

    def all
      @all ||= begin
        versioned_releases.reduce({'master' => master, 'latest' => latest}) do |all, release|
          all[release.version] = release
          all
        end
      end
    end

    def find(version)
      release = all[version]
      if release.nil?
        error = [
          "Invalid version '#{version}'.",
          "Valid versions are:",
        ].concat(all.keys.map{|v| "  #{v}"})
        raise VersionError, error.join("\n")
      end
      release
    end

    private
    def versioned_releases
      fetch! unless @feed
      @versioned_releases ||= @feed.items.map { |item| Release.new(item.title.content) }
    end

    def latest
      Release.new(versioned_releases.first.version)
    end

    def master
      Release.new('master')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
haravan_theme-0.0.26 lib/haravan_theme/releases.rb
haravan_theme-0.0.25 lib/haravan_theme/releases.rb