imgurapi 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +7 -8
- data/lib/imgurapi.rb +0 -5
- data/lib/imgurapi/api/image.rb +3 -3
- data/lib/imgurapi/models/image.rb +1 -1
- data/lib/imgurapi/tasks/rake.rb +16 -2
- data/lib/imgurapi/tasks/tasks.rake +1 -1
- data/lib/imgurapi/version.rb +1 -1
- data/spec/imgurapi/api/image_spec.rb +38 -0
- data/spec/imgurapi/models/image_spec.rb +21 -0
- data/spec/imgurapi/session_spec.rb +20 -0
- data/spec/imgurapi_spec.rb +26 -0
- data/spec/spec_helper.rb +7 -4
- metadata +25 -11
- data/lib/imgurapi/extensions/array.rb +0 -9
- data/lib/imgurapi/extensions/hash.rb +0 -61
- data/lib/imgurapi/extensions/string.rb +0 -9
- data/spec/imgur/session_spec.rb +0 -20
- data/spec/imgur_spec.rb +0 -146
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 216926a679fce6cb67109da7246e1d9919cee5f0
|
4
|
+
data.tar.gz: be7432049b038156e42f376c0548c711421ec4c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1cf1a21fafb97f9d7efdf405814cc388d8e1d6c279c148aac5ec48e07b8d6d2c0a22d3893b1efebd2c5a33e51f335ed232f2773e093a29b20ddd0f550edad8a
|
7
|
+
data.tar.gz: 2bd8f37e2ff26f926d7db086f6eff2a29e19c90203cd98ca8c0f59c171cc107264de063e00be0a8c764906f923144e035bd7a30b105cc75b4c8deeb61d90bd81
|
data/README.md
CHANGED
@@ -37,12 +37,11 @@ $ rake imgur:authorize CLIENT_ID='CLIENT_ID' CLIENT_SECRET='CLIENT_SECRET'
|
|
37
37
|
Visit this URL: http://api.imgur.com/oauth/authorize?oauth_token=xxx
|
38
38
|
And after you approved the authorization please enter your verification code: yyy
|
39
39
|
|
40
|
-
Authorization was successful.
|
41
|
-
|
42
|
-
access_token: ACCESS_TOKEN
|
43
|
-
refresh_token: REFRESH_TOKEN
|
40
|
+
Authorization was successful. These are your credentials to initialize the library:
|
44
41
|
```
|
45
42
|
|
43
|
+
Copy the credentials shown as JSON or YAML, depending how you're going to use this library.
|
44
|
+
|
46
45
|
## Usage and overview
|
47
46
|
|
48
47
|
Create a session object to communicate to Imgur.
|
@@ -63,7 +62,7 @@ puts imgur_session.account.image_count
|
|
63
62
|
|
64
63
|
Upload your first image. Argument can be either a String or a File:
|
65
64
|
```ruby
|
66
|
-
image = imgur_session.image.
|
65
|
+
image = imgur_session.image.image_upload('portrait.jpg')
|
67
66
|
```
|
68
67
|
|
69
68
|
image is now an instance of Imgurapi::Image, a convenient way to manage all the attributes of your image (at least more convenient than a multilevel dictionary):
|
@@ -71,7 +70,7 @@ image is now an instance of Imgurapi::Image, a convenient way to manage all the
|
|
71
70
|
name = nil
|
72
71
|
title = nil
|
73
72
|
caption = nil
|
74
|
-
|
73
|
+
id = "xyzzy"
|
75
74
|
deletehash = "abcdef"
|
76
75
|
datetime = "2012-11-18 16:22:00"
|
77
76
|
type = "image/jpeg"
|
@@ -86,9 +85,9 @@ link = "http://imgur.com/xyzzy"
|
|
86
85
|
|
87
86
|
Do you need to retrieve a previously uploaded image?
|
88
87
|
```ruby
|
89
|
-
my_image = imgur_session.image.image('
|
88
|
+
my_image = imgur_session.image.image('id_code')
|
90
89
|
```
|
91
|
-
Granted, another Image object. It will
|
90
|
+
Granted, another Image object. It will error if it could not find an image matching the id.
|
92
91
|
|
93
92
|
Feel free to use it in your Rails views:
|
94
93
|
```ruby
|
data/lib/imgurapi.rb
CHANGED
@@ -3,11 +3,6 @@ require 'faraday'
|
|
3
3
|
require 'json'
|
4
4
|
require 'base64'
|
5
5
|
|
6
|
-
# Some nice extensions to base classes
|
7
|
-
require 'imgurapi/extensions/array'
|
8
|
-
require 'imgurapi/extensions/hash'
|
9
|
-
require 'imgurapi/extensions/string'
|
10
|
-
|
11
6
|
# This gem real code
|
12
7
|
require 'imgurapi/version'
|
13
8
|
require 'imgurapi/api/base'
|
data/lib/imgurapi/api/image.rb
CHANGED
@@ -4,14 +4,14 @@ module Imgurapi
|
|
4
4
|
|
5
5
|
# https://api.imgur.com/endpoints/image#image
|
6
6
|
def image(id)
|
7
|
-
raise 'Please provide a valid image identificator' if id.nil?
|
7
|
+
raise 'Please provide a valid image identificator' if id.nil? || !id.is_a?(String) || id == '' || !!(id =~ /[^\w]/)
|
8
8
|
|
9
9
|
Imgurapi::Image.new communication.call(:get, "image/#{id}")
|
10
10
|
end
|
11
11
|
|
12
12
|
# https://api.imgur.com/endpoints/image#image-upload
|
13
13
|
def image_upload(local_file)
|
14
|
-
if local_file.
|
14
|
+
if local_file.is_a?(String)
|
15
15
|
file = File.open(local_file, 'rb')
|
16
16
|
elsif local_file.respond_to? :read
|
17
17
|
file = local_file
|
@@ -28,7 +28,7 @@ module Imgurapi
|
|
28
28
|
id = id.id
|
29
29
|
end
|
30
30
|
|
31
|
-
raise 'Please provide a valid image identificator' if id.nil?
|
31
|
+
raise 'Please provide a valid image identificator' if id.nil? || !id.is_a?(String) || id == '' || !!(id =~ /[^\w]/)
|
32
32
|
|
33
33
|
communication.call(:delete, "image/#{id}")
|
34
34
|
end
|
@@ -10,7 +10,7 @@ module Imgurapi
|
|
10
10
|
|
11
11
|
# Provides the download URL in case you know a valid imgur hash and don't want to make a network trip with .find
|
12
12
|
# Just in case you don't need the full Imgurapi::Image object
|
13
|
-
def url(size)
|
13
|
+
def url(size = nil)
|
14
14
|
size = case size
|
15
15
|
when :small_square, :small, :s
|
16
16
|
's'
|
data/lib/imgurapi/tasks/rake.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'imgurapi/session'
|
2
|
+
require 'faraday'
|
2
3
|
|
3
4
|
module Imgurapi
|
4
5
|
|
@@ -26,10 +27,23 @@ module Imgurapi
|
|
26
27
|
|
27
28
|
puts <<-MESSAGE
|
28
29
|
|
29
|
-
Authorization was successful.
|
30
|
+
Authorization was successful. These are your credentials to initialize the library:
|
30
31
|
|
32
|
+
JSON format, for credentials.json:
|
33
|
+
|
34
|
+
{
|
35
|
+
"client_id": "#{client_id}",
|
36
|
+
"client_secret": "#{client_secret}",
|
37
|
+
"access_token": "#{credentials['access_token']}",
|
38
|
+
"refresh_token": "#{credentials['refresh_token']}"
|
39
|
+
}
|
40
|
+
|
41
|
+
YAML format, for imgur.yml:
|
42
|
+
|
43
|
+
client_id: #{client_id}
|
44
|
+
client_secret: #{client_secret}
|
31
45
|
access_token: #{credentials['access_token']}
|
32
|
-
|
46
|
+
refresh_token: #{credentials['refresh_token']}
|
33
47
|
|
34
48
|
MESSAGE
|
35
49
|
end
|
@@ -3,7 +3,7 @@ require 'imgurapi/tasks/rake'
|
|
3
3
|
namespace :imgur do
|
4
4
|
desc 'Obtain your Imgur tokens'
|
5
5
|
task :authorize do
|
6
|
-
if ENV['CLIENT_ID'].nil?
|
6
|
+
if ENV['CLIENT_ID'].nil? || ENV['CLIENT_SECRET'].nil?
|
7
7
|
puts "USAGE: `rake imgur:authorize CLIENT_ID=your_client_id CLIENT_SECRET=your_client_secret`"
|
8
8
|
exit
|
9
9
|
end
|
data/lib/imgurapi/version.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
require 'imgurapi'
|
3
|
+
|
4
|
+
describe Imgurapi::Api::Image do
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
credentials = read_credentials_file
|
8
|
+
@session = Imgurapi::Session.new(credentials)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#image' do
|
12
|
+
it 'retrieves the image' do
|
13
|
+
expect { @session.image.image(:not_an_image_id) }.to raise_error StandardError
|
14
|
+
expect { @session.image.image('r4ndom 1d') }.to raise_error StandardError
|
15
|
+
|
16
|
+
image = @session.image.image('12345')
|
17
|
+
expect(image.error).to eq 'Unable to find an image with the id, 12345'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#image_upload' do
|
22
|
+
it 'uploads the image' do
|
23
|
+
expect { @session.image.image_upload('') }.to raise_error StandardError
|
24
|
+
expect { @session.image.image_upload(:not_the_expected_object) }.to raise_error StandardError
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#image_delete' do
|
29
|
+
it 'deletes the image' do
|
30
|
+
expect { @session.image.image_delete(:not_an_image_id) }.to raise_error StandardError
|
31
|
+
expect { @session.image.image_delete('r4ndom 1d') }.to raise_error StandardError
|
32
|
+
|
33
|
+
#expect(@session.image.image_delete('12345')).to eq true # is 200 when image does not exist?
|
34
|
+
|
35
|
+
expect { @session.image.image_delete('valid_id') }.to raise_exception 'Retried 3 times but could not get an access_token'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
require 'imgurapi'
|
3
|
+
|
4
|
+
describe Imgurapi::Image do
|
5
|
+
|
6
|
+
it 'creates an Image with the fields provided' do
|
7
|
+
image = Imgurapi::Image.new(a: 1, b: 2)
|
8
|
+
|
9
|
+
expect(image.a).to eq 1
|
10
|
+
expect(image.b).to eq 2
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns a download URL' do
|
14
|
+
image = Imgurapi::Image.new(id: 'hash')
|
15
|
+
|
16
|
+
expect(image.url).to eq "http://i.imgur.com/hash.jpg"
|
17
|
+
expect(image.url(:random_size)).to eq "http://i.imgur.com/hash.jpg"
|
18
|
+
expect(image.url(:small_square)).to eq "http://i.imgur.com/hashs.jpg"
|
19
|
+
expect(image.url(:large_thumbnail)).to eq "http://i.imgur.com/hashl.jpg"
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'imgurapi'
|
3
|
+
|
4
|
+
describe Imgurapi::Session do
|
5
|
+
|
6
|
+
context 'incorrect credentials' do
|
7
|
+
it { expect { described_class.new }.to raise_error ArgumentError }
|
8
|
+
it { expect { described_class.new(random_key: nil) }.to raise_error ArgumentError }
|
9
|
+
it { expect { described_class.new(client_id: nil, random_key: nil) }.to raise_error ArgumentError }
|
10
|
+
it { expect { described_class.new({}) }.to raise_error ArgumentError }
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'correct credentials' do
|
14
|
+
it do
|
15
|
+
expect(
|
16
|
+
described_class.new(client_id: 'ID', client_secret: 'SECRET', refresh_token: 'TOKEN')
|
17
|
+
).to be_an_instance_of described_class
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'imgurapi'
|
3
|
+
|
4
|
+
describe Imgurapi do
|
5
|
+
|
6
|
+
it 'does an integration test by uploading, retrieving and deleting an image' do
|
7
|
+
credentials = read_credentials_file
|
8
|
+
@session = Imgurapi::Session.new(credentials)
|
9
|
+
@upload_path, @upload_file = my_sample_image
|
10
|
+
|
11
|
+
# Upload image
|
12
|
+
image = @session.image.image_upload(@upload_path)
|
13
|
+
|
14
|
+
# It is there
|
15
|
+
expect(@session.account.image_count).to be > 0
|
16
|
+
|
17
|
+
# Retrieve same image
|
18
|
+
retrieved_image = @session.image.image(image.id)
|
19
|
+
|
20
|
+
# Same, indeed
|
21
|
+
expect(retrieved_image.id).to eq(image.id)
|
22
|
+
|
23
|
+
# Delete image
|
24
|
+
expect(@session.image.image_delete(image)).to eq true
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,21 +4,24 @@ module EverythingAsExpected
|
|
4
4
|
unless File.exist? 'credentials.json'
|
5
5
|
raise "Please add a credentials.json file to the project directory containing your Imgur app_key, app_secret, access_token and access_token_secret. See credentials.json.example to get started."
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
credentials_file_contents = File.open('credentials.json', 'r').read
|
9
9
|
credentials = JSON.parse(credentials_file_contents)
|
10
10
|
if credentials.keys.count != 4 and credentials.keys & [:app_key, :app_secret, :access_token, :access_token_secret] != [:app_key, :app_secret, :access_token, :access_token_secret]
|
11
11
|
raise "Your credentials.json file does contain all the required information. See credentials.json.example for more help."
|
12
12
|
end
|
13
|
-
|
14
|
-
credentials.
|
13
|
+
|
14
|
+
credentials.inject({}) do |options, (key, value)|
|
15
|
+
options[key.to_sym] = value
|
16
|
+
options
|
17
|
+
end
|
15
18
|
end
|
16
19
|
|
17
20
|
def my_sample_image
|
18
21
|
unless File.exist? 'sample.jpg'
|
19
22
|
raise "Please add a sample.jpg file to the project directory to test upload and download. Recommended size: under 30kB"
|
20
23
|
end
|
21
|
-
|
24
|
+
|
22
25
|
['sample.jpg', File.open('sample.jpg', 'r')]
|
23
26
|
end
|
24
27
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imgurapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Cruz Horts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: pry
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,9 +80,6 @@ files:
|
|
66
80
|
- lib/imgurapi/api/base.rb
|
67
81
|
- lib/imgurapi/api/image.rb
|
68
82
|
- lib/imgurapi/communication.rb
|
69
|
-
- lib/imgurapi/extensions/array.rb
|
70
|
-
- lib/imgurapi/extensions/hash.rb
|
71
|
-
- lib/imgurapi/extensions/string.rb
|
72
83
|
- lib/imgurapi/models/account.rb
|
73
84
|
- lib/imgurapi/models/base.rb
|
74
85
|
- lib/imgurapi/models/image.rb
|
@@ -77,8 +88,10 @@ files:
|
|
77
88
|
- lib/imgurapi/tasks/rake.rb
|
78
89
|
- lib/imgurapi/tasks/tasks.rake
|
79
90
|
- lib/imgurapi/version.rb
|
80
|
-
- spec/
|
81
|
-
- spec/
|
91
|
+
- spec/imgurapi/api/image_spec.rb
|
92
|
+
- spec/imgurapi/models/image_spec.rb
|
93
|
+
- spec/imgurapi/session_spec.rb
|
94
|
+
- spec/imgurapi_spec.rb
|
82
95
|
- spec/spec_helper.rb
|
83
96
|
homepage: https://github.com/dncrht/imgur
|
84
97
|
licenses:
|
@@ -100,12 +113,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
113
|
version: '0'
|
101
114
|
requirements: []
|
102
115
|
rubyforge_project:
|
103
|
-
rubygems_version: 2.
|
116
|
+
rubygems_version: 2.6.11
|
104
117
|
signing_key:
|
105
118
|
specification_version: 4
|
106
119
|
summary: Imgur authenticated API
|
107
120
|
test_files:
|
108
|
-
- spec/
|
109
|
-
- spec/
|
121
|
+
- spec/imgurapi/api/image_spec.rb
|
122
|
+
- spec/imgurapi/models/image_spec.rb
|
123
|
+
- spec/imgurapi/session_spec.rb
|
124
|
+
- spec/imgurapi_spec.rb
|
110
125
|
- spec/spec_helper.rb
|
111
|
-
has_rdoc:
|
@@ -1,61 +0,0 @@
|
|
1
|
-
class Hash # :nodoc:
|
2
|
-
def slice(*keys) #:nodoc:
|
3
|
-
keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
|
4
|
-
hash = self.class.new
|
5
|
-
keys.each { |k| hash[k] = self[k] if has_key?(k) }
|
6
|
-
hash
|
7
|
-
end unless method_defined?(:slice)
|
8
|
-
|
9
|
-
def symbolize_keys # :nodoc:
|
10
|
-
inject({}) do |options, (key, value)|
|
11
|
-
options[(key.to_sym rescue key) || key] = value
|
12
|
-
options
|
13
|
-
end
|
14
|
-
end unless method_defined?(:symbolize_keys)
|
15
|
-
|
16
|
-
def symbolize_keys! # :nodoc:
|
17
|
-
self.replace(self.symbolize_keys)
|
18
|
-
end unless method_defined?(:symbolize_keys!)
|
19
|
-
|
20
|
-
def symbolize_keys_recursively # :nodoc:
|
21
|
-
hsh = symbolize_keys
|
22
|
-
hsh.each { |k, v| hsh[k] = v.symbolize_keys_recursively if v.kind_of?(Hash) }
|
23
|
-
hsh.each { |k, v| hsh[k] = v.map { |i| i.kind_of?(Hash) ? i.symbolize_keys_recursively : i } if v.kind_of?(Array) }
|
24
|
-
return hsh
|
25
|
-
end unless method_defined?(:symbolize_keys_recursively)
|
26
|
-
|
27
|
-
def stringify_keys # :nodoc:
|
28
|
-
inject({}) do |options, (key, value)|
|
29
|
-
options[(key.to_s rescue key) || key] = value
|
30
|
-
options
|
31
|
-
end
|
32
|
-
end unless method_defined?(:stringify_keys)
|
33
|
-
|
34
|
-
def stringify_keys! # :nodoc:
|
35
|
-
self.replace(self.stringify_keys)
|
36
|
-
end unless method_defined?(:stringify_keys!)
|
37
|
-
|
38
|
-
def stringify_keys_recursively # :nodoc:
|
39
|
-
hsh = stringify_keys
|
40
|
-
hsh.each { |k, v| hsh[k] = v.stringify_keys_recursively if v.kind_of?(Hash) }
|
41
|
-
hsh.each { |k, v| hsh[k] = v.map { |i| i.kind_of?(Hash) ? i.stringify_keys_recursively : i } if v.kind_of?(Array) }
|
42
|
-
return hsh
|
43
|
-
end unless method_defined?(:stringify_keys_recursively)
|
44
|
-
|
45
|
-
def to_struct # :nodoc:
|
46
|
-
struct = Struct.new(*keys).new(*values)
|
47
|
-
# attach methods for any predicate keys, since Struct.new doesn't seem to do that
|
48
|
-
pred_keys = slice(*(keys.select { |key| key.to_s.ends_with?('?') }))
|
49
|
-
pred_keys.each do |key, val|
|
50
|
-
struct.eigenclass.send(:define_method, key.to_sym) { return val }
|
51
|
-
end
|
52
|
-
return struct
|
53
|
-
end unless method_defined?(:to_struct)
|
54
|
-
|
55
|
-
def to_struct_recursively # :nodoc:
|
56
|
-
hsh = dup
|
57
|
-
hsh.each { |k, v| hsh[k] = v.to_struct_recursively if v.kind_of?(Hash) }
|
58
|
-
hsh.each { |k, v| hsh[k] = v.map { |i| i.kind_of?(Hash) ? i.to_struct_recursively : i } if v.kind_of?(Array) }
|
59
|
-
return hsh.to_struct
|
60
|
-
end unless method_defined?(:to_struct_recursively)
|
61
|
-
end
|
@@ -1,9 +0,0 @@
|
|
1
|
-
class String # :nodoc:
|
2
|
-
def starts_with?(prefix) # :nodoc:
|
3
|
-
self[0, prefix.length] == prefix
|
4
|
-
end unless method_defined?(:starts_with?)
|
5
|
-
|
6
|
-
def ends_with?(suffix) # :nodoc:
|
7
|
-
self[-suffix.length, suffix.length] == suffix
|
8
|
-
end unless method_defined?(:ends_with?)
|
9
|
-
end
|
data/spec/imgur/session_spec.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require_relative '../spec_helper'
|
2
|
-
require 'imgur'
|
3
|
-
|
4
|
-
describe Imgurapiapi::Session do
|
5
|
-
|
6
|
-
context 'incorrect credentials' do
|
7
|
-
it { expect { Imgurapi::Session.new }.to raise_error }
|
8
|
-
it { expect { Imgurapi::Session.new(random_key: nil) }.to raise_error }
|
9
|
-
it { expect { Imgurapi::Session.new(client_id: nil, random_key: nil) }.to raise_error }
|
10
|
-
it { expect { Imgurapi::Session.new({}) }.to raise_error }
|
11
|
-
end
|
12
|
-
|
13
|
-
context 'correct credentials' do
|
14
|
-
it do
|
15
|
-
expect(
|
16
|
-
Imgurapi::Session.new(client_id: 'ID', client_secret: 'SECRET', refresh_token: 'TOKEN')
|
17
|
-
).to be_an_instance_of Imgurapi::Session
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/spec/imgur_spec.rb
DELETED
@@ -1,146 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
require 'imgur'
|
3
|
-
|
4
|
-
describe Imgurapi do
|
5
|
-
|
6
|
-
describe 'placeholder classes' do
|
7
|
-
|
8
|
-
it 'should create an Image with the fields provided' do
|
9
|
-
image = Imgurapi::Image.new
|
10
|
-
image.should be_an_instance_of Imgurapi::Image
|
11
|
-
|
12
|
-
fields = {:a => 1, :b => 2}
|
13
|
-
image = Imgurapi::Image.new(fields)
|
14
|
-
image.should be_an_instance_of Imgurapi::Image
|
15
|
-
image.a.should eq(1)
|
16
|
-
image.b.should eq(2)
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'should create a Link with the fields provided' do
|
20
|
-
links = Imgurapi::Links.new
|
21
|
-
links.should be_an_instance_of Imgurapi::Links
|
22
|
-
|
23
|
-
fields = {:a => 1, :b => 2}
|
24
|
-
links = Imgurapi::Links.new(fields)
|
25
|
-
links.should be_an_instance_of Imgurapi::Links
|
26
|
-
links.a.should eq(1)
|
27
|
-
links.b.should eq(2)
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
describe 'api' do
|
33
|
-
|
34
|
-
it 'should return a download URL' do
|
35
|
-
imgur_hash = 'random_valid_hash'
|
36
|
-
|
37
|
-
@session.url(:foo).should eq('')
|
38
|
-
@session.url().should eq('')
|
39
|
-
@session.url(imgur_hash).should eq("http://i.imgur.com/#{imgur_hash}.jpg")
|
40
|
-
@session.url(imgur_hash, :random_size).should eq("http://i.imgur.com/#{imgur_hash}.jpg")
|
41
|
-
@session.url(imgur_hash, :small_square).should eq("http://i.imgur.com/#{imgur_hash}s.jpg")
|
42
|
-
@session.url(imgur_hash, :large_thumbnail).should eq("http://i.imgur.com/#{imgur_hash}l.jpg")
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
describe 'communication' do
|
48
|
-
|
49
|
-
before(:all) do
|
50
|
-
credentials = read_credentials_file
|
51
|
-
@session = Imgurapi::Session.new(credentials)
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'should parse_message' do
|
55
|
-
@session.send(:parse_message, '[]').should eq([])
|
56
|
-
expect { session.send(:parse_message) }.to raise_error
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'should process_message' do
|
60
|
-
Response = Struct.new(:code, :body)
|
61
|
-
|
62
|
-
response = Response.new(200, '[]')
|
63
|
-
@session.send(:process_response, response).should eq([])
|
64
|
-
|
65
|
-
response = Response.new(404, '[]')
|
66
|
-
@session.send(:process_response, response).should eq([])
|
67
|
-
|
68
|
-
response = Response.new(401, '{"error": {"message": "1"}}')
|
69
|
-
expect { @session.send(:process_response, response) }.to raise_error "Unauthorized: 1"
|
70
|
-
|
71
|
-
response = Response.new(500, '{"error": {"message": "1"}}')
|
72
|
-
expect { @session.send(:process_response, response) }.to raise_error "Unauthorized: 1"
|
73
|
-
|
74
|
-
code = rand(999)
|
75
|
-
response = Response.new(code, '')
|
76
|
-
expect { @session.send(:process_response, response) }.to raise_error "Response code #{code} not recognized"
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'should compose_image' do
|
80
|
-
@session.send(:compose_image, {}).should eq(nil)
|
81
|
-
@session.send(:compose_image, {:imags => {:image => {}, :links => {}}}).should eq(nil)
|
82
|
-
@session.send(:compose_image, {:images => {:imae => {}, :links => {}}}).should eq(nil)
|
83
|
-
@session.send(:compose_image, {:images => {:image => {}, :lins => {}}}).should eq(nil)
|
84
|
-
@session.send(:compose_image, {:images => {}}).should eq(nil)
|
85
|
-
|
86
|
-
from_hash = {:images => {:image => {}, :links => {}}}
|
87
|
-
@session.send(:compose_image, from_hash).should be_an_instance_of Imgurapi::Image
|
88
|
-
@session.send(:compose_image, from_hash).links.should be_an_instance_of Imgurapi::Links
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
describe 'image management calls' do
|
94
|
-
|
95
|
-
before(:all) do
|
96
|
-
credentials = read_credentials_file
|
97
|
-
@session = Imgurapi::Session.new(credentials)
|
98
|
-
@upload_path, @upload_file = my_sample_image
|
99
|
-
end
|
100
|
-
|
101
|
-
it 'should return my account information' do
|
102
|
-
@session.account.should be_an_instance_of Hash
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'should return the number of images stored' do
|
106
|
-
@session.images_count.should > 0
|
107
|
-
end
|
108
|
-
|
109
|
-
it 'should upload the image' do
|
110
|
-
expect { @session.upload('') }.to raise_error
|
111
|
-
expect { @session.upload(:not_the_expected_object) }.to raise_error
|
112
|
-
|
113
|
-
@@image_by_path = @session.upload(@upload_path) #FIXME I know this is horrible, but I need to share the same image between tests http://brentlavelle.wordpress.com/2011/04/04/rspec-and-instance-variables/
|
114
|
-
@@image_by_path.should be_an_instance_of Imgurapi::Image
|
115
|
-
|
116
|
-
@@image_by_file = @session.upload(@upload_file)
|
117
|
-
@@image_by_file.should be_an_instance_of Imgurapi::Image
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'should retrieve the image' do
|
121
|
-
expect { @session.find(:not_an_image_hash) }.to raise_error
|
122
|
-
expect { @session.find('r4ndom ha5h') }.to raise_error
|
123
|
-
|
124
|
-
image = @session.find('valid_hash_not_related_to_any_image')
|
125
|
-
image.should be_nil
|
126
|
-
|
127
|
-
image = @session.find(@@image_by_path.hash)
|
128
|
-
image.should be_an_instance_of Imgurapi::Image
|
129
|
-
|
130
|
-
image.hash.should eq(@@image_by_path.hash)
|
131
|
-
end
|
132
|
-
|
133
|
-
it 'should delete the image' do
|
134
|
-
expect { @session.destroy(:not_an_image_hash) }.to raise_error
|
135
|
-
expect { @session.destroy('r4ndom ha5h') }.to raise_error
|
136
|
-
|
137
|
-
@session.destroy('valid_hash_not_related_to_any_image').should be_false
|
138
|
-
|
139
|
-
@session.destroy(@@image_by_path).should be_true #deletes first image
|
140
|
-
|
141
|
-
@session.destroy(@@image_by_file.hash).should be_true #deletes second image
|
142
|
-
end
|
143
|
-
|
144
|
-
end
|
145
|
-
|
146
|
-
end
|