imgurz 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/imgurz/client.rb +15 -3
- data/lib/imgurz/token.rb +27 -0
- data/lib/imgurz.rb +3 -5
- data/tests/test.rb +23 -0
- metadata +3 -2
- data/tests/upload.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1aac921f29b2c8fcf1585c45f96cdd73359e480e
|
4
|
+
data.tar.gz: 69888c27859d2aea46da54496454ca26efdada2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af745926a647d86d0ac923f98f436d19bb8c8993b00aa1006ce00c16fee8b18852e6e87db6412dd676a97569d935c09b96ec4f2608073b7d8daf703c131c0930
|
7
|
+
data.tar.gz: 3aab0a39330f60945bb1b2123804831357e1848023c24177d7c0e6dfb88445a3d7d15837654fde8212f6f99149f6bbe606dcbac09328a0e15297eb61eb9b7701
|
data/lib/imgurz/client.rb
CHANGED
@@ -5,7 +5,17 @@ module Imgurz
|
|
5
5
|
require 'net/https'
|
6
6
|
require 'json'
|
7
7
|
|
8
|
-
class Client < Struct.new(:
|
8
|
+
class Client < Struct.new(:key)
|
9
|
+
|
10
|
+
def as_anonymous
|
11
|
+
@method = 'anonymous'
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def as_regsitered
|
16
|
+
@method = 'registered'
|
17
|
+
self
|
18
|
+
end
|
9
19
|
|
10
20
|
def upload(image)
|
11
21
|
return self.upload_file(image) if image.kind_of?(File) # already a file
|
@@ -27,11 +37,13 @@ module Imgurz
|
|
27
37
|
|
28
38
|
def upload_image_content(file_content)
|
29
39
|
return false unless file_content.kind_of?(String)
|
30
|
-
uri = URI.parse Imgurz::
|
40
|
+
uri = URI.parse Imgurz::UPLOAD_ENDPOINT
|
31
41
|
http = Net::HTTP.new(uri.host, uri.port)
|
32
42
|
http.use_ssl = true
|
33
43
|
request = Net::HTTP::Post.new uri.path
|
34
|
-
|
44
|
+
self.as_anonymous unless @method
|
45
|
+
request['Authorization'] = "Client-ID #{key}" if @method=='anonymous'
|
46
|
+
request['Authorization'] = "Bearer #{key}" if @method=='registered'
|
35
47
|
request.set_form_data(image:file_content)
|
36
48
|
response = http.request(request)
|
37
49
|
JSON.parse response.body
|
data/lib/imgurz/token.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Imgurz
|
2
|
+
extend self
|
3
|
+
|
4
|
+
require 'net/http'
|
5
|
+
require 'net/https'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
class Token < Struct.new(:client_id, :client_secret)
|
9
|
+
|
10
|
+
def refresh(refresh_token)
|
11
|
+
uri = URI.parse Imgurz::TOKEN_ENDPOINT
|
12
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
13
|
+
http.use_ssl = true
|
14
|
+
request = Net::HTTP::Post.new uri.path
|
15
|
+
request.set_form_data({
|
16
|
+
refresh_token: refresh_token,
|
17
|
+
client_id: client_id,
|
18
|
+
client_secret: client_secret,
|
19
|
+
grant_type: 'refresh_token'
|
20
|
+
})
|
21
|
+
response = http.request(request)
|
22
|
+
JSON.parse response.body
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/lib/imgurz.rb
CHANGED
@@ -2,11 +2,9 @@ IMGURZ_BASE_PATH = File.dirname(__FILE__)
|
|
2
2
|
|
3
3
|
module Imgurz
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
UPLOAD_ENDPOINT = 'https://api.imgur.com/3/image'
|
6
|
+
TOKEN_ENDPOINT = 'https://api.imgur.com/oauth2/token'
|
7
7
|
|
8
8
|
end
|
9
9
|
|
10
|
-
[
|
11
|
-
'client'
|
12
|
-
].each{|f| require "#{IMGURZ_BASE_PATH}/imgurz/#{f}" }
|
10
|
+
['client','token'].each{|f| require "#{IMGURZ_BASE_PATH}/imgurz/#{f}" }
|
data/tests/test.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require './lib/imgurz'
|
2
|
+
|
3
|
+
# To create one go to https://api.imgur.com/oauth2/addclient
|
4
|
+
YOUR_CLIENT_ID = ''
|
5
|
+
YOUR_CLIENT_SECRET = ''
|
6
|
+
|
7
|
+
# To get one go to https://api.imgur.com/oauth2#authorization
|
8
|
+
YOUR_TOKEN = ''
|
9
|
+
YOUR_REFRESH_TOKEN = ''
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
# Upload using a path, as an anononymous user
|
14
|
+
client = Imgurz::Client.new YOUR_CLIENT_ID
|
15
|
+
puts client.anonymous.upload('tests/lama.gif')
|
16
|
+
|
17
|
+
# Upload using a path, as a registered user (with a token)
|
18
|
+
client = Imgurz::Client.new YOUR_TOKEN
|
19
|
+
puts client.as_regsitered.upload('tests/lama.gif')
|
20
|
+
|
21
|
+
# Refresh token
|
22
|
+
token = Imgurz::Token.new(YOUR_CLIENT_ID, YOUR_CLIENT_SECRET)
|
23
|
+
puts token.refresh(YOUR_REFRESH_TOKEN)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imgurz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maxime Zielony
|
@@ -18,7 +18,8 @@ extra_rdoc_files: []
|
|
18
18
|
files:
|
19
19
|
- lib/imgurz.rb
|
20
20
|
- lib/imgurz/client.rb
|
21
|
-
-
|
21
|
+
- lib/imgurz/token.rb
|
22
|
+
- tests/test.rb
|
22
23
|
- tests/lama.gif
|
23
24
|
homepage: https://github.com/xumi/imgurz
|
24
25
|
licenses:
|