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 = <<-BANNER.gsub(/^          /,'')
      Display the members of an organisation account on GitHub.

      Usage: #{File.basename($0)} [options]

      Options are:
    BANNER
    opts.separator ""
    opts.on("-j", "--json",
            "Display results in JSON format") { options[:json] = true }
    opts.on("-h", "--help",
            "Show this help message.") { stdout.puts opts; exit }
    opts.parse!(arguments)

  end
  
   = arguments.shift
  html = Net::HTTP.get(URI.parse("http://github.com/#{}"))
  doc = Nokogiri::HTML(html)
  members = doc.search("ul.org-members li").map do |member|
     = member.search("h4 a").text
    if member.search("h4 em").text =~ /\((.*)\)/
      name = $1
    end
    repo_summary = member.search("p").text
    { :login => , :name => name, :repo_summary => repo_summary }
  end
  
  if options[:json]
    display_members_json(stdout, members)
  else
    display_members_tty(stdout, members)
  end
end