module OctocatHerder::Base

This provides most of the functionality to interact with the GitHub v3 API.

Attributes

connection[R]

Our {OctocatHerder::Connection}, so we can make more requests based on the information we retrieved from the GitHub API.

@since 0.0.1 @return [OctocatHerder::Connection]

raw[R]

The re-hydrated JSON retrieved from the GitHub API.

@since 0.0.1 @return [Hash]

Public Class Methods

new(raw_hash, conn = OctocatHerder::Connection.new) click to toggle source

@api private @since 0.0.1

@param [Hash] raw_hash The re-hydrated JSON received from the

GitHub API via {OctocatHerder::Connection}.

@param [OctocatHerder::Connection] conn If not provided requests

will be unauthenticated.
# File lib/octocat_herder/base.rb, line 38
def initialize(raw_hash, conn = OctocatHerder::Connection.new)
  @connection = conn
  @raw = raw_hash
end

Public Instance Methods

available_attributes() click to toggle source

This returns a list of the things that the API request returned to us.

@since 0.0.1

@return [Array<String>] Names of available methods providing

additional detail about the object.
# File lib/octocat_herder/base.rb, line 63
def available_attributes
  attrs = []
  attrs += @raw.keys.reject do |k|
    [
      'id',
      'type',
    ].include? k
  end if @raw

  (attrs + additional_attributes).uniq
end
method_missing(id, *args) click to toggle source

We use the method_missing magic to create accessors for the information we got back from the GitHub API. You can get a list of all of the available things from {#available_attributes}.

@since 0.0.1

# File lib/octocat_herder/base.rb, line 48
def method_missing(id, *args)
  unless @raw and @raw.keys.include?(id.id2name)
    raise NoMethodError.new("undefined method #{id.id2name} for #{self}:#{self.class}")
  end

  @raw[id.id2name]
end

Private Instance Methods

additional_attributes() click to toggle source

Intended to be overridden in classes using {OctocatHerder::Base}, so they can make the methods they define show up in {#available_attributes}.

@since 0.0.1

# File lib/octocat_herder/base.rb, line 82
def additional_attributes
  []
end
parse_date_time(date_time) click to toggle source

@since 0.0.1

# File lib/octocat_herder/base.rb, line 87
def parse_date_time(date_time)
  return nil unless date_time

  if defined? ParseDate
    Time.utc(*ParseDate.parsedate(date_time))
  else
    DateTime.parse date_time
  end
end