Class: Repositories::Base

Inherits:
Object
  • Object
show all
Defined in:
app/models/repositories/base.rb

Constant Summary collapse

COLOR =
'#fff'
LIBRARIAN_SUPPORT =
false
LIBRARIAN_PLANNED =
false
SECURITY_PLANNED =
false
HIDDEN =
false

Class Method Summary collapse

Class Method Details

.colorObject


9
10
11
# File 'app/models/repositories/base.rb', line 9

def self.color
  self::COLOR
end

.dependencies(_name, _version) ⇒ Object


170
171
172
# File 'app/models/repositories/base.rb', line 170

def self.dependencies(_name, _version)
  []
end

.download_dependenciesObject


166
167
168
# File 'app/models/repositories/base.rb', line 166

def self.download_dependencies
  project_names.each { |name| save_dependencies(name) }
end

.formatted_nameObject


13
14
15
# File 'app/models/repositories/base.rb', line 13

def self.formatted_name
  self.to_s.demodulize
end

.get(url, options = {}) ⇒ Object


174
175
176
# File 'app/models/repositories/base.rb', line 174

def self.get(url, options = {})
  Oj.load(get_raw(url, options))
end

.get_html(url, options = {}) ⇒ Object


182
183
184
# File 'app/models/repositories/base.rb', line 182

def self.get_html(url, options = {})
  Nokogiri::HTML(get_raw(url, options))
end

.get_json(url) ⇒ Object


186
187
188
# File 'app/models/repositories/base.rb', line 186

def self.get_json(url)
  get(url, headers: { 'Accept' => "application/json"})
end

.get_raw(url, options = {}) ⇒ Object


178
179
180
# File 'app/models/repositories/base.rb', line 178

def self.get_raw(url, options = {})
  Typhoeus.get(url, options).body
end

.import(include_versions = true) ⇒ Object


133
134
135
# File 'app/models/repositories/base.rb', line 133

def self.import(include_versions = true)
  project_names.each { |name| update(name, include_versions) }
end

.import_asyncObject


121
122
123
# File 'app/models/repositories/base.rb', line 121

def self.import_async
  project_names.each { |name| RepositoryDownloadWorker.perform_async(self.name.demodulize, name) }
end

.import_newObject


148
149
150
# File 'app/models/repositories/base.rb', line 148

def self.import_new
  new_names.each { |name| update(name) }
end

.import_new_asyncObject


129
130
131
# File 'app/models/repositories/base.rb', line 129

def self.import_new_async
  new_names.each { |name| RepositoryDownloadWorker.perform_async(self.name.demodulize, name) }
end

.import_recentObject


137
138
139
# File 'app/models/repositories/base.rb', line 137

def self.import_recent
  recent_names.each { |name| update(name) }
end

.import_recent_asyncObject


125
126
127
# File 'app/models/repositories/base.rb', line 125

def self.import_recent_async
  recent_names.each { |name| RepositoryDownloadWorker.perform_async(self.name.demodulize, name) }
end

.new_namesObject


141
142
143
144
145
146
# File 'app/models/repositories/base.rb', line 141

def self.new_names
  names = project_names
  existing_names = []
  Project.platform(self.name.demodulize).select(:id, :name).find_each {|project| existing_names << project.name }
  names - existing_names
end

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
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
77
78
79
80
81
82
83
# File 'app/models/repositories/base.rb', line 17

def self.package_link(project, version = nil)
  name = project.name
  platform = project.platform
  case platform
  when 'Inqlude'
    "https://inqlude.org/libraries/#{name}.html"
  when 'Hex'
    "https://hex.pm/packages/#{name}/#{version}"
  when 'Dub'
    "http://code.dlang.org/packages/#{name}" + (version ? "/#{version}" : "")
  when 'Emacs'
    "http://melpa.org/#/#{name}"
  when 'Jam'
    "http://jamjs.org/packages/#/details/#{name}/#{version}"
  when 'Pub'
    "https://pub.dartlang.org/packages/#{name}"
  when 'NPM'
    "https://www.npmjs.com/package/#{name}"
  when 'Rubygems'
    "https://rubygems.org/gems/#{name}" + (version ? "/versions/#{version}" : "")
  when 'Sublime'
    "https://packagecontrol.io/packages/#{name}"
  when 'Pypi'
    "https://pypi.python.org/pypi/#{name}/#{version}"
  when 'Packagist'
    "https://packagist.org/packages/#{name}##{version}"
  when 'Cargo'
    "https://crates.io/crates/#{name}/#{version}"
  when 'Hackage'
    "http://hackage.haskell.org/package/#{name}" + (version ? "-#{version}" : "")
  when 'Go'
    "http://go-search.org/view?id=#{name}"
  when 'Wordpress'
    "https://wordpress.org/plugins/#{name}/#{version}"
  when 'NuGet'
    "https://www.nuget.org/packages/#{name}/#{version}"
  when 'CPAN'
    "https://metacpan.org/release/#{name}"
  when 'CRAN'
    "http://cran.r-project.org/web/packages/#{name}/index.html"
  when 'CocoaPods'
    "http://cocoapods.org/pods/#{name}"
  when 'Julia'
    "http://pkg.julialang.org/?pkg=#{name}&ver=release"
  when 'Atom'
    "https://atom.io/packages/#{name}"
  when 'Elm'
    "http://package.elm-lang.org/packages/#{name}/#{version || 'latest'}"
  when 'Clojars'
    "https://clojars.org/#{name}" + (version ? "/versions/#{version}" : "")
  when 'Maven'
    if version
      "http://search.maven.org/#artifactdetails%7C#{name.gsub(':', '%7C')}%7C#{version}%7Cjar"
    else
      group, artifact = name.split(':')
      "http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22#{group}%22%20AND%20a%3A%22#{artifact}%22"
    end
  when 'Meteor'
    "https://atmospherejs.com/#{name.gsub(':', '/')}"
  when 'PlatformIO'
    "http://platformio.org/lib/show/#{project.pm_id}/#{name}"
  when 'Homebrew'
    "http://brewformulas.org/#{name}"
  when 'Shards'
    "https://crystal-shards-registry.herokuapp.com/shards/#{name}"
  end
end

.repo_fallback(repo, homepage) ⇒ Object


190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/models/repositories/base.rb', line 190

def self.repo_fallback(repo, homepage)
  repo = '' if repo.nil?
  homepage = '' if homepage.nil?
  repo_gh = GithubUrls.parse(repo)
  homepage_gh = GithubUrls.parse(homepage)
  if repo_gh.present?
    return "https://github.com/#{repo_gh}"
  elsif homepage_gh.present?
    return "https://github.com/#{homepage_gh}"
  else
    repo
  end
end

.save(project, include_versions = true) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/repositories/base.rb', line 85

def self.save(project, include_versions = true)
  mapped_project = mapping(project).delete_if { |key, value| value.blank? }
  return false unless mapped_project
  puts "Saving #{mapped_project[:name]}"
  dbproject = Project.find_or_initialize_by({:name => mapped_project[:name], :platform => self.name.demodulize})
  if dbproject.new_record?
    dbproject.assign_attributes(mapped_project.except(:name))
    dbproject.save
  else
    dbproject.update_attributes(mapped_project.except(:name))
  end

  if include_versions && self::HAS_VERSIONS
    versions(project).each do |version|
      dbproject.versions.find_or_create_by(version)
    end
  end

  if self::HAS_DEPENDENCIES
    save_dependencies(mapped_project[:name])
  end
  dbproject.save
  dbproject
end

.save_dependencies(name) ⇒ Object


152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/repositories/base.rb', line 152

def self.save_dependencies(name)
  proj = Project.find_by(name: name, platform: self.name.demodulize)
  proj.versions.each do |version|
    deps = dependencies(name, version.number)
    next unless deps.any? && version.dependencies.empty?
    deps.each do |dep|
      unless version.dependencies.find_by_project_name dep[:project_name]
        named_project = Project.platform(self.name.demodulize).where('lower(name) = ?', dep[:project_name].downcase).first.try(:id)
        version.dependencies.create(dep.merge(project_id: named_project.try(:strip)))
      end
    end
  end
end

.update(name, include_versions = true) ⇒ Object


110
111
112
113
114
115
116
117
118
119
# File 'app/models/repositories/base.rb', line 110

def self.update(name, include_versions = true)
  begin
    save(project(name), include_versions)
  rescue SystemExit, Interrupt
    exit 0
  rescue Exception => e
    p name
    p e
  end
end