Trying to get list of freinds from twitter using httparty(ruby) -
trying list of specific user`s friends twitter.
code -
require 'rubygems' require 'httparty' class twitterdata include httparty base_uri 'http://api.twitter.com/1/' default_params :output => 'json' format :json def self.get_username_data(username) get('statuses/friends.json' , :query => { :screen_name => username }) end end puts "please twitter username - " twitter_username = gets puts twitterdata.get_username_data(twitter_username).inspect
this error getting -
please twitter username - twitter c:/ruby192/lib/ruby/gems/1.9.1/gems/crack-0.1.8/lib/crack/json.rb:14:in `rescue in parse': invalid json string (crack::parseerror) c:/ruby192/lib/ruby/gems/1.9.1/gems/crack-0.1.8/lib/crack/json.rb:12:in `parse' c:/ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:116:in `json' c:/ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:136:in `parse_supported_format' c:/ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:103:in `parse' c:/ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:66:in `call' c:/ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:180:in `parse_response' c:/ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:164:in `handle_response' c:/ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:57:in `perform' c:/ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty.rb:280:in `perform_request' c:/ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty.rb:232:in `get' twitter_friends_2.rb:11:in `get_username_data' twitter_friends_2.rb:17:in `<main>'
i new ruby,so sorry if newbie question.
, yosy.
replace method this
def self.get_username_data(username) get("/statuses/friends.json?screen_name=#{username}") end
or
def self.get_username_data(username) get('/statuses/friends.json' , :query => { :screen_name => username.strip }) end
you need strip username because command line when user enters twitter username , hits enter key "\n" gets appended username , same username sent parameter causes problem. of above given code snippets should work.
Comments
Post a Comment