Class: GitHub::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/github-api-client/base.rb

Overview

Basic functionality inherited later

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_attributes(resource, attributes) ⇒ Hash

Converts pitfalls from GitHub API differences into normal data

Parameters:

  • resource (Symbol)

    GitHub Resource to parse

  • attributes (Hash)

    GitHub API retrieved attributes to be parsed

  • [Symbol] (Hash)

    a customizable set of options

Returns:

  • (Hash)

    parsed attributes, fully compatibile with local db



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/github-api-client/base.rb', line 44

def self.parse_attributes(resource, attributes)
  return {} unless attributes # Guard against empty results
  hash = case resource
    when :user_get then {:public_repo_count => :nil, :public_gist_count => :nil, :created => :nil, :permission => :nil, :followers_count => :nil, :following_count => :nil}
    when :user_search then {:name => :login, :username => :login, :fullname => :name, :followers => :nil, :repos => :nil, :created => :nil, :permission => :nil}
    when :repo_get then {:fork => :b_fork, :watchers => nil, :owner => :owner_login, :forks => nil, :followers_count => nil, :forks_count => nil, :master_branch => nil}
    when :org_get then {:public_gist_count => nil, :public_repo_count => nil, :following_count => :nil, :followers_count => :nil}
    when :org_repo_index then {:owner => nil, :open_issues => nil, :has_issues => nil, :watchers => nil, :forks => nil, :fork => :b_fork, :gravatar_id => nil, :organization => :organization_login, :master_branch => nil}
    when :org_repo_get then {:owner => nil, :open_issues => nil, :has_issues => nil, :watchers => nil, :forks => nil, :fork => :b_fork, :gravatar_id => nil, :organization => :organization_login}
    else raise "Unknown resource #{resource.inspect} with attributes #{attributes.inspect}"
  end
  # Provides abstraction layer between YAML :keys and 'keys' returned by Hub
  symbolized_resources = [:repo_get, :org_repo_index, :org_repo_get]
  hash.each do |k, v|
    unless v == :nil || v == nil
      if v.class != Symbol
        attributes[k.to_s] = v
      else
        if symbolized_resources.include? resource
          attributes[v.to_s] = attributes[k.to_sym]
        else
          attributes[v.to_s] = attributes[k.to_s]
        end
      end
    end
    if symbolized_resources.include? resource
      attributes.delete k.to_sym
    else
      attributes.delete k.to_s
    end
  end
  attributes
end

.syncObject

Synchronizes every information from local database with GitHub

VERY DANGEROUS AND EVIL

Recursively gets all* GitHub Users, takes years to fetch

    • all that have at least one follower

Returns:

  • nil



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/github-api-client/base.rb', line 17

def self.sync
  puts "Synchronizing local database with GitHub"
  users = GitHub::User.all
  repos = GitHub::Repo.all
  puts "Updating Records of all #{"users".color(:yellow).bright}"
  #progress = ProgressBar.new("Updating records", users.count)
  users.each do |user|
    # Disabled because of its length
    user.fetch(:self)
    #progress.inc
  end
  progress.finish
  #progress = ProgressBar.new("Updating records", repos.count)
  repos.each do |repo|
    repo.fetch(:self, :watchers)
    #progress.inc
  end
  #progress.finish
  nil
end

Instance Method Details

#build(options = {}) ⇒ Object

Sends key= value signals at object, that inherits it

Parameters:

  • options (Hash) (defaults to: {})

    to assign for an object



6
7
8
9
10
# File 'lib/github-api-client/base.rb', line 6

def build(options = {})
  options.each_pair do |k, v|
    self.send "#{k.to_sym}=", v
  end
end

#to_aryHash

ActiveRecord fix that returns attributes

Returns:

  • (Hash)

    Attributes of the object



80
81
82
# File 'lib/github-api-client/base.rb', line 80

def to_ary
  return self.attributes
end