Class: Gigbot::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/gigbot/source.rb

Constant Summary collapse

SOURCES_YAML =
File.dirname(__FILE__) + '/sources.yml'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, parser_name, title) ⇒ Source

Returns a new instance of Source.



12
13
14
15
16
17
# File 'lib/gigbot/source.rb', line 12

def initialize(url, parser_name, title)
  @url = url
  @parser_name = parser_name
  @title = title
  @imported = []
end

Instance Attribute Details

#importedObject (readonly)

Returns the value of attribute imported.



10
11
12
# File 'lib/gigbot/source.rb', line 10

def imported
  @imported
end

#parser_nameObject (readonly)

Returns the value of attribute parser_name.



10
11
12
# File 'lib/gigbot/source.rb', line 10

def parser_name
  @parser_name
end

#titleObject (readonly)

Returns the value of attribute title.



10
11
12
# File 'lib/gigbot/source.rb', line 10

def title
  @title
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/gigbot/source.rb', line 10

def url
  @url
end

Class Method Details

.allObject



49
50
51
52
53
54
# File 'lib/gigbot/source.rb', line 49

def self.all
  definitions = YAML.load_file(SOURCES_YAML)
  definitions.map do |definition|
    new(definition['url'], definition['parser'], definition['title'])
  end
end

.eachObject



56
57
58
59
60
# File 'lib/gigbot/source.rb', line 56

def self.each
  all.each do |source|
    yield source
  end
end

.find(id) ⇒ Object



62
63
64
# File 'lib/gigbot/source.rb', line 62

def self.find(id)
  all.find {|source| source.id == id}
end

Instance Method Details

#idObject



27
28
29
# File 'lib/gigbot/source.rb', line 27

def id
  Digest::SHA1.hexdigest(url)
end

#importObject



31
32
33
34
35
36
37
38
# File 'lib/gigbot/source.rb', line 31

def import
  @imported = []
  parser.parse do |params|
    gig = Gig.new(params.merge(source_id: self.id))
    gig.save
    @imported << gig
  end
end

#import_deep(gig) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/gigbot/source.rb', line 40

def import_deep(gig)
  params = parser.parse_deep(gig)
  if params.length > 0
    return gig.update_attributes(params)
  else
    return false
  end
end

#parserObject



23
24
25
# File 'lib/gigbot/source.rb', line 23

def parser
  @parser ||= parser_class.new(url)
end

#parser_classObject



19
20
21
# File 'lib/gigbot/source.rb', line 19

def parser_class
  Gigbot::Parsers[parser_name]
end