datasift 1.2.1 → 1.3.0
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/README.md +8 -0
 - data/VERSION +1 -1
 - data/examples/consume-stream.rb +10 -0
 - data/lib/DataSift/stream_consumer.rb +41 -3
 - data/lib/DataSift/stream_consumer_http.rb +16 -5
 - metadata +5 -5
 
    
        data/README.md
    CHANGED
    
    | 
         @@ -52,6 +52,14 @@ more details. 
     | 
|
| 
       52 
52 
     | 
    
         
             
            Changelog
         
     | 
| 
       53 
53 
     | 
    
         
             
            ---------
         
     | 
| 
       54 
54 
     | 
    
         | 
| 
      
 55 
     | 
    
         
            +
            * v.1.3.0 Improved error handling (2012-03-08)
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
              Added onError and onWarning events - see examples/consume-stream.rb for an
         
     | 
| 
      
 58 
     | 
    
         
            +
              example.
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
              Stopped the HTTP consumer from attempting to reconnect when it receives a
         
     | 
| 
      
 61 
     | 
    
         
            +
              4xx response from the server.
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
       55 
63 
     | 
    
         
             
            * v.1.2.0 Twitter Compliance (2012-02-28)
         
     | 
| 
       56 
64 
     | 
    
         | 
| 
       57 
65 
     | 
    
         
             
              The consumer now has an onDeleted method to which you can assign a block
         
     | 
    
        data/VERSION
    CHANGED
    
    | 
         @@ -1 +1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            1. 
     | 
| 
      
 1 
     | 
    
         
            +
            1.3.0
         
     | 
    
        data/examples/consume-stream.rb
    CHANGED
    
    | 
         @@ -37,6 +37,16 @@ consumer.onStopped do |reason| 
     | 
|
| 
       37 
37 
     | 
    
         
             
            	puts
         
     | 
| 
       38 
38 
     | 
    
         
             
            end
         
     | 
| 
       39 
39 
     | 
    
         | 
| 
      
 40 
     | 
    
         
            +
            # Set up the warning event handler.
         
     | 
| 
      
 41 
     | 
    
         
            +
            consumer.onWarning do |message|
         
     | 
| 
      
 42 
     | 
    
         
            +
            	puts 'WARNING: ' + message
         
     | 
| 
      
 43 
     | 
    
         
            +
            end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
            # Set up the error event handler.
         
     | 
| 
      
 46 
     | 
    
         
            +
            consumer.onError do |message|
         
     | 
| 
      
 47 
     | 
    
         
            +
            	puts 'ERROR: ' + message
         
     | 
| 
      
 48 
     | 
    
         
            +
            end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
       40 
50 
     | 
    
         
             
            # And start consuming
         
     | 
| 
       41 
51 
     | 
    
         
             
            puts 'Consuming...'
         
     | 
| 
       42 
52 
     | 
    
         
             
            puts '--'
         
     | 
| 
         @@ -75,6 +75,34 @@ module DataSift 
     | 
|
| 
       75 
75 
     | 
    
         
             
            			end
         
     | 
| 
       76 
76 
     | 
    
         
             
            		end
         
     | 
| 
       77 
77 
     | 
    
         | 
| 
      
 78 
     | 
    
         
            +
            		# This is called when an error message is received.
         
     | 
| 
      
 79 
     | 
    
         
            +
            		# === Parameters
         
     | 
| 
      
 80 
     | 
    
         
            +
            		#
         
     | 
| 
      
 81 
     | 
    
         
            +
            		# * +message+ - The error message.
         
     | 
| 
      
 82 
     | 
    
         
            +
            		#
         
     | 
| 
      
 83 
     | 
    
         
            +
            		def onError(&block)
         
     | 
| 
      
 84 
     | 
    
         
            +
            			if block_given?
         
     | 
| 
      
 85 
     | 
    
         
            +
            				@on_error = block
         
     | 
| 
      
 86 
     | 
    
         
            +
            				self
         
     | 
| 
      
 87 
     | 
    
         
            +
            			else
         
     | 
| 
      
 88 
     | 
    
         
            +
            				@on_error
         
     | 
| 
      
 89 
     | 
    
         
            +
            			end
         
     | 
| 
      
 90 
     | 
    
         
            +
            		end
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 92 
     | 
    
         
            +
            		# This is called when an error message is received.
         
     | 
| 
      
 93 
     | 
    
         
            +
            		# === Parameters
         
     | 
| 
      
 94 
     | 
    
         
            +
            		#
         
     | 
| 
      
 95 
     | 
    
         
            +
            		# * +message+ - The error message.
         
     | 
| 
      
 96 
     | 
    
         
            +
            		#
         
     | 
| 
      
 97 
     | 
    
         
            +
            		def onWarning(&block)
         
     | 
| 
      
 98 
     | 
    
         
            +
            			if block_given?
         
     | 
| 
      
 99 
     | 
    
         
            +
            				@on_warning = block
         
     | 
| 
      
 100 
     | 
    
         
            +
            				self
         
     | 
| 
      
 101 
     | 
    
         
            +
            			else
         
     | 
| 
      
 102 
     | 
    
         
            +
            				@on_warning
         
     | 
| 
      
 103 
     | 
    
         
            +
            			end
         
     | 
| 
      
 104 
     | 
    
         
            +
            		end
         
     | 
| 
      
 105 
     | 
    
         
            +
             
     | 
| 
       78 
106 
     | 
    
         
             
            		# This is called when the consumer is stopped.
         
     | 
| 
       79 
107 
     | 
    
         
             
            		# === Parameters
         
     | 
| 
       80 
108 
     | 
    
         
             
            		#
         
     | 
| 
         @@ -103,10 +131,20 @@ module DataSift 
     | 
|
| 
       103 
131 
     | 
    
         
             
            			# Start consuming
         
     | 
| 
       104 
132 
     | 
    
         
             
            			@state = STATE_STARTING
         
     | 
| 
       105 
133 
     | 
    
         
             
            			onStart do |interaction|
         
     | 
| 
       106 
     | 
    
         
            -
            				if interaction.has_key?(' 
     | 
| 
       107 
     | 
    
         
            -
            					 
     | 
| 
      
 134 
     | 
    
         
            +
            				if interaction.has_key?('status')
         
     | 
| 
      
 135 
     | 
    
         
            +
            					if interaction['status'] == 'error' || interaction['status'] == 'failure'
         
     | 
| 
      
 136 
     | 
    
         
            +
            						onError.call(interaction['message'])
         
     | 
| 
      
 137 
     | 
    
         
            +
            					elsif interaction['status'] == 'warning'
         
     | 
| 
      
 138 
     | 
    
         
            +
            						onWarning.call(interaction['message'])
         
     | 
| 
      
 139 
     | 
    
         
            +
            					else
         
     | 
| 
      
 140 
     | 
    
         
            +
            						# Tick
         
     | 
| 
      
 141 
     | 
    
         
            +
            					end
         
     | 
| 
       108 
142 
     | 
    
         
             
            				else
         
     | 
| 
       109 
     | 
    
         
            -
            					 
     | 
| 
      
 143 
     | 
    
         
            +
            					if interaction.has_key?('deleted') and interaction['deleted']
         
     | 
| 
      
 144 
     | 
    
         
            +
            						onDeleted.call(interaction) unless onDeleted.nil?
         
     | 
| 
      
 145 
     | 
    
         
            +
            					else
         
     | 
| 
      
 146 
     | 
    
         
            +
            						block.call(interaction) unless block.nil?
         
     | 
| 
      
 147 
     | 
    
         
            +
            					end
         
     | 
| 
       110 
148 
     | 
    
         
             
            				end
         
     | 
| 
       111 
149 
     | 
    
         
             
            			end
         
     | 
| 
       112 
150 
     | 
    
         
             
            		end
         
     | 
| 
         @@ -45,7 +45,10 @@ module DataSift 
     | 
|
| 
       45 
45 
     | 
    
         
             
            								end
         
     | 
| 
       46 
46 
     | 
    
         
             
            							else
         
     | 
| 
       47 
47 
     | 
    
         
             
            								# received only part of the chunk, grab the rest
         
     | 
| 
       48 
     | 
    
         
            -
            								 
     | 
| 
      
 48 
     | 
    
         
            +
            								received_data = @socket.read(chunkLeft)
         
     | 
| 
      
 49 
     | 
    
         
            +
            								if not received_data.nil?
         
     | 
| 
      
 50 
     | 
    
         
            +
            									parser << received_data
         
     | 
| 
      
 51 
     | 
    
         
            +
            								end
         
     | 
| 
       49 
52 
     | 
    
         
             
            							end
         
     | 
| 
       50 
53 
     | 
    
         
             
            						end
         
     | 
| 
       51 
54 
     | 
    
         
             
            					else
         
     | 
| 
         @@ -130,16 +133,24 @@ module DataSift 
     | 
|
| 
       130 
133 
     | 
    
         
             
            					if @response_head[:code] == 200
         
     | 
| 
       131 
134 
     | 
    
         
             
            						# Success!
         
     | 
| 
       132 
135 
     | 
    
         
             
            						@state = StreamConsumer::STATE_RUNNING
         
     | 
| 
       133 
     | 
    
         
            -
            					elsif @response_head[:code]  
     | 
| 
       134 
     | 
    
         
            -
            						 
     | 
| 
      
 136 
     | 
    
         
            +
            					elsif @response_head[:code] >= 400 && @response_head[:code] < 500 && @response_head[:code] != 420
         
     | 
| 
      
 137 
     | 
    
         
            +
            						line = ''
         
     | 
| 
      
 138 
     | 
    
         
            +
            						while !@socket.eof? && line.length < 10
         
     | 
| 
      
 139 
     | 
    
         
            +
            							line = @socket.gets
         
     | 
| 
      
 140 
     | 
    
         
            +
            						end
         
     | 
| 
      
 141 
     | 
    
         
            +
            						data = Yajl::Parser.parse(line)
         
     | 
| 
      
 142 
     | 
    
         
            +
            						if data.has_key?('message')
         
     | 
| 
      
 143 
     | 
    
         
            +
            							raise StreamError, data['message']
         
     | 
| 
      
 144 
     | 
    
         
            +
            						else
         
     | 
| 
      
 145 
     | 
    
         
            +
            							raise StreamError, 'Connection refused: ' + @response_head[:code] + ' ' + @response_head[:msg]
         
     | 
| 
      
 146 
     | 
    
         
            +
            						end
         
     | 
| 
       135 
147 
     | 
    
         
             
            					else
         
     | 
| 
       136 
     | 
    
         
            -
            						puts 'Connection failed: ' + @response_head[:code] + ' ' + @response_head[:msg]
         
     | 
| 
       137 
148 
     | 
    
         
             
            						if connection_delay == 0
         
     | 
| 
       138 
149 
     | 
    
         
             
            							connection_delay = 10;
         
     | 
| 
       139 
150 
     | 
    
         
             
            						elsif connection_delay < 240
         
     | 
| 
       140 
151 
     | 
    
         
             
            							connection_delay *= 2;
         
     | 
| 
       141 
152 
     | 
    
         
             
            						else
         
     | 
| 
       142 
     | 
    
         
            -
            							raise StreamError, 'Connection  
     | 
| 
      
 153 
     | 
    
         
            +
            							raise StreamError, 'Connection refused: ' + @response_head[:code] + ' ' + @response_head[:msg]
         
     | 
| 
       143 
154 
     | 
    
         
             
            						end
         
     | 
| 
       144 
155 
     | 
    
         
             
            					end
         
     | 
| 
       145 
156 
     | 
    
         
             
            				#rescue
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,13 +1,13 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification 
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: datasift
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version 
         
     | 
| 
       4 
     | 
    
         
            -
              hash:  
     | 
| 
      
 4 
     | 
    
         
            +
              hash: 27
         
     | 
| 
       5 
5 
     | 
    
         
             
              prerelease: 
         
     | 
| 
       6 
6 
     | 
    
         
             
              segments: 
         
     | 
| 
       7 
7 
     | 
    
         
             
              - 1
         
     | 
| 
       8 
     | 
    
         
            -
              -  
     | 
| 
       9 
     | 
    
         
            -
              -  
     | 
| 
       10 
     | 
    
         
            -
              version: 1. 
     | 
| 
      
 8 
     | 
    
         
            +
              - 3
         
     | 
| 
      
 9 
     | 
    
         
            +
              - 0
         
     | 
| 
      
 10 
     | 
    
         
            +
              version: 1.3.0
         
     | 
| 
       11 
11 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       12 
12 
     | 
    
         
             
            authors: 
         
     | 
| 
       13 
13 
     | 
    
         
             
            - DataSift
         
     | 
| 
         @@ -15,7 +15,7 @@ autorequire: 
     | 
|
| 
       15 
15 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       16 
16 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       17 
17 
     | 
    
         | 
| 
       18 
     | 
    
         
            -
            date: 2012-03- 
     | 
| 
      
 18 
     | 
    
         
            +
            date: 2012-03-08 00:00:00 Z
         
     | 
| 
       19 
19 
     | 
    
         
             
            dependencies: 
         
     | 
| 
       20 
20 
     | 
    
         
             
            - !ruby/object:Gem::Dependency 
         
     | 
| 
       21 
21 
     | 
    
         
             
              name: rest-client
         
     |