lib/tumblr/post.rb in tumblr_client-0.5 vs lib/tumblr/post.rb in tumblr_client-0.6

- old
+ new

@@ -1,9 +1,11 @@ module Tumblr class Client module Post + @@standard_post_options = [:state, :tags, :tweet, :date, :markdown, :slug] + def edit(blog_name, options={}) post("v2/blog/#{blog_name}/post/edit", options) end #Don't be lazy and create a nice interface @@ -14,60 +16,90 @@ def delete(blog_name, id) post("v2/blog/#{blog_name}/post/delete", {:id => id}) end def photo(blog_name, options={}) - options[:type] = "photo" - if options.has_key?(:data) - #Probably can be refactored - if options[:data].kind_of?(Array) - count = 0 - options[:data].each do |filepath| - options["data[#{count}]"] = File.open(filepath, 'rb').read() - count += 1 + valid_opts = @@standard_post_options + [:caption, :link, :data, :source] + if valid_options(valid_opts, options) + options[:type] = "photo" + if (options.has_key?(:data) && options.has_key?(:source)) + raise Exception, "You can only use one parameter, either source or data." + end + if options.has_key?(:data) + #Probably can be refactored + if options[:data].kind_of?(Array) + count = 0 + options[:data].each do |filepath| + options["data[#{count}]"] = File.open(filepath, 'rb').read() + count += 1 + end + options.delete(:data) + else + options[:data] = File.open(options[:data],'rb').read() end - options.delete(:data) - else - options[:data] = File.open(options[:data],'rb').read() end + post("v2/blog/#{blog_name}/post", options) end - post("v2/blog/#{blog_name}/post", options) end def quote(blog_name, options={}) - options[:type] = "quote" - post("v2/blog/#{blog_name}/post", options) + valid_opts = @@standard_post_options + [:quote, :source] + if valid_options(valid_opts, options) + options[:type] = "quote" + post("v2/blog/#{blog_name}/post", options) + end end def text(blog_name, options={}) - options[:type] = "text" - post("v2/blog/#{blog_name}/post", options) + valid_opts = @@standard_post_options + [:title, :body] + if valid_options(valid_opts, options) + options[:type] = "text" + post("v2/blog/#{blog_name}/post", options) + end end def link(blog_name, options={}) - options[:type] = "link" - post("v2/blog/#{blog_name}/post", options) + valid_opts = @@standard_post_options + [:title, :url, :description] + if valid_options(valid_opts, options) + options[:type] = "link" + post("v2/blog/#{blog_name}/post", options) + end end def chat(blog_name, options={}) - options[:type] = "chat" - post("v2/blog/#{blog_name}/post", options) + valid_opts = @@standard_post_options + [:title, :conversation] + if valid_options(valid_opts, options) + options[:type] = "chat" + post("v2/blog/#{blog_name}/post", options) + end end def audio(blog_name, options={}) - options[:type] = "audio" - post("v2/blog/#{blog_name}/post", options) + valid_opts = @@standard_post_options + [:data, :caption, :external_url] + if valid_options(valid_opts, options) + options[:type] = "audio" + if (options.has_key?(:data) && options.has_key?(:external_url)) + raise Exception, "You can only use one parameter, either data or external url." + end + if(options.has_key?(:data)) + options[:data] = File.open(options[:data],'rb').read() + end + post("v2/blog/#{blog_name}/post", options) + end end def video(blog_name, options={}) - options[:type] = "video" - post("v2/blog/#{blog_name}/post", options) + valid_opts = @@standard_post_options + [:data, :embed, :caption] + if valid_options(valid_opts, options) + options[:type] = "video" + if (options.has_key?(:data) && options.has_key?(:embed)) + raise Exception, "You can only use one parameter, either data or embed." + end + if(options.has_key?(:data)) + options[:data] = File.open(options[:data],'rb').read() + end + post("v2/blog/#{blog_name}/post", options) + end end - - def answer(blog_name, options={}) - options[:type] = "answer" - post("v2/blog/#{blog_name}/post", options) - end - end - end + end end