nervion 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -3,6 +3,6 @@
3
3
  .yardoc
4
4
  coverage
5
5
  doc
6
- examples
6
+ examples_with_keys
7
7
  Gemfile.lock
8
8
  pkg
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # version 0.0.4
2
+
3
+ - Nervion won't raise an exception if you try to stop it and it wasn't running
4
+ - Added `running?` method to check whether Nervion is running or not
5
+
1
6
  # version 0.0.3
2
7
 
3
8
  - Allow Nervion to run without being responsible for EventMachine's event loop
data/README.md CHANGED
@@ -177,10 +177,42 @@ end
177
177
 
178
178
  ## EventMachine Integration
179
179
 
180
- Nervion runs on the top of EventMachine.
180
+ Nervion runs on the top of EventMachine. This means that you can take advantage
181
+ of any of the features of the EventMachine ecosystem in your Nervion callbacks.
181
182
 
182
- In the near future this `README` will provide a guideline to take advantage of
183
- the benefits that EventMachine can provide when used correctly.
183
+ Nervion can be run insinde an instance of EventMachine that is already running
184
+ or you can let Nervion handle the event loop for you.
185
+
186
+ With that purpose in mind Nervion provides a few handy methods:
187
+
188
+ ### stop
189
+
190
+ The `stop` method will stop both the streaming and EventMachine.
191
+
192
+ ```ruby
193
+ Nervion.stop
194
+ ```
195
+
196
+ ### close_stream
197
+
198
+ You can use `close_stream` to close the connection to the streaming API but
199
+ keep EventMachine's event loop running.
200
+
201
+ ```ruby
202
+ Nervion.close_stream
203
+ ```
204
+
205
+ ### running?
206
+
207
+ The `running?` method will allow you to check whether Nervion is already
208
+ running or not, what, in the asyncronous land that EventMachine lives in, you
209
+ may not be sure about.
210
+
211
+ ```ruby
212
+ Nervion.running?
213
+ ```
214
+
215
+ And remember **do not block the event loop**.
184
216
 
185
217
 
186
218
 
@@ -0,0 +1,47 @@
1
+ require 'nervion'
2
+
3
+ RED = "\e[0;31m"
4
+ NO_COLOR = "\e[0m"
5
+
6
+ def print_status(name, status)
7
+ puts "#{RED}#{name.rjust(20)}:#{NO_COLOR} #{status}"
8
+ end
9
+
10
+ # Setup your own keys here
11
+ Nervion.configure do |config|
12
+ config.consumer_key = ''
13
+ config.consumer_secret = ''
14
+ config.access_token = ''
15
+ config.access_token_secret = ''
16
+ end
17
+
18
+ EM.run do
19
+ @count = 0
20
+
21
+ # This is tracking every tweet that includes the string "madrid" OR any tweet
22
+ # that is geo-located in Madrid.
23
+ Nervion.filter(track: 'madrid', locations: '40.364,-3.760,40.365,-3.609') do |status|
24
+ print_status status[:user][:screen_name], status[:text] if status.has_key? :text
25
+ @count += 1
26
+ end
27
+
28
+ # Check the count of streamed statuses every 2 seconds and switch to sample
29
+ # stream after getting 20. Stop after getting 5 statuses through the sample
30
+ # endpoint.
31
+ EM.add_periodic_timer(2) do
32
+ STDERR.puts "Checking the number of tweets: #{@count}"
33
+ if @count > 20
34
+ @count = 0
35
+ STDERR.puts "MORE THAN 20!!!!!! => switching to sample endpoint"
36
+ Nervion.close_stream
37
+
38
+ EM.next_tick do
39
+ Nervion.sample do |status|
40
+ @count += 1
41
+ STDERR.puts @count
42
+ Nervion.stop if @count >= 5
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,13 @@
1
+ require 'nervion'
2
+
3
+ # Setup your own keys here
4
+ Nervion.configure do |config|
5
+ config.consumer_key = ''
6
+ config.consumer_secret = ''
7
+ config.access_token = ''
8
+ config.access_token_secret = ''
9
+ end
10
+
11
+ # This will cause a 403: User not in role message unless you have the level of
12
+ # access required to stream the firehose endpoint.
13
+ Nervion.firehose { |status| puts status }
@@ -0,0 +1,32 @@
1
+ require 'nervion'
2
+
3
+ CYAN = "\e[0;36m"
4
+ NO_COLOR = "\e[0m"
5
+
6
+ def print_non_status(text)
7
+ puts "#{CYAN}#{text}#{NO_COLOR}"
8
+ end
9
+
10
+ # Setup your own keys here
11
+ Nervion.configure do |config|
12
+ config.consumer_key = ''
13
+ config.consumer_secret = ''
14
+ config.access_token = ''
15
+ config.access_token_secret = ''
16
+ end
17
+
18
+ @deleted_count = 0
19
+ # Calculate the percentage of delete statuses streamed through the sample endpoint.
20
+ Nervion.sample(stall_warnings: true) do |status|
21
+ @count += 1
22
+ if status.has_key? :delete
23
+ print_non_status status
24
+ @deleted_count += 1
25
+ end
26
+
27
+ if @count >= 1000
28
+ Nervion.stop
29
+ percentage = (@deleted_count * 100.0) / @count
30
+ puts "#@deleted_count out of #@count were deleted tweets (#{percentage.round(2)}%)"
31
+ end
32
+ end
@@ -90,16 +90,19 @@ module Nervion
90
90
 
91
91
  # Stops streaming and stops EventMachine's event loop
92
92
  def self.stop
93
- check_for_running_client
94
93
  @client.stop
95
94
  end
96
95
 
97
96
  # Stops streaming but keeps EventMachine's event loop running
98
97
  def self.close_stream
99
- check_for_running_client
100
98
  @client.close_stream
101
99
  end
102
100
 
101
+ # @return [boolean] whether Nervion is running or not
102
+ def self.running?
103
+ not @client.nil?
104
+ end
105
+
103
106
  private
104
107
 
105
108
  def self.callback_table
@@ -137,10 +140,6 @@ module Nervion
137
140
  raise "You have to setup a message callback. Please, check out #{MSG_CALLBACK_README_URL}"
138
141
  end
139
142
 
140
- def self.check_for_running_client
141
- raise 'Nervion is not running' if @client.nil?
142
- end
143
-
144
143
  STREAM_API_HOST = 'stream.twitter.com'
145
144
  STREAM_API_PORT = 443
146
145
  SAMPLE_ENDPOINT = "https://#{STREAM_API_HOST}/1/statuses/sample.json"
@@ -149,5 +148,4 @@ module Nervion
149
148
 
150
149
  AUTHENTICATION_README_URL = 'https://github.com/jacegu/nervion#authentication'
151
150
  MSG_CALLBACK_README_URL = 'https://github.com/jacegu/nervion#message-callback'
152
-
153
151
  end
@@ -1,3 +1,3 @@
1
1
  module Nervion
2
- VERSION = "0.0.3"
2
+ VERSION = '0.0.4'
3
3
  end
data/nervion.gemspec CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
5
5
  gem.authors = ["Javier Acero"]
6
6
  gem.email = ["j4cegu@gmail.com"]
7
7
  gem.description = %q{A minimalistic Ruby client for the Public Streams of Twitter Streaming API}
8
- gem.summary = %q{}
8
+ gem.summary = %q{Nervions is an easy tool and lightweight tool for consuming Twitter's Public Streams with an API that mimics the provided endpoints}
9
9
  gem.homepage = "https://github.com/jacegu/nervion"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
@@ -1,10 +1,8 @@
1
1
  require 'nervion/facade'
2
2
 
3
- class Callable; def call; end; end;
4
-
5
3
  describe "Facade that exposes Nervion's API" do
6
4
  let(:callback_table) { mock(:callback_table).as_null_object }
7
- let(:message_callback) { lambda { :message_callback } }
5
+ let(:message_callback) { lambda { :message_callback } }
8
6
  let(:http_callback) { lambda { :http_error_callback } }
9
7
  let(:network_callback) { lambda { :network_error_callback } }
10
8
 
@@ -97,32 +95,23 @@ describe "Facade that exposes Nervion's API" do
97
95
  it_behaves_like 'an endpoint'
98
96
  end
99
97
 
100
- context 'stoping' do
101
- it 'stops the client and the event loop' do
102
- client.should_receive(:stop)
103
- Nervion.sample{}
104
- Nervion.stop
105
- end
106
-
107
- it 'raises an error if it is not streaming' do
108
- Nervion.instance_variable_set(:@client, nil)
109
- expect { Nervion.stop }.to raise_error
110
- end
98
+ it 'stops the client and the event loop' do
99
+ client.should_receive(:stop)
100
+ Nervion.sample{}
101
+ Nervion.stop
111
102
  end
112
103
 
113
- context 'closing the stream' do
114
- it 'closes the stream but keeps the event loop running' do
115
- client.should_receive(:close_stream)
116
- Nervion.sample{}
117
- Nervion.close_stream
118
- end
119
-
120
- it 'raises an error if it is not streaming' do
121
- Nervion.instance_variable_set(:@client, nil)
122
- expect { Nervion.close_stream }.to raise_error
123
- end
104
+ it 'closes the stream but keeps the event loop running' do
105
+ client.should_receive(:close_stream)
106
+ Nervion.sample{}
107
+ Nervion.close_stream
124
108
  end
125
109
 
110
+ it 'knows if Nervion is running' do
111
+ Nervion.instance_variable_set('@client', nil)
112
+ Nervion.should_not be_running
113
+ Nervion.sample{}
114
+ Nervion.should be_running
115
+ end
126
116
  end
127
-
128
117
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nervion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-10 00:00:00.000000000 Z
12
+ date: 2012-07-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -156,6 +156,9 @@ files:
156
156
  - LICENSE
157
157
  - README.md
158
158
  - Rakefile
159
+ - examples/filter_endpoint.rb
160
+ - examples/firehose_example.rb
161
+ - examples/sample_endpoint.rb
159
162
  - features/callbacks.feature
160
163
  - features/client_validation.feature
161
164
  - features/step_definitions/client_validation_steps.rb
@@ -215,7 +218,8 @@ rubyforge_project:
215
218
  rubygems_version: 1.8.24
216
219
  signing_key:
217
220
  specification_version: 3
218
- summary: ''
221
+ summary: Nervions is an easy tool and lightweight tool for consuming Twitter's Public
222
+ Streams with an API that mimics the provided endpoints
219
223
  test_files:
220
224
  - features/callbacks.feature
221
225
  - features/client_validation.feature