Class: MurmuringSpider::Operation

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/murmuring_spider/operation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(type, target, opts = {}) ⇒ Object

Add an operation

  • type : request type. Name of a Twitter’s method

  • target : First argument of the Twitter’s method. Usually, an user or a operation

  • opts : options. Second argument of the Twitter’s method.

returns : created Operation instance

raises : DataMapper::SaveFailureError



34
35
36
# File 'lib/murmuring_spider/operation.rb', line 34

def add(type, target, opts = {})
  create(:type => type, :target => target, :opts => opts)
end

.remove(type, target) ⇒ Object

Remove an operation specified by type and target



48
49
50
# File 'lib/murmuring_spider/operation.rb', line 48

def remove(type, target)
  first(:type => type, :target => target).destroy
end

.run_all(client = Twitter) ⇒ Object

Run all queries



41
42
43
# File 'lib/murmuring_spider/operation.rb', line 41

def run_all(client = Twitter)
  all.map { |o| o.run(client) }
end

Instance Method Details

#collect_statuses(client = Twitter) ⇒ Object

Execute Twitter request and update :since_id of opts This method has side effect

returns : Array of Twitter::Status



59
60
61
62
63
64
65
66
# File 'lib/murmuring_spider/operation.rb', line 59

def collect_statuses(client = Twitter)
  res = client.__send__(type, target, opts)
  unless res.empty?
    self.opts = opts.merge(:since_id => res.first.id)
    save
  end
  res
end

#run(client = Twitter) ⇒ Object

Collect tweet statuses and save them Return value should not be used



72
73
74
75
76
77
78
79
80
# File 'lib/murmuring_spider/operation.rb', line 72

def run(client = Twitter)
  collect_statuses(client).each do |s|

    # not to raise error on save, remove an invalid status beforehand
    last = self.statuses.new(s)
    self.statuses.pop unless last.valid?
  end
  save
end