Sha256: d32094738d203532a835a3da97cff490545f3e6bfe79eb055d34b6be99140bae

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module Octonore

  # A gitignore template. Templates consist of a name and source.
  class Template

    attr_accessor :name, :source

    include HTTParty

    USER_AGENT = "octocore/#{VERSION}"

    base_uri 'https://api.github.com/gitignore'

    # Create a new template.
    #
    # Example:
    #   c_template = Octonore::Template.new('C')
    #   java_template = Octonore::Template.new('Java')
    #
    # Arguments:
    #   name: (String)
    
    def initialize(name)
      self.name = name
      update
    end

    # Update the Gitignore source from Github.

    def update
      data = get_data
      
      if valid_template_hash? data
        @source = data["source"]
      else
        raise GitignoreTemplateNotFoundError,
          "Template '#{@name}' does not exist!"
      end
    end


    private

    def get_data
      self.class.get "/templates/#{self.name}", headers: headers 
    end

    def valid_template_hash?(template_hash)
      template_hash["message"] != "Not Found"
    end

    def headers
      {"User-Agent" => USER_AGENT}
    end

  end

  class GitignoreTemplateNotFoundError < StandardError
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
octonore-0.0.3 lib/octonore/template.rb