From e609b91e39ad6d7db80c1f8ff2f597be9ed47fdc Mon Sep 17 00:00:00 2001 From: Jason Spradlin Date: Mon, 14 Oct 2013 11:51:41 -0500 Subject: [PATCH 1/5] Added comments model and requests --- lib/imgur/models/comments.rb | 16 ++++++++++++++++ lib/imgur/requests/get_comment.rb | 12 ++++++++++++ lib/imgur/requests/get_comments.rb | 12 ++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 lib/imgur/models/comments.rb create mode 100644 lib/imgur/requests/get_comment.rb create mode 100644 lib/imgur/requests/get_comments.rb diff --git a/lib/imgur/models/comments.rb b/lib/imgur/models/comments.rb new file mode 100644 index 0000000..1b40eb8 --- /dev/null +++ b/lib/imgur/models/comments.rb @@ -0,0 +1,16 @@ +class Imgur::Client::Comments < Cistern::Collection + include Imgur::PagedCollection + include Imgur::Collection + + model Imgur::Client::Comment + + model_root "data" + model_request :get_comment + + collection_root "data" + collection_request :get_comment + + def get(id) + connection.comments.new(connection.get_comment(id).body["data"]) + end +end diff --git a/lib/imgur/requests/get_comment.rb b/lib/imgur/requests/get_comment.rb new file mode 100644 index 0000000..4667348 --- /dev/null +++ b/lib/imgur/requests/get_comment.rb @@ -0,0 +1,12 @@ +class Imgur::Client + class Real + def get_comment(id) + path = "/comment/#{id}" + + request( + :method => :get, + :path => path, + ) + end + end +end diff --git a/lib/imgur/requests/get_comments.rb b/lib/imgur/requests/get_comments.rb new file mode 100644 index 0000000..fca90e9 --- /dev/null +++ b/lib/imgur/requests/get_comments.rb @@ -0,0 +1,12 @@ +class Imgur::Client + class Real + def get_comments(data) + path = "/gallery/#{data[:type]}/#{data[:id]}/comments/best" + + request( + :method => :get, + :path => path, + ) + end + end +end From d154b7c64366e3f35b857589df1dece34692eb9d Mon Sep 17 00:00:00 2001 From: Jason Spradlin Date: Mon, 14 Oct 2013 12:17:14 -0500 Subject: [PATCH 2/5] Added ability to add comments to images in the gallery --- lib/imgur/client.rb | 1 + lib/imgur/models/image.rb | 5 +++++ lib/imgur/requests/add_comment.rb | 13 +++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 lib/imgur/requests/add_comment.rb diff --git a/lib/imgur/client.rb b/lib/imgur/client.rb index 36e3f22..4650716 100644 --- a/lib/imgur/client.rb +++ b/lib/imgur/client.rb @@ -24,6 +24,7 @@ class Imgur::Client < Cistern::Service collection :comments request :get_comments request :get_comment + request :add_comment model :basic_response collection :basic_responses diff --git a/lib/imgur/models/image.rb b/lib/imgur/models/image.rb index 5d930f4..923e271 100644 --- a/lib/imgur/models/image.rb +++ b/lib/imgur/models/image.rb @@ -32,4 +32,9 @@ def comments data = connection.get_comments(type: type, id: id).body["data"] connection.comments.load(data) end + + def add_comment(comment) + type = is_album ? "album" : "image" + data = connection.add_comment({type: type, id: id}, comment).body["data"] + end end diff --git a/lib/imgur/requests/add_comment.rb b/lib/imgur/requests/add_comment.rb new file mode 100644 index 0000000..5cb7bf6 --- /dev/null +++ b/lib/imgur/requests/add_comment.rb @@ -0,0 +1,13 @@ +class Imgur::Client + class Real + def add_comment(image_data, comment) + path = "/gallery/#{image_data[:type]}/#{image_data[:id]}/comment" + + request( + :method => :post, + :path => path, + :params => {:comment => comment} + ) + end + end +end From d9a30847871ea5114cdf864fa0fe3d8c0b439f2f Mon Sep 17 00:00:00 2001 From: Jason Spradlin Date: Mon, 14 Oct 2013 12:58:01 -0500 Subject: [PATCH 3/5] Adjusted return of "add_comment" to return the actual comment model Added ability to reply to comments --- lib/imgur/client.rb | 1 + lib/imgur/models/comment.rb | 6 ++++++ lib/imgur/models/image.rb | 1 + lib/imgur/requests/add_comment_reply.rb | 13 +++++++++++++ 4 files changed, 21 insertions(+) create mode 100644 lib/imgur/requests/add_comment_reply.rb diff --git a/lib/imgur/client.rb b/lib/imgur/client.rb index 4650716..b880138 100644 --- a/lib/imgur/client.rb +++ b/lib/imgur/client.rb @@ -25,6 +25,7 @@ class Imgur::Client < Cistern::Service request :get_comments request :get_comment request :add_comment + request :add_comment_reply model :basic_response collection :basic_responses diff --git a/lib/imgur/models/comment.rb b/lib/imgur/models/comment.rb index 46046e3..97360a0 100644 --- a/lib/imgur/models/comment.rb +++ b/lib/imgur/models/comment.rb @@ -14,4 +14,10 @@ class Imgur::Client::Comment < Imgur::Model attribute :parent_id, type: :integer attribute :deleted, type: :boolean attribute :children, type: :array + + def reply(reply) + image_type = on_album ? "album" : "image" + data = connection.add_comment_reply({image_type: image_type, image_id: image_id, comment_id: id}, reply).body["data"] + connection.comments.get(data["id"]) + end end diff --git a/lib/imgur/models/image.rb b/lib/imgur/models/image.rb index 923e271..800093b 100644 --- a/lib/imgur/models/image.rb +++ b/lib/imgur/models/image.rb @@ -36,5 +36,6 @@ def comments def add_comment(comment) type = is_album ? "album" : "image" data = connection.add_comment({type: type, id: id}, comment).body["data"] + connection.comments.get(data["id"]) end end diff --git a/lib/imgur/requests/add_comment_reply.rb b/lib/imgur/requests/add_comment_reply.rb new file mode 100644 index 0000000..44a666c --- /dev/null +++ b/lib/imgur/requests/add_comment_reply.rb @@ -0,0 +1,13 @@ +class Imgur::Client + class Real + def add_comment_reply(data, reply) + path = "/gallery/#{data[:image_type]}/#{data[:image_id]}/comment/#{data[:comment_id]}" + + request( + :method => :post, + :path => path, + :params => {:comment => reply} + ) + end + end +end From 194d2f7185eebeb7e1f4936fc7e9c99a1482a094 Mon Sep 17 00:00:00 2001 From: Jason Spradlin Date: Mon, 14 Oct 2013 14:06:20 -0500 Subject: [PATCH 4/5] Added functionality to add uploaded images to the gallery --- lib/imgur/client.rb | 1 + lib/imgur/models/image.rb | 4 ++++ lib/imgur/models/images.rb | 1 + lib/imgur/requests/add_to_gallery.rb | 14 ++++++++++++++ 4 files changed, 20 insertions(+) create mode 100644 lib/imgur/requests/add_to_gallery.rb diff --git a/lib/imgur/client.rb b/lib/imgur/client.rb index b880138..27e2cf5 100644 --- a/lib/imgur/client.rb +++ b/lib/imgur/client.rb @@ -9,6 +9,7 @@ class Imgur::Client < Cistern::Service request :get_images request :upload_image request :delete_image + request :add_to_gallery model :album collection :albums diff --git a/lib/imgur/models/image.rb b/lib/imgur/models/image.rb index 800093b..6249096 100644 --- a/lib/imgur/models/image.rb +++ b/lib/imgur/models/image.rb @@ -38,4 +38,8 @@ def add_comment(comment) data = connection.add_comment({type: type, id: id}, comment).body["data"] connection.comments.get(data["id"]) end + + def add_to_gallery(options = {}) + connection.add_to_gallery(id, options) + end end diff --git a/lib/imgur/models/images.rb b/lib/imgur/models/images.rb index 0ef39ca..658060d 100644 --- a/lib/imgur/models/images.rb +++ b/lib/imgur/models/images.rb @@ -38,4 +38,5 @@ def upload(options={}) data = connection.upload_image(options).body["data"] connection.images.new(data) end + end diff --git a/lib/imgur/requests/add_to_gallery.rb b/lib/imgur/requests/add_to_gallery.rb new file mode 100644 index 0000000..cb130cb --- /dev/null +++ b/lib/imgur/requests/add_to_gallery.rb @@ -0,0 +1,14 @@ +class Imgur::Client + class Real + def add_to_gallery(id, options={}) + path = "/gallery/#{id}" + + request( + :method => :post, + :path => path, + :params => options + ) + end + end + +end From 72a92113f381d130c0ad204e72a716db635a2c40 Mon Sep 17 00:00:00 2001 From: Jason Spradlin Date: Mon, 14 Oct 2013 14:44:50 -0500 Subject: [PATCH 5/5] Added custom config file functionality (previously was saving over .imgurrc no matter what) Added field for comment.comment (Imgur api docs incorrectly state it is comment.caption) --- lib/imgur/client.rb | 13 +++++++------ lib/imgur/models/comment.rb | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/imgur/client.rb b/lib/imgur/client.rb index 27e2cf5..ebdab27 100644 --- a/lib/imgur/client.rb +++ b/lib/imgur/client.rb @@ -1,5 +1,5 @@ class Imgur::Client < Cistern::Service - @recognized_arguments = [:config] + @recognized_arguments = [:config, :config_path] model_path "imgur/models" request_path "imgur/requests" @@ -32,10 +32,11 @@ class Imgur::Client < Cistern::Service collection :basic_responses class Real - attr_accessor :url, :path, :parser, :logger, :config, :authorize_path, :token_path, :connection + attr_accessor :url, :path, :parser, :logger, :config, :authorize_path, :token_path, :connection, :config_path def initialize(options={}) - @config = options[:config] || YAML.load_file(File.expand_path("~/.imgurrc")) || YAML.load_file("config/config.yml") + @config_path = options[:config_path] || '~/.imgurrc' + @config = options[:config] || YAML.load_file(File.expand_path(@config_path)) || YAML.load_file("config/config.yml") @authorize_path = "/oauth2/authorize" @token_path = "/oauth2/token" @url = URI.parse(options[:url] || "https://api.imgur.com") @@ -46,7 +47,7 @@ def initialize(options={}) def reset! @config = nil - @config = YAML.load_file(File.expand_path("~/.imgurrc")) + @config = YAML.load_file(File.expand_path(@config_path)) end def refresh_token @@ -60,7 +61,7 @@ def refresh_token new_params = @parser.load(response) @config[:access_token] = new_params["access_token"] @config[:refresh_token] = new_params["refresh_token"] - File.open(File.expand_path("~/.imgurrc"), "w") { |f| YAML.dump(@config, f) } + File.open(File.expand_path(@config_path), "w") { |f| YAML.dump(@config, f) } self.reset! return true end @@ -77,7 +78,7 @@ def request(options={}) refresh_token = $stdin.gets.strip @config[:access_token] = verifier @config[:refresh_token] = refresh_token - File.open(File.expand_path("~/.imgurrc"), 'w') { |f| YAML.dump(@config, f) } + File.open(File.expand_path(@config_path), 'w') { |f| YAML.dump(@config, f) } end headers = { "Accept" => "application/json", diff --git a/lib/imgur/models/comment.rb b/lib/imgur/models/comment.rb index 97360a0..792d7dc 100644 --- a/lib/imgur/models/comment.rb +++ b/lib/imgur/models/comment.rb @@ -3,6 +3,7 @@ class Imgur::Client::Comment < Imgur::Model attribute :image_id attribute :caption + attribute :comment attribute :author attribute :author_id, type: :integer attribute :on_album, type: :boolean