Sha256: 2fe804d8fc1422ca6177d27d2531e7a8ac899184fb05e5b8d5df68f59770778d

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Nearmiss

  # Class to parse incident owner and name from
  # URLs and to generate URLs
  class Incident
    attr_accessor :id

    # Instantiate from a incident URL
    #
    # @return [Incident]
    def self.from_url(url)
      Incident.new(URI.parse(url).path[1..-1])
    end


    def initialize(incident)
      case incident
      # when Integer
      #   @id = incident
      when String
        @id = incident
        # @owner, @name = repo.split('/')
        # unless @owner && @name
        #   raise ArgumentError, "Invalid Incident. Use user/repo format."
        # end
      when Incident
        @id   = incident.id
        # @name = repo.name
      when Hash
        @id = incident[:incident] ||= incident[:id]
        # @owner = repo[:owner] ||= repo[:user] ||= repo[:username]
      end
    end

    # Incident owner/name
    # @return [String]
    def slug
      # "#{@owner}/#{@name}"
    end
    alias :to_s :slug

    # @return [String] Incident API path
    def path
      # return named_api_path if @owner && @name
      return id_api_path if @id
    end

    # Get the api path for a repo
    # @param incident [Integer, String, Hash, Incident] A incident.
    # @return [String] Api path.
    def self.path(incident)
      new(incident).path
    end

    # @return [String] Api path for owner/name identified repos
    # def named_api_path
    #   "repos/#{slug}"
    # end

    # @return [String] Api path for id identified incidents
    def id_api_path
      "incidents/#{@id}"
    end

    # Incident URL based on {Nearmiss::Client#web_endpoint}
    # @return [String]
    # def url
    #   "#{Octokit.web_endpoint}#{slug}"
    # end

    # alias :user :owner
    # alias :username :owner
    # alias :repo :name
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nearmiss-ruby-1.0.4 lib/nearmiss-ruby/incident.rb