Class: ExtraLoop::Storage::ScrapingSession

Inherits:
Ohm::Model
  • Object
show all
Includes:
Ohm::Boundaries, Ohm::Callbacks, Ohm::Timestamping
Defined in:
lib/extraloop/redis-storage/scraping_session.rb

Instance Method Summary collapse

Instance Method Details

#records(params = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/extraloop/redis-storage/scraping_session.rb', line 11

def records(params={})
  klass = if Object.const_defined?(model.id)
    Object.const_get(model.id)
  else
    dynamic_class = Class.new(ExtraLoop::Storage::Record) do
      # override the default to_hash so that it will return the Redis hash
      # internally stored by Ohm
      def to_hash
        Ohm.redis.hgetall self.key
      end
    end

    Object.const_set(model.id, dynamic_class)
    dynamic_class
  end

  # set a session index, so that Ohm finder will work
  klass.indices << :session_id unless klass.indices.include? :session_id

  klass.find({
    :session_id => self.id
  }.merge(params))
end

#to_csvObject



50
51
52
53
54
55
# File 'lib/extraloop/redis-storage/scraping_session.rb', line 50

def to_csv
  _records = Array records.all.map &:to_hash
  header = _records.first && _records.first.keys.map(&:to_s)
  data = [header].concat _records.map(&:values)
  output = data.map { |cells| CSV.generate_line cells }.join
end

#to_hashObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/extraloop/redis-storage/scraping_session.rb', line 39

def to_hash
  attrs = attributes.reduce({}) { |memo, attribute| 
    memo.merge(attribute => send(attribute))
  }.merge({
    :records => records.map(&:to_hash),
    :model => model.to_hash
  })

  super.merge attrs
end

#to_yamlObject



57
58
59
# File 'lib/extraloop/redis-storage/scraping_session.rb', line 57

def to_yaml
  to_hash.to_yaml
end

#validateObject



35
36
37
# File 'lib/extraloop/redis-storage/scraping_session.rb', line 35

def validate
  assert_present :model
end