Class: Repositories::Hackage

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

Constant Summary collapse

HAS_VERSIONS =
true
HAS_DEPENDENCIES =
false
LIBRARIAN_PLANNED =
true
URL =
'http://hackage.haskell.org'
COLOR =
'#29b544'

Constants inherited from Base

Base::HIDDEN, Base::LIBRARIAN_SUPPORT, Base::SECURITY_PLANNED

Class Method Summary collapse

Methods inherited from Base

color, dependencies, download_dependencies, formatted_name, get, get_html, get_json, get_raw, import, import_async, import_new, import_new_async, import_recent, import_recent_async, new_names, package_link, repo_fallback, save, save_dependencies, update

Class Method Details

.description(page) ⇒ Object


52
53
54
55
56
# File 'app/models/repositories/hackage.rb', line 52

def self.description(page)
  contents = page.css('#content p, #content hr' ).map(&:text)
  index = contents.index ''
  contents[0..(index - 1)].join("\n\n")
end

.find_attribute(page, name) ⇒ Object


47
48
49
50
# File 'app/models/repositories/hackage.rb', line 47

def self.find_attribute(page, name)
  tr = page.css('#content tr').select { |t| t.css('th').text == name }.first
  tr.css('td').text if tr
end

.mapping(project) ⇒ Object


26
27
28
29
30
31
32
33
34
35
# File 'app/models/repositories/hackage.rb', line 26

def self.mapping(project)
  {
    name: project[:name],
    keywords_array: Array(project[:page].css('#content div:first a')[1..-1].map(&:text)),
    description: description(project[:page]),
    licenses: find_attribute(project[:page], 'License'),
    homepage: find_attribute(project[:page], 'Home page'),
    repository_url: repo_fallback(repository_url(find_attribute(project[:page], 'Source repository')), find_attribute(project[:page], 'Home page'))
  }
end

.project(name) ⇒ Object


19
20
21
22
23
24
# File 'app/models/repositories/hackage.rb', line 19

def self.project(name)
  {
    name: name,
    page: get_html("http://hackage.haskell.org/package/#{name}")
  }
end

.project_namesObject


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

def self.project_names
  get_json("http://hackage.haskell.org/packages/").map{ |h| h['packageName'] }
end

.recent_namesObject


13
14
15
16
17
# File 'app/models/repositories/hackage.rb', line 13

def self.recent_names
  u = 'http://hackage.haskell.org/packages/recent.rss'
  titles = SimpleRSS.parse(Typhoeus.get(u).body).items.map(&:title)
  titles.map { |t| t.split(' ').first }
end

.repository_url(text) ⇒ Object


58
59
60
61
62
63
# File 'app/models/repositories/hackage.rb', line 58

def self.repository_url(text)
  return nil unless text.present?
  match = text.match(/github.com\/(.+?)\.git/)
  return nil unless match
  "https://github.com/#{match[1]}"
end

.versions(project) ⇒ Object


37
38
39
40
41
42
43
44
45
# File 'app/models/repositories/hackage.rb', line 37

def self.versions(project)
  versions = find_attribute(project[:page], 'Versions')
  versions = find_attribute(project[:page], 'Version') if versions.nil?
  versions.split(',').map(&:strip).map do |v|
    {
      :number => v
    }
  end
end