ruby-imgur 0.01
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +45 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +55 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +40 -0
- data/Rakefile +12 -0
- data/imgur.gemspec +24 -0
- data/lib/imgur.rb +30 -0
- data/lib/imgur/client.rb +150 -0
- data/lib/imgur/collection.rb +33 -0
- data/lib/imgur/logger.rb +20 -0
- data/lib/imgur/model.rb +18 -0
- data/lib/imgur/models/image.rb +23 -0
- data/lib/imgur/models/images.rb +12 -0
- data/lib/imgur/paged_collection.rb +50 -0
- data/lib/imgur/requests/get_images.rb +20 -0
- data/lib/imgur/response.rb +38 -0
- data/lib/imgur/version.rb +3 -0
- data/spec/images_spec.rb +11 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/client.rb +13 -0
- metadata +167 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
|
4
|
+
# rdoc generated
|
5
|
+
rdoc
|
6
|
+
|
7
|
+
# yard generated
|
8
|
+
doc
|
9
|
+
.yardoc
|
10
|
+
|
11
|
+
# bundler
|
12
|
+
.bundle
|
13
|
+
|
14
|
+
# jeweler generated
|
15
|
+
pkg
|
16
|
+
|
17
|
+
config/config.yml
|
18
|
+
*.gem
|
19
|
+
|
20
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
21
|
+
#
|
22
|
+
# * Create a file at ~/.gitignore
|
23
|
+
# * Include files you want ignored
|
24
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
25
|
+
#
|
26
|
+
# After doing this, these files will be ignored in all your git projects,
|
27
|
+
# saving you from having to 'pollute' every project you touch with them
|
28
|
+
#
|
29
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
30
|
+
#
|
31
|
+
# For MacOS:
|
32
|
+
#
|
33
|
+
#.DS_Store
|
34
|
+
#
|
35
|
+
# For TextMate
|
36
|
+
#*.tmproj
|
37
|
+
#tmtags
|
38
|
+
#
|
39
|
+
# For emacs:
|
40
|
+
#*~
|
41
|
+
#\#*
|
42
|
+
#.\#*
|
43
|
+
#
|
44
|
+
# For vim:
|
45
|
+
#*.swp
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ruby-imgur (0.01)
|
5
|
+
addressable
|
6
|
+
cistern (~> 0.1.3)
|
7
|
+
excon
|
8
|
+
launchy
|
9
|
+
multi_json
|
10
|
+
rest-client
|
11
|
+
|
12
|
+
GEM
|
13
|
+
remote: http://rubygems.org/
|
14
|
+
specs:
|
15
|
+
addressable (2.3.2)
|
16
|
+
awesome_print (1.1.0)
|
17
|
+
cistern (0.1.4)
|
18
|
+
coderay (1.0.8)
|
19
|
+
diff-lcs (1.1.3)
|
20
|
+
excon (0.16.10)
|
21
|
+
launchy (2.1.2)
|
22
|
+
addressable (~> 2.3)
|
23
|
+
method_source (0.8.1)
|
24
|
+
mime-types (1.19)
|
25
|
+
multi_json (1.5.0)
|
26
|
+
pry (0.9.10)
|
27
|
+
coderay (~> 1.0.5)
|
28
|
+
method_source (~> 0.8)
|
29
|
+
slop (~> 3.3.1)
|
30
|
+
pry-nav (0.2.2)
|
31
|
+
pry (~> 0.9.10)
|
32
|
+
rake (10.0.2)
|
33
|
+
rest-client (1.6.7)
|
34
|
+
mime-types (>= 1.16)
|
35
|
+
rspec (2.12.0)
|
36
|
+
rspec-core (~> 2.12.0)
|
37
|
+
rspec-expectations (~> 2.12.0)
|
38
|
+
rspec-mocks (~> 2.12.0)
|
39
|
+
rspec-core (2.12.1)
|
40
|
+
rspec-expectations (2.12.0)
|
41
|
+
diff-lcs (~> 1.1.3)
|
42
|
+
rspec-mocks (2.12.0)
|
43
|
+
slop (3.3.3)
|
44
|
+
|
45
|
+
PLATFORMS
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
awesome_print
|
50
|
+
bundler
|
51
|
+
pry
|
52
|
+
pry-nav
|
53
|
+
rake
|
54
|
+
rspec
|
55
|
+
ruby-imgur!
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Eugene Howe
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
= imgur
|
2
|
+
|
3
|
+
Library to interface with the imgur API.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
1. Register your application with imgur at https://api.imgur.com/oauth2/addclient
|
8
|
+
2. Place a file called .imgurrc in your user's $HOME directory with the following contents
|
9
|
+
:client_id: "client_id_from_registering_app"
|
10
|
+
:client_secret: "client_secret_from_registering_app"
|
11
|
+
:account_username: "username_of_imgur_account"
|
12
|
+
3. On first run, you will be presented with a browser window asking you to allow access to your account, do this.
|
13
|
+
4. After approval, you will be prompted to enter both the access_token and refresh_token values, which can be retrieved from the URL of the page you are redirected to
|
14
|
+
|
15
|
+
Examples:
|
16
|
+
|
17
|
+
#Get first page of gallery
|
18
|
+
client = Imgur::Client.new
|
19
|
+
client.images.all(resource: "gallery", page: 0)
|
20
|
+
-> [
|
21
|
+
[ 0] #<Imgur::Client::Image:0x7fa559f29f08 attributes={id:"pzJKU",title:"I had quite the view last night",datetime:1355670464,animated:false,width:5184,height:3456,size:519719,views:0,bandwidth:0,account_url:nil,link:"http://i.imgur.com/pzJKU.jpg",ups:94,downs:0,score:406,is_album:false}>,
|
22
|
+
...
|
23
|
+
[173] #<Imgur::Client::Image:0x7fa55ad4a5b0 attributes={id:"CmIrs",title:"Day officially made",datetime:1355595301,animated:false,width:245,height:176,size:899725,views:21774,bandwidth:19590612150,account_url:"idontwantoliveonthisplanetanymore",link:"http://i.imgur.com/CmIrs.gif",ups:269,downs:6,score:273,is_album:false}>
|
24
|
+
]
|
25
|
+
|
26
|
+
== Contributing to imgur
|
27
|
+
|
28
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
29
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
30
|
+
* Fork the project
|
31
|
+
* Start a feature/bugfix branch
|
32
|
+
* Commit and push until you are happy with your contribution
|
33
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
34
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
35
|
+
|
36
|
+
== Copyright
|
37
|
+
|
38
|
+
Copyright (c) 2012 Eugene Howe. See LICENSE.txt for
|
39
|
+
further details.
|
40
|
+
|
data/Rakefile
ADDED
data/imgur.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/imgur/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Eugene Howe"]
|
6
|
+
gem.email = ["eugene@xtreme-computers.net"]
|
7
|
+
gem.description = ""
|
8
|
+
gem.summary = ""
|
9
|
+
gem.homepage = "http://github.com/ehowe/ruby-imgur.git"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "ruby-imgur"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Imgur::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "multi_json"
|
19
|
+
gem.add_dependency "cistern", "~> 0.1.3"
|
20
|
+
gem.add_dependency "addressable"
|
21
|
+
gem.add_dependency "excon"
|
22
|
+
gem.add_dependency "launchy"
|
23
|
+
gem.add_dependency "rest-client"
|
24
|
+
end
|
data/lib/imgur.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'imgur/version'
|
2
|
+
require 'cistern'
|
3
|
+
require 'logger'
|
4
|
+
require 'excon'
|
5
|
+
require 'launchy'
|
6
|
+
require 'yaml'
|
7
|
+
require 'rest-client'
|
8
|
+
|
9
|
+
module Imgur
|
10
|
+
DEFAULT_TIMEOUT = 60
|
11
|
+
RequestFailure = Class.new(Exception)
|
12
|
+
|
13
|
+
autoload :Client, 'imgur/client'
|
14
|
+
autoload :Collection, 'imgur/collection'
|
15
|
+
autoload :Json, 'imgur/json'
|
16
|
+
autoload :Logger, 'imgur/logger'
|
17
|
+
autoload :Model, 'imgur/model'
|
18
|
+
autoload :Response, 'imgur/response'
|
19
|
+
autoload :PagedCollection, 'imgur/paged_collection'
|
20
|
+
|
21
|
+
def self.paging_parameters(params)
|
22
|
+
if url = params["url"]
|
23
|
+
Addressable::URI.parse(url).query_values
|
24
|
+
else
|
25
|
+
params
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Cistern.timeout = Imgur::DEFAULT_TIMEOUT
|
data/lib/imgur/client.rb
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
class Imgur::Client < Cistern::Service
|
2
|
+
@recognized_arguments = [:config]
|
3
|
+
model_path "imgur/models"
|
4
|
+
request_path "imgur/requests"
|
5
|
+
|
6
|
+
model :image
|
7
|
+
collection :images
|
8
|
+
request :get_images
|
9
|
+
|
10
|
+
class Real
|
11
|
+
attr_accessor :url, :path, :connection, :parser, :logger, :config, :authorize_path, :token_path
|
12
|
+
|
13
|
+
def initialize(options={})
|
14
|
+
@config = options[:config] || YAML.load_file(File.expand_path("~/.imgurrc")) || YAML.load_file("config/config.yml")
|
15
|
+
@authorize_path = "/oauth2/authorize"
|
16
|
+
@token_path = "/oauth2/token"
|
17
|
+
@url = URI.parse(options[:url] || "https://api.imgur.com")
|
18
|
+
@logger = options[:logger] || Logger.new(nil)
|
19
|
+
@parser = begin; require 'json'; JSON; end
|
20
|
+
|
21
|
+
Excon.defaults[:ssl_verify_peer] = false
|
22
|
+
@connection = Excon.new(@url.to_s)
|
23
|
+
end
|
24
|
+
|
25
|
+
def refresh_token
|
26
|
+
response = RestClient.post(
|
27
|
+
@url.to_s + @token_path,
|
28
|
+
:client_id => @config[:client_id],
|
29
|
+
:client_secret => @config[:client_secret],
|
30
|
+
:refresh_token => @config[:refresh_token],
|
31
|
+
:grant_type => "refresh_token",
|
32
|
+
)
|
33
|
+
new_params = @parser.load(response)
|
34
|
+
@config[:access_token] = new_params["access_token"]
|
35
|
+
@config[:refresh_token] = new_params["refresh_token"]
|
36
|
+
File.open(File.expand_path("~/.imgurrc"), "w") { |f| YAML.dump(@config, f) }
|
37
|
+
return true
|
38
|
+
end
|
39
|
+
|
40
|
+
def request(options={})
|
41
|
+
method = (options[:method] || :get).to_s.downcase
|
42
|
+
path = "/3#{options[:path]}" || "/3"
|
43
|
+
query = options[:query] || {}
|
44
|
+
unless @config[:access_token]
|
45
|
+
Launchy.open(@url.to_s + @authorize_path + "?client_id=#{@config[:client_id]}&response_type=token")
|
46
|
+
puts "Copy and paste access_token from URL here"
|
47
|
+
verifier = $stdin.gets.strip
|
48
|
+
puts "Copy and paste refresh_token from URL here"
|
49
|
+
refresh_token = $stdin.gets.strip
|
50
|
+
@config[:access_token] = verifier
|
51
|
+
File.open(File.expand_path("~/.imgurrc"), 'w') { |f| YAML.dump(@config, f) }
|
52
|
+
end
|
53
|
+
headers = {
|
54
|
+
"Accept" => "application/json",
|
55
|
+
"Authorization" => "Bearer #{@config[:access_token]}",
|
56
|
+
}.merge(options[:headers] || {})
|
57
|
+
|
58
|
+
request_body = if body = options[:body]
|
59
|
+
json_body = parser.dump(body)
|
60
|
+
headers = {
|
61
|
+
"Content-Type" => "application/json, charset=utf-8",
|
62
|
+
"Content-Length" => json_body.size.to_s,
|
63
|
+
}.merge(options[:headers] || {})
|
64
|
+
|
65
|
+
json_body
|
66
|
+
end
|
67
|
+
request_body ||= options[:params] || {}
|
68
|
+
path = "#{path}?#{query.map{|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}"}.join("&")}" unless query.empty?
|
69
|
+
options = {:method => method, :path => path, :headers => headers}
|
70
|
+
response = @connection.request(options)
|
71
|
+
response = if response.status == 403
|
72
|
+
self.refresh_token
|
73
|
+
@connection.request(options).body
|
74
|
+
else
|
75
|
+
response.body
|
76
|
+
end
|
77
|
+
parsed_body = response.strip.empty? ? {} : parser.load(response)
|
78
|
+
status = parsed_body.delete("status")
|
79
|
+
Imgur::Response.new(status, {}, parsed_body).raise!
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class Mock
|
84
|
+
def self.data
|
85
|
+
@data ||= {}
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.reset!
|
89
|
+
@data = nil
|
90
|
+
end
|
91
|
+
|
92
|
+
def data
|
93
|
+
self.class.data
|
94
|
+
end
|
95
|
+
|
96
|
+
def initalize(options={})
|
97
|
+
@url = options[:url] || "https://api.imgur.com"
|
98
|
+
end
|
99
|
+
|
100
|
+
def page(params, collection, object_rool, options={})
|
101
|
+
if params["url"]
|
102
|
+
uri = URI.parse(params["url"])
|
103
|
+
params = uri.query.split("&").inject({}) { |r,e| k,v = e.qplit("="); r.merge(k => v) }
|
104
|
+
end
|
105
|
+
|
106
|
+
resources = options[:resources] || self.data[collection]
|
107
|
+
page_size = (params["per_page"] || 20).to_i
|
108
|
+
page_index = (params["page"] || 1).to_i
|
109
|
+
total_pages = (resources.size.to_f / page_size.to_f).round
|
110
|
+
offset = (page_index - 1) * page_size
|
111
|
+
links = []
|
112
|
+
|
113
|
+
resource_page = resources.values.reverse.slice(offset, page_size)
|
114
|
+
|
115
|
+
if page_index < total_pages
|
116
|
+
links << url_for_page(collection, page_index - 1, page_size, 'next')
|
117
|
+
end
|
118
|
+
|
119
|
+
if page_index - 1 > 0
|
120
|
+
links << url_for_page(collection, page_index - 1, page_size, 'prev')
|
121
|
+
end
|
122
|
+
|
123
|
+
links << url_for_page(collection, total_pages, page_size, 'last')
|
124
|
+
|
125
|
+
[links.join(", "), resource_page]
|
126
|
+
end
|
127
|
+
|
128
|
+
def url_for_page(collection, page_index, page_size, rel)
|
129
|
+
"<#{File.join(@url, collection.to_s)}?page=#{page_index}&per_page=#{page_size}>; rel=\"#{rel}\""
|
130
|
+
end
|
131
|
+
|
132
|
+
def response(options={})
|
133
|
+
status = options[:status] || 200
|
134
|
+
body = options[:body]
|
135
|
+
headers = {
|
136
|
+
"Content-Type" => "application/json; charset=utf-8"
|
137
|
+
}.merge(options[:headers] || {})
|
138
|
+
|
139
|
+
Imgur::Response.new(status, headers, body).raise!
|
140
|
+
end
|
141
|
+
|
142
|
+
def url_for(path)
|
143
|
+
File.join(@url, path)
|
144
|
+
end
|
145
|
+
|
146
|
+
def stringify_keys(hash)
|
147
|
+
hash.inject({}) { |r,(k,v)| r.merge(k.to_s => v) }
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Imgur::Collection
|
2
|
+
def self.included(klass)
|
3
|
+
klass.send(:extend, Imgur::Collection::Attributes)
|
4
|
+
end
|
5
|
+
|
6
|
+
module Attributes
|
7
|
+
def model_root(model_root)
|
8
|
+
@model_root = model_root
|
9
|
+
end
|
10
|
+
|
11
|
+
def model_request(model_request)
|
12
|
+
@model_request = model_request
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def model_root
|
17
|
+
self.class.instance_variable_get(:@model_root)
|
18
|
+
end
|
19
|
+
|
20
|
+
def model_request
|
21
|
+
self.class.instance_variable_get(:@model_request)
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(id)
|
25
|
+
if data = connection.send(self.model_request, {"id" => id}).body[self.model_root]
|
26
|
+
new(data)
|
27
|
+
else
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
rescue Imgur::Response::NotFound
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
end
|
data/lib/imgur/logger.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class Imgur::Logger
|
2
|
+
attr_reader :logger
|
3
|
+
def initialize(app, logger)
|
4
|
+
@app, @logger = app, logger
|
5
|
+
end
|
6
|
+
def call(env)
|
7
|
+
request = Rack::Request.new(env)
|
8
|
+
headers = env.keys.select{|k| k.match(/HTTP_/)}
|
9
|
+
logger.debug { "--> #{request.request_method} #{request.url} #{request.params}\n#{headers.inject([]){|r,k| r + ["#{k.gsub("HTTP_", "")}: #{env[k].inspect}"]}.join("\n")}\n#{request.body.tap(&:rewind).read}" }
|
10
|
+
request.body.tap(&:rewind)
|
11
|
+
status, headers, body = @app.call(env)
|
12
|
+
real_body = ""
|
13
|
+
body.each{|b| real_body += b.to_s}
|
14
|
+
logger.debug { "<-- #{status} #{request.url}\n#{headers.inject([]){|r,(k,v)| r + ["#{k.upcase}: #{v}"]}.join("\n")}\n#{real_body}" }
|
15
|
+
[status, headers, body]
|
16
|
+
rescue => e
|
17
|
+
logger.error { "<-- #{e.inspect}" }
|
18
|
+
raise
|
19
|
+
end
|
20
|
+
end
|
data/lib/imgur/model.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
class Imgur::Model < Cistern::Model
|
2
|
+
def self.assoc_reader(name)
|
3
|
+
define_method(name) do
|
4
|
+
send("#{name}_id".to_sym) && self.connection.send("#{name}s").get(self.send("#{name}_id"))
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.assoc_writer(name)
|
9
|
+
define_method("#{name}=") do |value|
|
10
|
+
self.send("#{name}_id=", value.id) if value
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.assoc_accessor(name)
|
15
|
+
assoc_reader(name)
|
16
|
+
assoc_writer(name)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Imgur::Client::Image < Imgur::Model
|
2
|
+
identity :id
|
3
|
+
|
4
|
+
attribute :title
|
5
|
+
attribute :link
|
6
|
+
attribute :datetime, type: :integer
|
7
|
+
attribute :mime_type, alias: :type
|
8
|
+
attribute :animated, type: :boolean
|
9
|
+
attribute :width, type: :integer
|
10
|
+
attribute :height, type: :integer
|
11
|
+
attribute :size, type: :integer
|
12
|
+
attribute :views, type: :integer
|
13
|
+
attribute :account_url
|
14
|
+
attribute :bandwidth, type: :integer
|
15
|
+
attribute :ups, type: :integer
|
16
|
+
attribute :downs, type: :integer
|
17
|
+
attribute :score, type: :integer
|
18
|
+
attribute :is_album, type: :boolean
|
19
|
+
|
20
|
+
def open_in_browser
|
21
|
+
Launchy.open(link)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class Imgur::Client::Images < Cistern::Collection
|
2
|
+
include Imgur::PagedCollection
|
3
|
+
include Imgur::Collection
|
4
|
+
|
5
|
+
model Imgur::Client::Image
|
6
|
+
|
7
|
+
model_root "data"
|
8
|
+
model_request :get_image
|
9
|
+
|
10
|
+
collection_root "data"
|
11
|
+
collection_request :get_images
|
12
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Imgur::PagedCollection
|
2
|
+
def self.included(klass)
|
3
|
+
klass.attribute :next_link
|
4
|
+
klass.attribute :prev_link
|
5
|
+
klass.attribute :last_link
|
6
|
+
klass.send(:extend, Imgur::PagedCollection::Attributes)
|
7
|
+
end
|
8
|
+
|
9
|
+
module Imgur::PagedCollection::Attributes
|
10
|
+
def collection_root(collection_root)
|
11
|
+
@collection_root = collection_root
|
12
|
+
end
|
13
|
+
|
14
|
+
def collection_request(collection_request)
|
15
|
+
@collection_request = collection_request
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def collection_root
|
20
|
+
self.class.instance_variable_get(:@collection_root)
|
21
|
+
end
|
22
|
+
|
23
|
+
def collection_request
|
24
|
+
self.class.instance_variable_get(:@collection_request)
|
25
|
+
end
|
26
|
+
|
27
|
+
def next_page
|
28
|
+
all("url" => self.next_link) if self.next_link
|
29
|
+
end
|
30
|
+
|
31
|
+
def previous_page
|
32
|
+
all("url" => self.prev_link) if self.prev_link
|
33
|
+
end
|
34
|
+
|
35
|
+
def last_page
|
36
|
+
all("url" => self.last_link)
|
37
|
+
end
|
38
|
+
|
39
|
+
def ==(comparison_object)
|
40
|
+
comparison_object.equal?(self) ||
|
41
|
+
(comparison_object.is_a?(self.class) &&
|
42
|
+
comparison_object.map(&:identity) == self.map(&:identity))
|
43
|
+
end
|
44
|
+
|
45
|
+
def all(params={})
|
46
|
+
response = connection.send(self.collection_request, params)
|
47
|
+
|
48
|
+
collection = self.clone.load(response.body[self.collection_root])
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Imgur::Client
|
2
|
+
class Real
|
3
|
+
def get_images(params={})
|
4
|
+
path = ""
|
5
|
+
path = path + "/#{params[:resource] || "gallery"}"
|
6
|
+
if params[:section]
|
7
|
+
path = path + "/#{params[:section]}"
|
8
|
+
elsif params[:subreddit]
|
9
|
+
path = path + "/r/#{params[:subreddit]}"
|
10
|
+
end
|
11
|
+
path = path + "/#{params[:sort]}" if params[:sort]
|
12
|
+
path = path + "/#{params[:page] || 0}.json"
|
13
|
+
|
14
|
+
request(
|
15
|
+
:method => :get,
|
16
|
+
:path => path,
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class Imgur::Response
|
2
|
+
class Error < StandardError
|
3
|
+
attr_reader :response
|
4
|
+
def initialize(response)
|
5
|
+
@response = response
|
6
|
+
super({status: response.status, headers: response.headers, body: response.body}.inspect)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
BadRequest = Class.new(Error)
|
11
|
+
NotFound = Class.new(Error)
|
12
|
+
Forbidden = Class.new(Error)
|
13
|
+
Unprocessable = Class.new(Error)
|
14
|
+
|
15
|
+
EXCEPTION_MAPPING = {
|
16
|
+
400 => BadRequest,
|
17
|
+
403 => Forbidden,
|
18
|
+
404 => NotFound,
|
19
|
+
422 => Unprocessable,
|
20
|
+
}
|
21
|
+
|
22
|
+
attr_reader :headers, :status, :body
|
23
|
+
|
24
|
+
def initialize(status, headers, body)
|
25
|
+
@status, @headers, @body = status, headers, body
|
26
|
+
end
|
27
|
+
|
28
|
+
def successful?
|
29
|
+
self.status.to_i < 300 && self.status.to_i > 199
|
30
|
+
end
|
31
|
+
|
32
|
+
def raise!
|
33
|
+
if klass = EXCEPTION_MAPPING[self.status.to_i]
|
34
|
+
raise klass.new(self)
|
35
|
+
else self
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/spec/images_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Bundler.require(:test)
|
2
|
+
Bundler.require(:darwin) if RUBY_PLATFORM.match(/darwin/)
|
3
|
+
require File.expand_path("../../lib/imgur", __FILE__)
|
4
|
+
Dir[File.expand_path("../{shared,support}/*.rb", __FILE__)].each{|f| require(f)}
|
5
|
+
|
6
|
+
Cistern.formatter = Cistern::Formatter::AwesomePrint
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.before(:all) do
|
10
|
+
Imgur::Client.reset! if Imgur::Client.mocking?
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ClientHelper
|
2
|
+
def create_client(attributes={})
|
3
|
+
config = YAML.load_file(File.expand_path("~/.imgurrc")) || YAML.load_file("config/config.yml")
|
4
|
+
merged_attributes = attributes.merge(config: config)
|
5
|
+
merged_attributes.merge!(logger: Logger.new(STDOUT)) if ENV['VERBOSE']
|
6
|
+
|
7
|
+
Imgur::Client.new(merged_attributes)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.include(ClientHelper)
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-imgur
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.01'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Eugene Howe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: multi_json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: cistern
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.1.3
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.1.3
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: addressable
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: excon
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: launchy
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rest-client
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :runtime
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: ''
|
111
|
+
email:
|
112
|
+
- eugene@xtreme-computers.net
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- .document
|
118
|
+
- .gitignore
|
119
|
+
- Gemfile
|
120
|
+
- Gemfile.lock
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.rdoc
|
123
|
+
- Rakefile
|
124
|
+
- imgur.gemspec
|
125
|
+
- lib/imgur.rb
|
126
|
+
- lib/imgur/client.rb
|
127
|
+
- lib/imgur/collection.rb
|
128
|
+
- lib/imgur/logger.rb
|
129
|
+
- lib/imgur/model.rb
|
130
|
+
- lib/imgur/models/image.rb
|
131
|
+
- lib/imgur/models/images.rb
|
132
|
+
- lib/imgur/paged_collection.rb
|
133
|
+
- lib/imgur/requests/get_images.rb
|
134
|
+
- lib/imgur/response.rb
|
135
|
+
- lib/imgur/version.rb
|
136
|
+
- spec/images_spec.rb
|
137
|
+
- spec/spec_helper.rb
|
138
|
+
- spec/support/client.rb
|
139
|
+
homepage: http://github.com/ehowe/ruby-imgur.git
|
140
|
+
licenses: []
|
141
|
+
post_install_message:
|
142
|
+
rdoc_options: []
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
none: false
|
147
|
+
requirements:
|
148
|
+
- - ! '>='
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
153
|
+
requirements:
|
154
|
+
- - ! '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
requirements: []
|
158
|
+
rubyforge_project:
|
159
|
+
rubygems_version: 1.8.24
|
160
|
+
signing_key:
|
161
|
+
specification_version: 3
|
162
|
+
summary: ''
|
163
|
+
test_files:
|
164
|
+
- spec/images_spec.rb
|
165
|
+
- spec/spec_helper.rb
|
166
|
+
- spec/support/client.rb
|
167
|
+
has_rdoc:
|