ruby-imgur 0.03 → 0.04
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/imgur/client.rb +98 -39
- data/lib/imgur/models/image.rb +6 -0
- data/lib/imgur/requests/delete_image.rb +18 -0
- data/lib/imgur/requests/get_accounts.rb +7 -0
- data/lib/imgur/requests/get_albums.rb +13 -0
- data/lib/imgur/requests/get_image.rb +8 -0
- data/lib/imgur/requests/get_images.rb +13 -0
- data/lib/imgur/requests/upload_image.rb +30 -1
- data/lib/imgur/version.rb +1 -1
- data/spec/images_spec.rb +14 -2
- data/spec/spec_helper.rb +7 -0
- metadata +2 -2
data/lib/imgur/client.rb
CHANGED
@@ -20,6 +20,11 @@ class Imgur::Client < Cistern::Service
|
|
20
20
|
request :get_accounts
|
21
21
|
request :get_account
|
22
22
|
|
23
|
+
model :comment
|
24
|
+
collection :comments
|
25
|
+
request :get_comments
|
26
|
+
request :get_comment
|
27
|
+
|
23
28
|
model :basic_response
|
24
29
|
collection :basic_responses
|
25
30
|
|
@@ -110,8 +115,94 @@ class Imgur::Client < Cistern::Service
|
|
110
115
|
end
|
111
116
|
|
112
117
|
class Mock
|
118
|
+
def self.random_id
|
119
|
+
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
120
|
+
selection = ''
|
121
|
+
7.times do
|
122
|
+
position = rand(characters.length)
|
123
|
+
selection << characters[position..position]
|
124
|
+
end
|
125
|
+
selection
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.random_number
|
129
|
+
characters = '1234567890'
|
130
|
+
selection = ''
|
131
|
+
7.times do
|
132
|
+
position = rand(characters.length)
|
133
|
+
selection << characters[position..position]
|
134
|
+
end
|
135
|
+
selection.to_i
|
136
|
+
end
|
137
|
+
|
113
138
|
def self.data
|
114
|
-
@data ||=
|
139
|
+
@data ||= begin
|
140
|
+
image_id = random_id
|
141
|
+
account_id = random_number
|
142
|
+
account_name = random_id
|
143
|
+
album_id = random_id
|
144
|
+
comment_id = random_id
|
145
|
+
|
146
|
+
account = {
|
147
|
+
"id" => account_id,
|
148
|
+
"url" => "#{account_name}.imgur.com",
|
149
|
+
"bio" => nil,
|
150
|
+
"reputation" => 435.5,
|
151
|
+
"created" => 1349051610,
|
152
|
+
}
|
153
|
+
|
154
|
+
image = {
|
155
|
+
"id" => image_id,
|
156
|
+
"title" => "test image",
|
157
|
+
"datetime" => 1349051625,
|
158
|
+
"type" => "image/jpeg",
|
159
|
+
"animated" => false,
|
160
|
+
"width" => 2490,
|
161
|
+
"height" => 3025,
|
162
|
+
"size" => 618969,
|
163
|
+
"views" => 625622,
|
164
|
+
"bandwidth" => 387240623718,
|
165
|
+
"ups" => 1889,
|
166
|
+
"downs" => 58,
|
167
|
+
"score" => 18935622,
|
168
|
+
"account_id" => account_id,
|
169
|
+
}
|
170
|
+
|
171
|
+
album = {
|
172
|
+
"id" => album_id,
|
173
|
+
"title" => "test album",
|
174
|
+
"description" => nil,
|
175
|
+
"privacy" => "public",
|
176
|
+
"cover" => image_id,
|
177
|
+
"order" => 0,
|
178
|
+
"layout" => "blog",
|
179
|
+
"datetime" => 1347058832,
|
180
|
+
"link" => "https://imgur.com/a/#{album_id}",
|
181
|
+
"account_id" => account_id,
|
182
|
+
}
|
183
|
+
|
184
|
+
comment = {
|
185
|
+
"id" => comment_id,
|
186
|
+
"image_id" => image_id,
|
187
|
+
"author" => account_name,
|
188
|
+
"author_id" => account_id,
|
189
|
+
"on_album" => false,
|
190
|
+
"ups" => 10,
|
191
|
+
"downs" => 2,
|
192
|
+
"points" => 23.4,
|
193
|
+
"datetime" => 1349051670,
|
194
|
+
"parent_id" => 2,
|
195
|
+
"deleted" => false,
|
196
|
+
"children" => [],
|
197
|
+
}
|
198
|
+
|
199
|
+
{
|
200
|
+
:images => {image_id => image},
|
201
|
+
:accounts => {account_id => account},
|
202
|
+
:albums => {album_id => album},
|
203
|
+
:comments => {comment_id => comment},
|
204
|
+
}
|
205
|
+
end
|
115
206
|
end
|
116
207
|
|
117
208
|
def self.reset!
|
@@ -122,40 +213,16 @@ class Imgur::Client < Cistern::Service
|
|
122
213
|
self.class.data
|
123
214
|
end
|
124
215
|
|
125
|
-
def
|
126
|
-
|
216
|
+
def random_id
|
217
|
+
self.class.random_id
|
127
218
|
end
|
128
219
|
|
129
|
-
def
|
130
|
-
|
131
|
-
uri = URI.parse(params["url"])
|
132
|
-
params = uri.query.split("&").inject({}) { |r,e| k,v = e.qplit("="); r.merge(k => v) }
|
133
|
-
end
|
134
|
-
|
135
|
-
resources = options[:resources] || self.data[collection]
|
136
|
-
page_size = (params["per_page"] || 20).to_i
|
137
|
-
page_index = (params["page"] || 1).to_i
|
138
|
-
total_pages = (resources.size.to_f / page_size.to_f).round
|
139
|
-
offset = (page_index - 1) * page_size
|
140
|
-
links = []
|
141
|
-
|
142
|
-
resource_page = resources.values.reverse.slice(offset, page_size)
|
143
|
-
|
144
|
-
if page_index < total_pages
|
145
|
-
links << url_for_page(collection, page_index - 1, page_size, 'next')
|
146
|
-
end
|
147
|
-
|
148
|
-
if page_index - 1 > 0
|
149
|
-
links << url_for_page(collection, page_index - 1, page_size, 'prev')
|
150
|
-
end
|
151
|
-
|
152
|
-
links << url_for_page(collection, total_pages, page_size, 'last')
|
153
|
-
|
154
|
-
[links.join(", "), resource_page]
|
220
|
+
def credits
|
221
|
+
"980/1000"
|
155
222
|
end
|
156
223
|
|
157
|
-
def
|
158
|
-
|
224
|
+
def initialize(options={})
|
225
|
+
@url = options[:url] || "https://api.imgur.com"
|
159
226
|
end
|
160
227
|
|
161
228
|
def response(options={})
|
@@ -167,13 +234,5 @@ class Imgur::Client < Cistern::Service
|
|
167
234
|
|
168
235
|
Imgur::Response.new(status, headers, body).raise!
|
169
236
|
end
|
170
|
-
|
171
|
-
def url_for(path)
|
172
|
-
File.join(@url, path)
|
173
|
-
end
|
174
|
-
|
175
|
-
def stringify_keys(hash)
|
176
|
-
hash.inject({}) { |r,(k,v)| r.merge(k.to_s => v) }
|
177
|
-
end
|
178
237
|
end
|
179
238
|
end
|
data/lib/imgur/models/image.rb
CHANGED
@@ -26,4 +26,10 @@ class Imgur::Client::Image < Imgur::Model
|
|
26
26
|
data = connection.delete_image(deletehash).body
|
27
27
|
connection.basic_responses.new(data)
|
28
28
|
end
|
29
|
+
|
30
|
+
def comments
|
31
|
+
type = is_album ? "album" : "image"
|
32
|
+
data = connection.get_comments(type: type, id: id).body["data"]
|
33
|
+
connection.comments.load(data)
|
34
|
+
end
|
29
35
|
end
|
@@ -9,4 +9,22 @@ class Imgur::Client
|
|
9
9
|
)
|
10
10
|
end
|
11
11
|
end
|
12
|
+
|
13
|
+
class Mock
|
14
|
+
def delete_image(deletehash)
|
15
|
+
image = self.data[:images].values.detect { |i| i["deletehash"] == deletehash }
|
16
|
+
|
17
|
+
self.data[:images].delete_if do |id, image|
|
18
|
+
image["deletehash"] == deletehash
|
19
|
+
end
|
20
|
+
|
21
|
+
basic_response = {
|
22
|
+
"data" => true,
|
23
|
+
"status" => 200,
|
24
|
+
"success" => true,
|
25
|
+
}
|
26
|
+
|
27
|
+
response(body: basic_response)
|
28
|
+
end
|
29
|
+
end
|
12
30
|
end
|
@@ -8,4 +8,17 @@ class Imgur::Client
|
|
8
8
|
)
|
9
9
|
end
|
10
10
|
end
|
11
|
+
|
12
|
+
class Mock
|
13
|
+
def get_albums(params={})
|
14
|
+
albums = self.data[:albums].values
|
15
|
+
albums = if params[:path] =~ /^\/account/
|
16
|
+
{"data" => albums.select { |a| !a["account_id"].nil? } }
|
17
|
+
else
|
18
|
+
{"data" => albums}
|
19
|
+
end
|
20
|
+
|
21
|
+
response(body: albums)
|
22
|
+
end
|
23
|
+
end
|
11
24
|
end
|
@@ -8,4 +8,17 @@ class Imgur::Client
|
|
8
8
|
)
|
9
9
|
end
|
10
10
|
end
|
11
|
+
|
12
|
+
class Mock
|
13
|
+
def get_images(params={})
|
14
|
+
images = self.data[:images].values
|
15
|
+
images = if params[:path] =~ /^\/account/
|
16
|
+
{"data" => images.select { |i| !i["account_id"].nil? } }
|
17
|
+
else
|
18
|
+
{"data" => images}
|
19
|
+
end
|
20
|
+
|
21
|
+
response(body: images)
|
22
|
+
end
|
23
|
+
end
|
11
24
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
class Imgur::Client
|
2
2
|
class Real
|
3
|
-
def upload_image(options)
|
3
|
+
def upload_image(options={})
|
4
4
|
path = "/upload"
|
5
5
|
|
6
6
|
request(
|
@@ -10,4 +10,33 @@ class Imgur::Client
|
|
10
10
|
)
|
11
11
|
end
|
12
12
|
end
|
13
|
+
|
14
|
+
class Mock
|
15
|
+
def upload_image(options={})
|
16
|
+
images = self.data[:images]
|
17
|
+
image_id = self.random_id
|
18
|
+
account_id = self.data[:accounts].values.first["id"]
|
19
|
+
|
20
|
+
image = {
|
21
|
+
"id" => image_id,
|
22
|
+
"title" => options[:title],
|
23
|
+
"datetime" => 1349051625,
|
24
|
+
"type" => "image/jpeg",
|
25
|
+
"animated" => false,
|
26
|
+
"width" => 2490,
|
27
|
+
"height" => 3025,
|
28
|
+
"size" => 618969,
|
29
|
+
"views" => 625622,
|
30
|
+
"bandwidth" => 387240623718,
|
31
|
+
"ups" => 1889,
|
32
|
+
"downs" => 58,
|
33
|
+
"score" => 18935622,
|
34
|
+
"deletehash" => self.random_id,
|
35
|
+
}
|
36
|
+
|
37
|
+
images[image_id] = image
|
38
|
+
|
39
|
+
response(:body => {"data" => image})
|
40
|
+
end
|
41
|
+
end
|
13
42
|
end
|
data/lib/imgur/version.rb
CHANGED
data/spec/images_spec.rb
CHANGED
@@ -9,10 +9,19 @@ describe "images" do
|
|
9
9
|
images.first.should be_a_kind_of(Imgur::Client::Image)
|
10
10
|
end
|
11
11
|
|
12
|
+
it "should have comments" do
|
13
|
+
image = client.images.all.first
|
14
|
+
image.comments.should_not be_empty
|
15
|
+
image.comments.first.should be_a_kind_of(Imgur::Client::Comment)
|
16
|
+
end
|
17
|
+
|
12
18
|
it "should upload an image from filesystem" do
|
13
19
|
image = File.expand_path("spec/support/ruby_logo.jpg")
|
14
|
-
|
20
|
+
title = "ruby_logo_jpg"
|
21
|
+
response = client.images.upload(image: image, title: title)
|
15
22
|
response.should be_a_kind_of(Imgur::Client::Image)
|
23
|
+
image = client.images.get(response.id)
|
24
|
+
image.title.should == title
|
16
25
|
|
17
26
|
response = response.delete
|
18
27
|
response.should be_a_kind_of(Imgur::Client::BasicResponse)
|
@@ -20,8 +29,11 @@ describe "images" do
|
|
20
29
|
|
21
30
|
it "should upload an image from the internet" do
|
22
31
|
image = "http://www.ruby-lang.org/images/logo.gif"
|
23
|
-
|
32
|
+
title = "ruby_logo_gif"
|
33
|
+
response = client.images.upload(image: image, title: title)
|
24
34
|
response.should be_a_kind_of(Imgur::Client::Image)
|
35
|
+
image = client.images.get(response.id)
|
36
|
+
image.title.should == title
|
25
37
|
|
26
38
|
response = response.delete
|
27
39
|
response.should be_a_kind_of(Imgur::Client::BasicResponse)
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
ENV["IMGUR_MOCK"] ||= 'false'
|
2
|
+
|
1
3
|
Bundler.require(:test)
|
2
4
|
Bundler.require(:darwin) if RUBY_PLATFORM.match(/darwin/)
|
3
5
|
require File.expand_path("../../lib/imgur", __FILE__)
|
@@ -5,6 +7,11 @@ Dir[File.expand_path("../{shared,support}/*.rb", __FILE__)].each{|f| require(f)}
|
|
5
7
|
|
6
8
|
Cistern.formatter = Cistern::Formatter::AwesomePrint
|
7
9
|
|
10
|
+
if ENV["IMGUR_MOCK"] == 'true'
|
11
|
+
Cistern.timeout = 0
|
12
|
+
Imgur::Client.mock!
|
13
|
+
end
|
14
|
+
|
8
15
|
RSpec.configure do |config|
|
9
16
|
config.before(:all) do
|
10
17
|
Imgur::Client.reset! if Imgur::Client.mocking?
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-imgur
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.04'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: multi_json
|