class Tumblr4r::XMLConnection
Tumblr XML API への薄いラッパー。 Rubyオブジェクトからの変換やRubyオブジェクトへの変換などは Parserクラスで行う。Parserクラスへの依存関係は一切持たない。
Attributes
authenticated[RW]
group[RW]
logger[RW]
Public Class Methods
new(http_or_hostname, email=nil, password=nil, logger = nil)
click to toggle source
# File lib/tumblr4r.rb, line 377 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 but is #{http_or_hostname.class}") 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
Public Instance Methods
authenticate()
click to toggle source
@return true if email and password are valid @raise TumblrError
if email or password is invalid
# File lib/tumblr4r.rb, line 436 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(format_error(response), response) end end
dashboard(options = { })
click to toggle source
# File lib/tumblr4r.rb, line 418 def dashboard(options = { }) 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/dashboard', query_string) logger.debug(response.body) case response when Net::HTTPSuccess return response.body else raise TumblrError.new(format_error(response), response) end end
delete(post_id)
click to toggle source
@param [Integer] post_id
# File lib/tumblr4r.rb, line 470 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 raise TumblrError.new(format_error(response), response) end end
format_error(response)
click to toggle source
# File lib/tumblr4r.rb, line 487 def format_error(response) msg = response.inspect + "\n" response.each{|k,v| msg += "#{k}: #{v}\n"} msg += response.body msg end
get(options = { })
click to toggle source
@param [Hash] options :id, :type, :filter, :tagged, :search, :start, :num
# File lib/tumblr4r.rb, line 400 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})", res) else raise TumblrError.new("unexpected response #{res.inspect}", res) end end
write(options)
click to toggle source
@return [Integer] post_id if success @raise [TumblrError] if fail
# File lib/tumblr4r.rb, line 452 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 raise TumblrError.new(format_error(response), response) end end