Class: ExampleData

Inherits:
Object
  • Object
show all
Includes:
ContributorsStats::JsonHelper
Defined in:
lib/example_data.rb

Overview

Dump some test data from github, rather one time, but who knows ;)

Instance Method Summary collapse

Methods included from ContributorsStats::JsonHelper

#load_json, #path_prefix, #path_suffix, #url_builder

Constructor Details

#initialize(target_path = "../../test/fixtures-gh") ⇒ ExampleData

setup proper context for dumping data



8
9
10
11
12
# File 'lib/example_data.rb', line 8

def initialize(target_path = "../../test/fixtures-gh")
  target_path = File.expand_path( target_path, __FILE__ ) unless Dir.exist?(target_path)
  Dir.chdir(target_path)
  `rm -rf *` # clean
end

Instance Method Details

#parse_org(org_url) ⇒ Object

parse organization data from given url, parses also repositories

See Also:



16
17
18
19
20
21
22
23
# File 'lib/example_data.rb', line 16

def parse_org(org_url)
  puts "reading: #{org_url}"
  org_data = load_json(org_url)
  write(org_url, org_data)
  org_data.each do |repo|
    parse_repo(repo['contributors_url'], "  ")
  end
end

#parse_repo(contributors_url, str_prefix = "") ⇒ Object

parse repository data from given url, parses also users

See Also:



27
28
29
30
31
32
33
34
# File 'lib/example_data.rb', line 27

def parse_repo(contributors_url, str_prefix="")
  puts "#{str_prefix}reading: #{contributors_url}"
  contributors_data = load_json(contributors_url)
  write(contributors_url, contributors_data)
  contributors_data.each do |contributor|
    parse_user(contributor['url'], str_prefix+"  ")
  end
end

#parse_user(user_url, str_prefix = "") ⇒ Object

parse user data from given url



37
38
39
40
41
# File 'lib/example_data.rb', line 37

def parse_user(user_url, str_prefix="")
  puts "#{str_prefix}reading: #{user_url}"
  user_data = load_json(user_url)
  write(user_url, user_data)
end

#remove_prefix(conten) ⇒ Object

remove prefix from all urls in the given content



54
55
56
# File 'lib/example_data.rb', line 54

def remove_prefix(conten)
  conten.gsub(/#{path_prefix}([^"]*)/, "\\1.json")
end

#write(file, content) ⇒ Object

saves content to path corresponding to the given file url



44
45
46
47
48
49
50
51
# File 'lib/example_data.rb', line 44

def write(file, content)
  file = remove_prefix(file)
  return if File.exist?(file)
  `mkdir -p #{File.dirname(file)}`
  File.open(file, "w+") { |f|
    f.write(remove_prefix(JSON.pretty_generate(content)))
  }
end