Class: LaPack::GithubProvider

Inherits:
Provider
  • Object
show all
Defined in:
lib/providers/github.rb

Constant Summary collapse

@@PER_PAGE =

Entries per github query page

100

Instance Method Summary collapse

Methods inherited from Provider

#configure

Constructor Details

#initialize(env, params = {}) ⇒ GithubProvider

Returns a new instance of GithubProvider.



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

def initialize(env, params = {})
  super env, 'github', params

  @github_db = "https://api.github.com/search/repositories?q=language:tex&page=%d&per_page=#{@@PER_PAGE}"
end

Instance Method Details

#init(dbdir) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/providers/github.rb', line 14

def init(dbdir)
  # dbdir path
  @dbdir = dbdir
  # dbdir name
  @packages = File.join(dbdir, 'pkg')
  # Package index
  @index = File.join(dbdir, "#{@name}.json")

  FileUtils.mkdir_p(@packages) unless File.exists?(@packages)

  raise "Can't write to #{@packages}. Not a directory" unless File.directory?(@packages)
end

#install(to_dir, *packages) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/providers/github.rb', line 55

def install(to_dir, *packages)
  packages.each do |package|
    LaPack.log("Installing #{package.blue.bold}")
    if list.select{|p| p[:name].eql?(package)}.empty?
      raise "No such package #{package.white.bold}"
    else
      install_package(package, to_dir)
    end
  end
end

#listObject



47
48
49
# File 'lib/providers/github.rb', line 47

def list
  LaPack.log("Unavailable", :warning)
end

#remove(*packages) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/providers/github.rb', line 66

def remove(*packages)
  packages.each do |package|
    LaPack.log("Removing #{package.blue.bold}")
    if list.select{|p| p[:name].eql?(package)}.empty?
      raise "No such package #{package.white.bold}"
    else
      ctan_remove(package)
    end
  end
end

#scan_page(page) ⇒ Object



43
44
45
# File 'lib/providers/github.rb', line 43

def scan_page(page)
  page[:items].map{|item| item.select{|k, v| [:html_url, :name, :description, :git_url, :full_name].include?(k)}}
end

#show(package) ⇒ Object



51
52
53
# File 'lib/providers/github.rb', line 51

def show(package)
  LaPack.log("Unavailable", :warning)
end

#updateObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/providers/github.rb', line 27

def update
  first_page = JSON.parse(LaPack.gets(@github_db % [ 1 ]), symbolize_names: true)
  repos_count = first_page[:total_count]
  pages = (repos_count / @@PER_PAGE) + ((repos_count % @@PER_PAGE) == 0 ? 0 : 1)
  repos = []

  repos << scan_page(first_page)
  repos << (2..pages).map do |page|
    sleep(60 / 5) # Five requests per minute limitation
    LaPack.log("Getting page #{page} from #{pages}")
    scan_page(JSON.parse(LaPack.gets(@github_db % [page]), symbolize_names: true))
  end

  File.open(@index, "w") {|f| f << repos.to_json}
end