imgur-api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +1 -0
- data/imgur.gemspec +25 -0
- data/lib/imgur.rb +190 -0
- data/lib/imgur/version.rb +3 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ab91171bd2f3ec61e508f2b9b8e85eac18479562
|
4
|
+
data.tar.gz: 41bd543ea770fe094d6c0903de6e862954dc4f6e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7be725a58539db5d13b304476e7e1dd412780aea4b7d12eb62214796f379e44564d060a59598d5dcbddc5301305b92d327b0de9855aa5fcc494d4b61f26da2cc
|
7
|
+
data.tar.gz: f554e94eeac10659d4f1b3a6dc7b972b8ab86d05239b5720ea18b136ff200a426f5aefcd64e5269e2402d9c1bc3121b46d00284361c52eae1965abadde1060ad
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 August
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Imgur
|
2
|
+
|
3
|
+
Ruby wrapper for the Imgur API.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
If you're using Bundler:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'imgur', github: 'augustt198/imgur'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
For anonymous usage, create a new client with your Client-ID
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
client = Imgur.new(client_id) # or Imgur::Client.new(client_id)
|
19
|
+
```
|
20
|
+
|
21
|
+
To upload an image, first create a `Imgur::LocalImage`
|
22
|
+
```ruby
|
23
|
+
image = Imgur::LocalImage.new('path/to/image', title: 'Awesome photo')
|
24
|
+
```
|
25
|
+
|
26
|
+
Then use the client to upload it and recieve a `Imgur::Image`
|
27
|
+
```ruby
|
28
|
+
uploaded = client.upload(image)
|
29
|
+
# uploaded.link => http://i.imgur.com/...
|
30
|
+
```
|
31
|
+
|
32
|
+
Creating an album is super easy!
|
33
|
+
```ruby
|
34
|
+
# The first argument can also be an array of images, or nil for a blank album.
|
35
|
+
album = client.new_album(uploaded, title: 'My Photography')
|
36
|
+
|
37
|
+
# album.link => http://imgur.com/a/...
|
38
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/imgur.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'imgur/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'imgur-api'
|
8
|
+
spec.version = Imgur::VERSION
|
9
|
+
spec.authors = ['August']
|
10
|
+
spec.email = ['augustt198@gmail.com']
|
11
|
+
spec.description = %q{Imgur API wrapper}
|
12
|
+
spec.summary = %q{Imgur API wrapper}
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_dependency 'httparty'
|
24
|
+
|
25
|
+
end
|
data/lib/imgur.rb
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'imgur/version'
|
2
|
+
require 'httparty'
|
3
|
+
|
4
|
+
module Imgur
|
5
|
+
|
6
|
+
HTML_PATH = 'https://imgur.com/'
|
7
|
+
API_PATH = 'https://api.imgur.com/3/'
|
8
|
+
UPLOAD_PATH = 'upload'
|
9
|
+
IMAGE_PATH = 'image/'
|
10
|
+
ALBUM_GET_PATH = 'album/'
|
11
|
+
ALBUM_CREATE_PATH = 'album'
|
12
|
+
|
13
|
+
class Client
|
14
|
+
attr_accessor :client_id
|
15
|
+
|
16
|
+
def initialize(client_id)
|
17
|
+
@client_id = client_id
|
18
|
+
end
|
19
|
+
|
20
|
+
def post(url, body={})
|
21
|
+
HTTParty.post(url, body: body, headers: auth_header)
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(url, data={})
|
25
|
+
HTTParty.get(url, query: data, headers: auth_header)
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_image(id)
|
29
|
+
url = API_PATH + IMAGE_PATH + id
|
30
|
+
resp = get(url).parsed_response
|
31
|
+
Image.new(resp['data'])
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_album(id)
|
35
|
+
url = API_PATH + ALBUM_GET_PATH + id
|
36
|
+
resp = get(url).parsed_response
|
37
|
+
Album.new resp['data']
|
38
|
+
end
|
39
|
+
|
40
|
+
def upload(local_file)
|
41
|
+
local_file.file.rewind
|
42
|
+
image = local_file.file.read
|
43
|
+
body = {image: image, type: 'file'}
|
44
|
+
body[:title] = local_file.title if local_file.title
|
45
|
+
body[:description] = local_file.description if local_file.description
|
46
|
+
body[:album] = local_file.album_id if local_file.album_id
|
47
|
+
resp = post(API_PATH + UPLOAD_PATH, body).parsed_response
|
48
|
+
# the Imgur API doesn't send title or description back apparently.
|
49
|
+
data = resp['data'].merge({'title' => body[:title], 'description' => body[:description]})
|
50
|
+
Image.new(data)
|
51
|
+
end
|
52
|
+
|
53
|
+
def new_album(images=nil, options={})
|
54
|
+
image_ids = []
|
55
|
+
if images.is_a? Array
|
56
|
+
if images[0].is_a? Image
|
57
|
+
images.each do |img|
|
58
|
+
image_ids << img.id
|
59
|
+
end
|
60
|
+
elsif images[0].is_a? String
|
61
|
+
image_ids = images
|
62
|
+
end
|
63
|
+
elsif
|
64
|
+
if images.is_a? Image
|
65
|
+
image_ids << images.id
|
66
|
+
elsif images.is_a? String
|
67
|
+
image_ids << images
|
68
|
+
end
|
69
|
+
end
|
70
|
+
options[:cover] = options[:cover].id if options[:cover].is_a? Image
|
71
|
+
body = {ids: image_ids}.merge options
|
72
|
+
url = API_PATH + ALBUM_CREATE_PATH
|
73
|
+
resp = post(url, body).parsed_response
|
74
|
+
id = resp['data']['id']
|
75
|
+
album = get_album id
|
76
|
+
album.deletehash = resp['data']['deletehash']
|
77
|
+
album
|
78
|
+
end
|
79
|
+
|
80
|
+
def auth_header
|
81
|
+
{'Authorization' => 'Client-ID ' + @client_id}
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
class Album
|
87
|
+
attr_accessor :id
|
88
|
+
attr_accessor :title
|
89
|
+
attr_accessor :description
|
90
|
+
attr_accessor :date
|
91
|
+
attr_accessor :cover
|
92
|
+
attr_accessor :cover_width
|
93
|
+
attr_accessor :cover_height
|
94
|
+
attr_accessor :account_url
|
95
|
+
attr_accessor :privacy
|
96
|
+
attr_accessor :layout
|
97
|
+
attr_accessor :views
|
98
|
+
attr_accessor :link
|
99
|
+
attr_accessor :deletehash
|
100
|
+
attr_accessor :images_count
|
101
|
+
attr_accessor :images
|
102
|
+
|
103
|
+
def initialize(data)
|
104
|
+
@id = data['id']
|
105
|
+
@title = data['title']
|
106
|
+
@description = data['description']
|
107
|
+
@date = Time.at data['datetime']
|
108
|
+
@cover = data['cover']
|
109
|
+
@cover_width = data['cover_width']
|
110
|
+
@account_url = data['account_url']
|
111
|
+
@privacy = data['privacy']
|
112
|
+
@layout = data['layout']
|
113
|
+
@views = data['views']
|
114
|
+
@link = data['link']
|
115
|
+
@deletehash = data['deletehash']
|
116
|
+
@images_count = data['images_count']
|
117
|
+
@images = []
|
118
|
+
data['images'].each do |img|
|
119
|
+
@images << Image.new(img)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# Represents an image stored on the computer
|
125
|
+
class LocalImage
|
126
|
+
attr_accessor :file
|
127
|
+
attr_accessor :title
|
128
|
+
attr_accessor :description
|
129
|
+
attr_accessor :album_id
|
130
|
+
|
131
|
+
def initialize(file, options={})
|
132
|
+
if file.is_a? String
|
133
|
+
@file = File.open file, 'rb'
|
134
|
+
else
|
135
|
+
@file = file
|
136
|
+
end
|
137
|
+
@title = options[:title]
|
138
|
+
@description = options[:description]
|
139
|
+
@album_id = options[:album_id]
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
# Represents an image on Imgur
|
145
|
+
class Image
|
146
|
+
attr_accessor :id
|
147
|
+
attr_accessor :title
|
148
|
+
attr_accessor :description
|
149
|
+
attr_accessor :date # Time object of :datetime
|
150
|
+
attr_accessor :type
|
151
|
+
attr_accessor :animated
|
152
|
+
attr_accessor :width
|
153
|
+
attr_accessor :height
|
154
|
+
attr_accessor :size
|
155
|
+
attr_accessor :views
|
156
|
+
attr_accessor :bandwidth
|
157
|
+
attr_accessor :favorite
|
158
|
+
attr_accessor :nsfw
|
159
|
+
attr_accessor :section
|
160
|
+
attr_accessor :deletehash
|
161
|
+
attr_accessor :link
|
162
|
+
attr_accessor :html_link
|
163
|
+
|
164
|
+
def initialize(data)
|
165
|
+
@id = data['id']
|
166
|
+
@title = data['title']
|
167
|
+
@description = data['description']
|
168
|
+
@date = Time.at data['datetime']
|
169
|
+
@type = data['type']
|
170
|
+
@animated = data['animated']
|
171
|
+
@width = data['width']
|
172
|
+
@height = data['height']
|
173
|
+
@size = data['size']
|
174
|
+
@views = data['views']
|
175
|
+
@bandwidth = data['bandwidth']
|
176
|
+
@favorite = data['favorite']
|
177
|
+
@nsfw = data['nsfw']
|
178
|
+
@section = data['section']
|
179
|
+
@deletehash = data['deletehash']
|
180
|
+
@link = data['link']
|
181
|
+
@html_link = HTML_PATH + @id
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
def self.new(client_id)
|
187
|
+
Client.new client_id
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: imgur-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- August
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: httparty
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Imgur API wrapper
|
56
|
+
email:
|
57
|
+
- augustt198@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- imgur.gemspec
|
68
|
+
- lib/imgur.rb
|
69
|
+
- lib/imgur/version.rb
|
70
|
+
homepage: ''
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.2.0.rc.1
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Imgur API wrapper
|
94
|
+
test_files: []
|