Class: GithubOrganizationsScraper::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/github_organizations_scraper/cli.rb

Class Method Summary collapse

Class Method Details

.display_members_json(stdout, members) ⇒ Object



54
55
56
57
# File 'lib/github_organizations_scraper/cli.rb', line 54

def self.display_members_json(stdout, members)
  require "json"
  stdout.print members.map { |mem| { "user" => mem } }.to_json
end

.display_members_tty(stdout, members) ⇒ Object



47
48
49
50
51
52
# File 'lib/github_organizations_scraper/cli.rb', line 47

def self.display_members_tty(stdout, members)
  members.each do |member|
    details = [member[:login], member[:name], member[:repo_summary]].reject { |d| d.nil? }
    stdout.puts details.join(", ")
  end
end

.execute(stdout, arguments = []) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/github_organizations_scraper/cli.rb', line 7

def self.execute(stdout, arguments=[])

  options = {}

  parser = OptionParser.new do |opts|
    opts.banner = "      Display the members of an organisation account on GitHub.\n\n      Usage: \#{File.basename($0)} [options]\n\n      Options are:\n    BANNER\n    opts.separator \"\"\n    opts.on(\"-j\", \"--json\",\n            \"Display results in JSON format\") { options[:json] = true }\n    opts.on(\"-h\", \"--help\",\n            \"Show this help message.\") { stdout.puts opts; exit }\n    opts.parse!(arguments)\n\n  end\n  \n  account = arguments.shift\n  html = Net::HTTP.get(URI.parse(\"http://github.com/\#{account}\"))\n  doc = Nokogiri::HTML(html)\n  members = doc.search(\"ul.org-members li\").map do |member|\n    login = member.search(\"h4 a\").text\n    if member.search(\"h4 em\").text =~ /\\((.*)\\)/\n      name = $1\n    end\n    repo_summary = member.search(\"p\").text\n    { :login => login, :name => name, :repo_summary => repo_summary }\n  end\n  \n  if options[:json]\n    display_members_json(stdout, members)\n  else\n    display_members_tty(stdout, members)\n  end\nend\n".gsub(/^          /,'')