Trying to get list of freinds from twitter using httparty(ruby)

I am trying to get the list of specific user`s friends from twitter. This is my 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 your twitter username - "
twitter_username = gets
puts TwitterData.get_username_data(twitter_username).inspect

This is the error I am getting -

Please your 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)
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/crack-0.1.8/lib/crack/json.rb:12:in `parse'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:116:in `json'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:136:in `parse_supported_format'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:103:in `parse'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/parser.rb:66:in `call'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:180:in `parse_response'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:164:in `handle_response'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty/request.rb:57:in `perform'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty.rb:280:in `perform_request'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/httparty-0.6.1/lib/httparty.rb:232:in `get'
        from twitter_friends_2.rb:11:in `get_username_data'
        from twitter_friends_2.rb:17:in `<main>'

I am new to ruby,So I am sorry if this is a newbie question. Thanks , Yosy.

Answers


Replace your method with 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 to strip the username because from command line when user enters the twitter username and hits enter key a "\n" gets appended to the username and the same username is sent as a parameter which causes the problem. Any of the above given code snippets should work.


Need Your Help

QResource::registerResource fails on Mac version

osx qt

I have the following code that is working fine in my qt program (windows version):

Protocol for Web Description Resources

semantic-web powder

Is POWDER (http://www.w3.org/TR/powder-dr/) meant to be used for Trust/ Proof layers of the Semantic Web? Are there any other w3c standards made for the same layers?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.