Class: Messed::Interface::Adapter::TwitterSearch

Inherits:
TwitterConsumer show all
Defined in:
lib/messed/interface/adapter/twitter_search.rb

Constant Summary

Constants inherited from Messed::Interface::Adapter

Registry

Instance Attribute Summary collapse

Attributes inherited from TwitterConsumer

#errors, #last_error, #last_ok, #last_status, #started_at

Attributes inherited from Messed::Interface::Adapter

#interface

Instance Method Summary collapse

Methods inherited from TwitterConsumer

#init, #status, #type

Methods inherited from Messed::Interface::Adapter

for_name, #init, #initialize, #load_state, register_for_name, #save_state, #send, #state_file, #type

Methods included from Logger::LoggingModule

included, #logger, #logger=

Constructor Details

This class inherits a constructor from Messed::Interface::Adapter

Instance Attribute Details

#packets_processedObject (readonly)

Returns the value of attribute packets_processed.



9
10
11
# File 'lib/messed/interface/adapter/twitter_search.rb', line 9

def packets_processed
  @packets_processed
end

Instance Method Details

#build_queryObject



11
12
13
# File 'lib/messed/interface/adapter/twitter_search.rb', line 11

def build_query
  Rack::Utils.build_query(interface.configuration.options[:fetch][:query])
end

#default_idsObject



76
77
78
79
80
81
# File 'lib/messed/interface/adapter/twitter_search.rb', line 76

def default_ids
  store_ids? ? read_ids : []
rescue 
  logger.error "Twitter Search: Unable to load ids from #{id_retention_file.inspect}"
  []
end

#id_retention_countObject



54
55
56
# File 'lib/messed/interface/adapter/twitter_search.rb', line 54

def id_retention_count
  interface.configuration.options[:id_retention_size] || 500
end

#id_retention_fileObject



83
84
85
# File 'lib/messed/interface/adapter/twitter_search.rb', line 83

def id_retention_file
  interface.configuration.options[:id_retention_tmp_file]
end

#read_idsObject



72
73
74
# File 'lib/messed/interface/adapter/twitter_search.rb', line 72

def read_ids
  @ids = JSON.parse(File.read(id_retention_file))
end

#result_to_message(result) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/messed/interface/adapter/twitter_search.rb', line 87

def result_to_message(result)
  unless @test_set.include?(result['id'])
    message = Message::Twitter.new do |m|
      m.body = result['text']
      m.from = result['from_user']
      m.to = result['to_user_id']
      m.created_at = Time.rfc2822(result['created_at'])
      m.profile_image_url = result['profile_image_url']
      m.id = result['id']
      m.geo = result['geo']
      m.from_user_id = result['from_user_id']
      m.iso_language_code = result['iso_language_code']
      m.source = result['source']
    end
    @ids << message.id
    @packets_processed += 1
    interface.application.incoming << message
    logger.debug "Twitter Search: Adding message #{message.id}: #{message.body} to incoming queue"
  end
end

#startObject



15
16
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
# File 'lib/messed/interface/adapter/twitter_search.rb', line 15

def start
  @ids ||= default_ids
  # do work.
  begin
    query = build_query
    logger.debug "Twitter Search -> #{query.inspect}"
    http = EventMachine::HttpRequest.new("http://#{interface.configuration.options[:fetch][:host]}/#{interface.configuration.options[:fetch][:path]}").
      get(:query => query, :timeout => 30)
    http.callback {
      self.last_status = http.response_header.status
      case http.response_header.status
        when 200
          self.last_ok = Time.new
          data = JSON.parse(http.response)
          interface.configuration.options[:fetch][:query][:since_id] = data['max_id'] if interface.configuration.options[:fetch][:query]
          @test_set = Set.new(@ids)
          data['results'].each do |result|
            result_to_message(result)
          end
          trim_ids
      end
      EM.add_timer(interface.configuration.options[:interval]) do
        start
      end
    }
    http.errback {
      self.errors += 1
      self.last_error = Time.new
      EM.add_timer(interface.configuration.options[:interval]) do
        start
      end
    }
  rescue RuntimeError
    EM.add_timer(interface.configuration.options[:interval]) do
      start
    end
  end
end

#store_ids?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/messed/interface/adapter/twitter_search.rb', line 64

def store_ids?
  id_retention_file
end

#trim_idsObject



58
59
60
61
62
# File 'lib/messed/interface/adapter/twitter_search.rb', line 58

def trim_ids
  while @ids.size > id_retention_count
    @ids.shift
  end
end

#write_idsObject



68
69
70
# File 'lib/messed/interface/adapter/twitter_search.rb', line 68

def write_ids
  File.open(id_retention_file, 'w') { |f| f << @ids.to_json }
end