lib/tumblr/post.rb in tumblr_client-0.6.11 vs lib/tumblr/post.rb in tumblr_client-0.7.0
- old
+ new
@@ -1,113 +1,110 @@
module Tumblr
- class Client
- module Post
-
- @@standard_post_options = [:state, :tags, :tweet, :date, :markdown, :slug, :format]
-
- def edit(blog_name, options={})
- post("v2/blog/#{blog_name}/post/edit", options)
- end
+ module Post
- #Don't be lazy and create a nice interface
- def reblog(blog_name, options={})
- post("v2/blog/#{blog_name}/post/reblog", options)
- end
-
- def delete(blog_name, id)
- post("v2/blog/#{blog_name}/post/delete", {:id => id})
- end
+ STANDARD_POST_OPTIONS = [:state, :tags, :tweet, :date, :markdown, :slug, :format]
- def photo(blog_name, options={})
- valid_opts = @@standard_post_options + [:caption, :link, :data, :source, :photoset_layout]
- 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?(:source) && options[:source].kind_of?(Array)
- count = 0
- options[:source].each do |src|
- options["source[#{count}]"] = src
- count += 1
- end
- options.delete(:source)
- 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
- end
- post("v2/blog/#{blog_name}/post", options)
- end
- end
-
- def quote(blog_name, 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 edit(blog_name, options = {})
+ post("v2/blog/#{blog_name}/post/edit", options)
+ end
- def text(blog_name, 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 reblog(blog_name, options = {})
+ post("v2/blog/#{blog_name}/post/reblog", options)
+ end
- def link(blog_name, 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 delete(blog_name, id)
+ post("v2/blog/#{blog_name}/post/delete", :id => id)
+ end
- def chat(blog_name, options={})
- valid_opts = @@standard_post_options + [:title, :conversation]
- if valid_options(valid_opts, options)
- options[:type] = "chat"
- post("v2/blog/#{blog_name}/post", options)
+ def photo(blog_name, options = {})
+ valid_opts = STANDARD_POST_OPTIONS + [:caption, :link, :data, :data_raw, :source, :photoset_layout]
+ validate_options(valid_opts, options)
+ validate_no_collision options, [:data, :source]
+
+ # Allow source to be passed as an Array
+ if options.has_key?(:source) && options[:source].kind_of?(Array)
+ options[:source].each.with_index do |src, idx|
+ options["source[#{idx}]"] = src
end
+ options.delete(:source)
end
- def audio(blog_name, 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)
+ options[:type] = 'photo'
+ extract_data!(options)
+ post("v2/blog/#{blog_name}/post", options)
+ end
+
+ def quote(blog_name, options = {})
+ valid_opts = STANDARD_POST_OPTIONS + [:quote, :source]
+ validate_options(valid_opts, options)
+
+ options[:type] = 'quote'
+ post("v2/blog/#{blog_name}/post", options)
+ end
+
+ def text(blog_name, options = {})
+ valid_opts = STANDARD_POST_OPTIONS + [:title, :body]
+ validate_options(valid_opts, options)
+
+ options[:type] = 'text'
+ post("v2/blog/#{blog_name}/post", options)
+ end
+
+ def link(blog_name, options = {})
+ valid_opts = STANDARD_POST_OPTIONS + [:title, :url, :description]
+ validate_options(valid_opts, options)
+
+ options[:type] = 'link'
+ post("v2/blog/#{blog_name}/post", options)
+ end
+
+ def chat(blog_name, options = {})
+ valid_opts = STANDARD_POST_OPTIONS + [:title, :conversation]
+ validate_options(valid_opts, options)
+
+ options[:type] = 'chat'
+ post("v2/blog/#{blog_name}/post", options)
+ end
+
+ def audio(blog_name, options = {})
+ valid_opts = STANDARD_POST_OPTIONS + [:data, :data_raw, :caption, :external_url]
+ validate_options(valid_opts, options)
+ validate_no_collision options, [:data, :external_url]
+
+ options[:type] = 'audio'
+ extract_data!(options)
+ post("v2/blog/#{blog_name}/post", options)
+ end
+
+ def video(blog_name, options = {})
+ valid_opts = STANDARD_POST_OPTIONS + [:data, :data_raw, :embed, :caption]
+ validate_options(valid_opts, options)
+ validate_no_collision options, [:data, :embed]
+
+ options[:type] = 'video'
+ extract_data!(options)
+ post("v2/blog/#{blog_name}/post", options)
+ end
+
+ private
+
+ # Look for the various ways that data can be passed, and normalize
+ # the result in this hash
+ def extract_data!(options)
+ validate_no_collision options, [:data, :data_raw]
+ if options.has_key?(:data)
+ data = options.delete :data
+ data = [data] unless Array === data
+ data.each.with_index do |filepath, idx|
+ options["data[#{idx}]"] = File.open(filepath, 'rb').read
end
- end
-
- def video(blog_name, 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)
+ elsif options.has_key?(:data_raw)
+ data_raw = options.delete :data_raw
+ data_raw = [data_raw] unless Array === data_raw
+ data_raw.each.with_index do |dr, idx|
+ options["data[#{idx}]"] = dr
end
end
end
- end
+
+ end
end