imgurz 0.0.3 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ecc71374331fee56f55c92f1bf27cb6546ac7086
4
- data.tar.gz: ed5bae0509c89e72c615d6987788d9c6f16a0414
3
+ metadata.gz: 1aac921f29b2c8fcf1585c45f96cdd73359e480e
4
+ data.tar.gz: 69888c27859d2aea46da54496454ca26efdada2d
5
5
  SHA512:
6
- metadata.gz: cb0e70f517d3b4c4b75e0e9b53060b741a862049d1f2cac2c4d9e93dba348744361fa9d75fd47ac6e27f232040d3c418d767dca93cee2282ac6aec63ab5b0cb5
7
- data.tar.gz: 90e245e88b73bc4930679a3d84906d751ec166e85f3ae3507784840e6d776b4d90bb30e185a03bc0eeacdb072f02749d10a45b3feeec0e944fa49e3ae0d5741b
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(:client_key)
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::ENDPOINT
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
- request['Authorization'] = "Client-ID #{client_key}"
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
@@ -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
- VERSION = '0.0.3'
6
- ENDPOINT = 'https://api.imgur.com/3/image'
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.3
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
- - tests/upload.rb
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:
data/tests/upload.rb DELETED
@@ -1,8 +0,0 @@
1
- require './lib/imgurz'
2
-
3
- # To create one go to https://api.imgur.com/oauth2/addclient
4
- YOUR_CLIENT_ID = ''
5
-
6
- # Upload using a path
7
- client = Imgurz::Client.new YOUR_CLIENT_ID
8
- puts client.upload('tests/lama.gif')