Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 20th, 2010, 04:42 AM
Rolandko Rolandko is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2010
Location: Arnhem NLD
Posts: 2 Rolandko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 45 sec
Reputation Power: 0
Twitter Streamin API crash

Hello All,

I want to write a Ruby script that uses the Twitter Streaming API and loads the results into MySQL. The base is ready, I can display the results on screen, but some how after a few lines of tweets, the scripts end with:


2mysqlorg:60: undefined method `[]' for nil:NilClass (NoMethodError)
from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:479:in `call'
from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:479:in `on_decoded_body_data'
from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:472:in `on_body_data'
from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:786:in `process_chunk_body'
from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:534:in `dispatch'
from /var/lib/gems/1.8/gems/em-http-request-0.2.15/lib/em-http/client.rb:460:in `receive_data'
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
from 2mysqlorg:40

This is the script:

Code:
require 'rubygems'
require 'em-http'
require 'json'
require 'mysql'

class LanguageDetector
  URL = "http://www.google.com/uds/GlangDetect"

  include EM::Deferrable

  def initialize(text)
    request = EM::HttpRequest.new(URL).get({
      :query => {:v => "1.0", :q => text}
    })

    # This is called if the request completes successfully (whatever the code)
    request.callback {
      if request.response_header.status == 200
        info = JSON.parse(request.response)["responseData"]
        if info['isReliable']
          self.succeed(info['language'])
        else
          self.fail("Language could not be reliably determined")
        end
      else
        self.fail("Call to fetch language failed")
      end
    }

    # This is called if the request totally failed
    request.errback {
      self.fail("Error making API call")
    }
  end
end

# connect to the MySQL server
dbh = Mysql.real_connect("localhost", "MyUser", "MyPass", "MyDB")

EM.run {
  username = 'MyTwitterUsername'
  password = 'MyTwitterPassword'
  buffer = ""

  http = EventMachine::HttpRequest.new('http://stream.twitter.com/1/statuses/sample.json').post({
    :head => { 'Authorization' => [ username, password ] }
  })

  http.callback {
    unless http.response_header.status == 200
      puts "Call failed with response code #{http.response_header.status}"
    end
  }

  http.stream do |chunk|
    buffer += chunk
    while line = buffer.slice!(/.+\r\n/)
	if line!=nil
	      tweet = JSON.parse(line)
	      if (tweet['user']['screen_name']!=nil && tweet['text']!=nil)
     			puts tweet['user']['screen_name'] + ": " + tweet['text']
	      end
	end
    end
  end
}


What am I doing wrong? I am a newbie, so I have no clou!

Thank you,

Roland.

Reply With Quote
  #2  
Old December 31st, 2010, 04:51 AM
Rolandko Rolandko is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2010
Location: Arnhem NLD
Posts: 2 Rolandko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 45 sec
Reputation Power: 0
Anyone???

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Twitter Streamin API crash

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap