Class: Tumblr4r::XMLConnection

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

Overview

Tumblr XML API への薄いラッパー。 Rubyオブジェクトからの変換やRubyオブジェクトへの変換などは Parserクラスで行う。Parserクラスへの依存関係は一切持たない。

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#authenticatedObject

Returns the value of attribute authenticated.



288
289
290
# File 'lib/tumblr4r.rb', line 288

def authenticated
  @authenticated
end

#groupObject

Returns the value of attribute group.



288
289
290
# File 'lib/tumblr4r.rb', line 288

def group
  @group
end

#loggerObject

Returns the value of attribute logger.



288
289
290
# File 'lib/tumblr4r.rb', line 288

def logger
  @logger
end

Instance Method Details

#authenticateObject

Returns true if email and password are valid.

Returns:

  • true if email and password are valid

Raises:

  • TumblrError if email or password is invalid



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

Parameters:

  • post_id (Integer)

Raises:



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

Parameters:

  • options (Hash) (defaults to: { })

    :id, :type, :filter, :tagged, :search, :start, :num



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(options = { })
  params = options.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.

Returns:

  • (Integer)

    post_id if success

Raises:



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(options)
  raise TumblrError.new("email or password is invalid") unless authenticated

  response = nil
  http = Net::HTTP.new("www.tumblr.com")
  params = options.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