Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to comment and reply to comments, post images to gallery #2

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/imgur/client.rb
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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
Expand All @@ -24,15 +25,18 @@ class Imgur::Client < Cistern::Service
collection :comments
request :get_comments
request :get_comment
request :add_comment
request :add_comment_reply

model :basic_response
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")
Expand All @@ -43,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
Expand All @@ -57,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
Expand All @@ -74,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",
Expand Down
7 changes: 7 additions & 0 deletions lib/imgur/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,4 +15,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
16 changes: 16 additions & 0 deletions lib/imgur/models/comments.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions lib/imgur/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ 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"]
connection.comments.get(data["id"])
end

def add_to_gallery(options = {})
connection.add_to_gallery(id, options)
end
end
1 change: 1 addition & 0 deletions lib/imgur/models/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ def upload(options={})
data = connection.upload_image(options).body["data"]
connection.images.new(data)
end

end
13 changes: 13 additions & 0 deletions lib/imgur/requests/add_comment.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions lib/imgur/requests/add_comment_reply.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions lib/imgur/requests/add_to_gallery.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions lib/imgur/requests/get_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Imgur::Client
class Real
def get_comment(id)
path = "/comment/#{id}"

request(
:method => :get,
:path => path,
)
end
end
end
12 changes: 12 additions & 0 deletions lib/imgur/requests/get_comments.rb
Original file line number Diff line number Diff line change
@@ -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