Class: Tumblr4r::XMLConnection
- Inherits:
-
Object
- Object
- Tumblr4r::XMLConnection
- Defined in:
- lib/tumblr4r.rb
Overview
Tumblr XML API への薄いラッパー。 Rubyオブジェクトからの変換やRubyオブジェクトへの変換などは Parserクラスで行う。Parserクラスへの依存関係は一切持たない。
Instance Attribute Summary collapse
-
#authenticated ⇒ Object
Returns the value of attribute authenticated.
-
#group ⇒ Object
Returns the value of attribute group.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
-
#authenticate ⇒ Object
True if email and password are valid.
- #delete(post_id) ⇒ Object
- #get(options = { }) ⇒ Object
-
#initialize(http_or_hostname, email = nil, password = nil, logger = nil) ⇒ XMLConnection
constructor
A new instance of XMLConnection.
-
#write(options) ⇒ Integer
Post_id if success.
Constructor Details
#initialize(http_or_hostname, email = nil, password = nil, logger = nil) ⇒ XMLConnection
Returns a new instance of XMLConnection.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/tumblr4r.rb', line 289 def initialize(http_or_hostname, email=nil, password=nil, logger = nil) case http_or_hostname when String @conn = Net::HTTP.new(http_or_hostname) when Net::HTTP @conn = http_or_hostname else raise ArgumentError.new("http_or_hostname must be String or Net::HTTP") end @email= email @password = password if @email && @password begin @authenticated = authenticate rescue TumblrError @authenticated = false end end @group = @conn.address @logger = logger || Logger.new(STDERR) end |
Instance Attribute Details
#authenticated ⇒ Object
Returns the value of attribute authenticated.
288 289 290 |
# File 'lib/tumblr4r.rb', line 288 def authenticated @authenticated end |
#group ⇒ Object
Returns the value of attribute group.
288 289 290 |
# File 'lib/tumblr4r.rb', line 288 def group @group end |
#logger ⇒ Object
Returns the value of attribute logger.
288 289 290 |
# File 'lib/tumblr4r.rb', line 288 def logger @logger end |
Instance Method Details
#authenticate ⇒ Object
Returns true if email and password are valid.
332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/tumblr4r.rb', line 332 def authenticate response = nil http = Net::HTTP.new("www.tumblr.com") response = http.post('/api/authenticate', "email=#{CGI.escape(@email)}&password=#{CGI.escape(@password)}") case response when Net::HTTPOK return true else raise TumblrError.new(response.inspect + "\n" + response.body) end end |
#delete(post_id) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/tumblr4r.rb', line 369 def delete(post_id) raise TumblrError.new("email or password is invalid") unless authenticated response = nil http = Net::HTTP.new("www.tumblr.com") params = {"post-id" => post_id, "email" => @email, "password" => @password, "group" => @group} query_string = params.delete_if{|k,v| v == nil }.map{|k,v| "#{k}=#{CGI.escape(v.to_s)}" unless v.nil?}.join("&") logger.debug("#### query_string: #{query_string}") response = http.post('/api/delete', query_string) case response when Net::HTTPSuccess logger.debug("#### response: #{response.code}: #{response.body}") return true else msg = response.inspect + "\n" response.each{|k,v| msg += "#{k}: #{v}\n"} msg += response.body raise TumblrError.new(msg) end end |
#get(options = { }) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/tumblr4r.rb', line 312 def get( = { }) params = .map{|k, v| "#{k}=#{v}" }.join("&") req = "/api/read?#{params}" logger.info(req) res = @conn.get(req) logger.debug(res.body) case res when Net::HTTPOK return res.body when Net::HTTPNotFound raise TumblrError.new("no such site(#{@hostname})") else raise TumblrError.new("unexpected response #{res.inspect}") end end |
#write(options) ⇒ Integer
Returns post_id if success.
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/tumblr4r.rb', line 348 def write() raise TumblrError.new("email or password is invalid") unless authenticated response = nil http = Net::HTTP.new("www.tumblr.com") params = .merge({"email" => @email, "password" => @password, "group" => @group}) query_string = params.delete_if{|k,v| v == nil }.map{|k,v| "#{k}=#{CGI.escape(v.to_s)}" unless v.nil?}.join("&") logger.debug("#### query_string: #{query_string}") response = http.post('/api/write', query_string) case response when Net::HTTPSuccess return response.body.to_i else msg = response.inspect + "\n" response.each{|k,v| msg += "#{k}: #{v}\n"} msg += response.body raise TumblrError.new(msg) end end |