2012-05-25 3 views
1

Моя проблема с заголовками запроса.Могу ли я вернуть список субредадов, используя HTTParty?

Это небольшой скрипт, я состряпал, который не работает:

require 'httparty' 
require 'highline/import' 

#Login.new.post lets us log in. Everything works up to the subreddits method. 

class Login 
    include HTTParty 
    base_uri 'www.reddit.com' 
#So far, so good. 

    def post(username, password) 
    options = {:body => {:user => username, :passwd => password, :api_type => 'json'}} 
    self.class.post("/api/login/#{username}", options) 
    end 

#I think this part is right, in and of itself. The problem is in how the headers 
#are composed (which is why I keep bonking my head on the wall.) 
    def subreddits(headers) 
    self.class.get("/reddits/mine", headers) 
    end 

    def cli_user_login(user, password) 
    a = Login.new.post(user, password); 
    puts a; 
    #We logged in and got some JSON with an empty errors array and, nestled deep, a cookie. 
    reddit_session = a["json"]["data"]["cookie"] 
    #Just putting the cookie in a variable. Print it to make sure it's there: 
    puts reddit_session 
    #reddit_session displays, no problem. We HAVE the cookie. 
    b = Login.new.subreddits("headers" => {"reddit_session" => reddit_session}) 
    puts b 
    #Then suddenly it spits out a mountain of html....... but not the subreddits! :(
    end 

end 


##The following is just for testing, because I'm whimsical or stupid or something 
class IWouldLikeToPlayAGame 
    def initialize 
    puts "PLEASE ETNER YOUR REDDIT CRITERIALS" 
    a = gets.chomp.strip 
    b = ask("PLEASE BE ENTERING YOUR RREDDIT password"){ |q| q.echo = false } 

    Login.new.cli_user_login(a, b) 
    end 
end 

IWouldLikeToPlayAGame.new 

Я думаю, что моя ошибка вокруг линии 30. Мой заголовок хэш выглядит следующим образом:

{"headers" => {"reddit_session" => cookie}} 

GET reddit.com/reddits/mine.xml с этот заголовок и я должны имеют листинг субредада, правильно?

ответ

0

Прежде всего, необходимо указать расширение формата для данных, которые вы хотите получить, xml или json:

/reddits/mine.extension 

Во-вторых, вам нужно отправить reddit_session в печенье хэша, а не заголовки:

:cookies => {'reddit_session' => reddit_session} 

Точки с запятой в рубине являются необязательными, их лучше опустить.

+0

Я провешу это сегодня вечером. Спасибо за ответ! – gryfft

 Смежные вопросы

  • Нет связанных вопросов^_^