Class: LiveJournal::Request::Req

Inherits:
Object
  • Object
show all
Defined in:
lib/livejournal/request.rb

Overview

Superclass for all LiveJournal requests.

Instance Method Summary collapse

Constructor Details

#initialize(user, mode) ⇒ Req

:nodoc:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/livejournal/request.rb', line 60

def initialize(user, mode)
  @user = user
  @request = { 'mode'          => mode,
               'clientversion' => 'Ruby',
               'ver'           => 1 }
  if user
    challenge = GetChallenge.new.run
    response = Digest::MD5.hexdigest(challenge +
                 Digest::MD5.hexdigest(user.password))
    @request.update({ 'user'           => user.username,
                      'auth_method'    => 'challenge',
                      'auth_challenge' => challenge,
                      'auth_response'  => response })
    @request['usejournal'] = user.usejournal if user.usejournal
  end
  @result = {}
  @verbose = false
  @dryrun = false
end

Instance Method Details

#dryrun!Object



81
# File 'lib/livejournal/request.rb', line 81

def dryrun!; @dryrun = true; end

#dumpresponseObject



100
101
102
# File 'lib/livejournal/request.rb', line 100

def dumpresponse
  @result.keys.sort.each { |key| puts "#{key} -> #{@result[key]}" }
end

#runObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/livejournal/request.rb', line 83

def run
  h = Net::HTTP.new('www.livejournal.com')
  h.set_debug_output $stderr if @verbose
  request = @request.collect { |key, value|
    "#{CGI.escape(key)}=#{CGI.escape(value.to_s)}"
  }.join("&")
  p request if @verbose
  return if @dryrun
  response = h.post('/interface/flat', request, {"User-Agent" => "ljrb/0.3.x"})
  data = response.read_body
  parseresponse(data)
  dumpresponse if @verbose
  if @result['success'] != "OK"
    raise ProtocolException, @result['errmsg']
  end
end

#verbose!Object



80
# File 'lib/livejournal/request.rb', line 80

def verbose!; @verbose = true; end