ruby_imgur 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/imgur-old.rb +85 -0
- data/lib/imgur.rb +37 -42
- data/lib/imgur_rails.rb +4 -4
- metadata +3 -2
data/lib/imgur-old.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'json'
|
4
|
+
require 'base64'
|
5
|
+
|
6
|
+
class Imgur
|
7
|
+
|
8
|
+
IMAGE_SIZES = { :small => :s, :large => :l }
|
9
|
+
|
10
|
+
# add an API key to use for uploads
|
11
|
+
def self.api_key=( api_key )
|
12
|
+
@@api_key = api_key.to_s.chomp
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.upload( options = {} )
|
16
|
+
data = options[:data] ||
|
17
|
+
options[:url] ||
|
18
|
+
Base64.encode64 open(options[:filename]).read
|
19
|
+
post_data = { :key => api_key, :image => data }
|
20
|
+
post_url = generate_url :method => :upload
|
21
|
+
post_uri = URI.parse post_url
|
22
|
+
response = JSON (Net::HTTP.post_form post_uri, post_data).body
|
23
|
+
{ :hash => response["upload"]["image"]["hash"],
|
24
|
+
:delete_hash => response["upload"]["image"]["deletehash"],
|
25
|
+
:filetype => response["upload"]["image"]["type"].gsub("image\/","") }
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.copy( url )
|
29
|
+
upload :url => url
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.upload_file( filename )
|
33
|
+
upload :filename => filename
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.get( hash, options = {} )
|
37
|
+
open url_for hash, options
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.stats
|
41
|
+
json_get :method => :stats
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.delete( delete_hash )
|
45
|
+
json_get( :hash => delete_hash, :method => :delete )["delete"]["message"] == "Success"
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.album( hash, options = {} )
|
49
|
+
json_get :hash => hash, :method => :album
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.url_for( hash, options = {} )
|
53
|
+
size = IMAGE_SIZES[options[:size]]
|
54
|
+
filetype = options[:filetype] || :png
|
55
|
+
if filetype == :detect
|
56
|
+
filetype = json_get( :hash => hash, :method => :image )["image"]["image"]["type"].gsub "image\/", ""
|
57
|
+
end
|
58
|
+
"http://i.imgur.com/#{hash}#{size}.#{filetype}"
|
59
|
+
end
|
60
|
+
|
61
|
+
class << self
|
62
|
+
alias_method :[], :url_for
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def self.api_key
|
68
|
+
@@api_key
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.json_get( options = {} )
|
72
|
+
JSON open(generate_url options).read
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.generate_url( options = {} )
|
76
|
+
hash = options[:hash]
|
77
|
+
method = options[:method]
|
78
|
+
url = "http://api.imgur.com/2/"
|
79
|
+
url += method.to_s
|
80
|
+
url += "/" + hash.to_s if hash
|
81
|
+
url += ".json"
|
82
|
+
url
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
data/lib/imgur.rb
CHANGED
@@ -1,26 +1,32 @@
|
|
1
|
-
require '
|
2
|
-
require 'net/http'
|
1
|
+
require 'httpclient'
|
3
2
|
require 'json'
|
4
3
|
require 'base64'
|
5
4
|
|
6
5
|
class Imgur
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
ImgurException = Class.new Exception
|
8
|
+
|
9
|
+
IMAGE_SIZES = { :small => :s, :medium => :m, :large => :l }
|
10
|
+
|
11
|
+
def self.client_id=( client_id )
|
12
|
+
@@client_id = client_id
|
11
13
|
end
|
12
14
|
|
13
15
|
def self.upload( options = {} )
|
14
16
|
data = options[:data] ||
|
15
17
|
options[:url] ||
|
16
18
|
Base64.encode64(open(options[:filename]).read)
|
17
|
-
post_data = { :
|
19
|
+
post_data = { :image => data }
|
18
20
|
post_url = generate_url :method => :upload
|
19
|
-
|
20
|
-
|
21
|
-
{ :hash => response["
|
22
|
-
:delete_hash => response["
|
23
|
-
:filetype => response["
|
21
|
+
response = JSON http.post(post_url, post_data, header).body
|
22
|
+
raise ImgurException.new response["data"]["error"] if response["data"]["error"]
|
23
|
+
{ :hash => response["data"]["id"],
|
24
|
+
:delete_hash => response["data"]["deletehash"],
|
25
|
+
:filetype => response["data"]["link"].match(/\.(\w*)\Z/)[1] }
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.copy( url )
|
29
|
+
upload :url => url
|
24
30
|
end
|
25
31
|
|
26
32
|
def self.upload_file( filename )
|
@@ -28,59 +34,48 @@ class Imgur
|
|
28
34
|
end
|
29
35
|
|
30
36
|
def self.get( hash, options = {} )
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.stats
|
35
|
-
json_get :method => :stats
|
37
|
+
http.get_content url_for hash, options
|
36
38
|
end
|
37
39
|
|
38
40
|
def self.delete( delete_hash )
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
def self.album( hash, options = {} )
|
43
|
-
json_get :hash => hash, :method => :album
|
41
|
+
url = generate_url :hash => delete_hash, :method => :image
|
42
|
+
response = JSON http.delete( url, {}, header ).body
|
43
|
+
response["success"] == true
|
44
44
|
end
|
45
45
|
|
46
46
|
def self.url_for( hash, options = {} )
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
# fix this it's ugly
|
52
|
-
options[:filetype] = json_get( :hash => hash, :method => :image )["image"]["image"]["type"].gsub("image\/","")
|
47
|
+
size = IMAGE_SIZES[options[:size].to_sym] if options[:size]
|
48
|
+
filetype = options[:filetype] || :png
|
49
|
+
if filetype == :detect
|
50
|
+
filetype = json_get( :hash => hash, :method => :image )["data"]["link"].match(/\.(\w*)\Z/)[1]
|
53
51
|
end
|
54
|
-
"http://i.imgur.com/#{hash}#{
|
55
|
-
end
|
56
|
-
|
57
|
-
def self.debug=( debug_setting )
|
58
|
-
@debug = debug_setting
|
59
|
-
end
|
60
|
-
|
61
|
-
class << self
|
62
|
-
alias_method :[], :url_for
|
52
|
+
"http://i.imgur.com/#{hash}#{size}.#{filetype}"
|
63
53
|
end
|
64
54
|
|
65
55
|
private
|
66
56
|
|
67
|
-
def
|
68
|
-
|
69
|
-
thing_to_debug
|
57
|
+
def self.header
|
58
|
+
{ "Authorization" => "Client-ID #{@@client_id}" }
|
70
59
|
end
|
71
60
|
|
72
61
|
def self.json_get( options = {} )
|
73
|
-
JSON
|
62
|
+
response = JSON http.get_content generate_url(options), {}, header
|
63
|
+
raise ImgurException.new response["data"]["error"] if response["data"]["error"]
|
74
64
|
end
|
75
65
|
|
76
66
|
def self.generate_url( options = {} )
|
77
67
|
hash = options[:hash]
|
78
68
|
method = options[:method]
|
79
|
-
url = "
|
69
|
+
url = "https://api.imgur.com/3/"
|
80
70
|
url += method.to_s
|
81
71
|
url += "/" + hash.to_s if hash
|
82
72
|
url += ".json"
|
83
|
-
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.http
|
76
|
+
@@http ||= HTTPClient.new
|
77
|
+
@@http.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
78
|
+
@@http
|
84
79
|
end
|
85
80
|
|
86
81
|
end
|
data/lib/imgur_rails.rb
CHANGED
@@ -23,13 +23,13 @@ module ImgurRails
|
|
23
23
|
module InstanceMethods
|
24
24
|
|
25
25
|
def url
|
26
|
-
|
27
|
-
Imgur.url_for self.image_hash, :filetype =>
|
26
|
+
type = ( self.respond_to? :filetype ) ? self.filetype : :detect
|
27
|
+
Imgur.url_for self.image_hash, :filetype => type
|
28
28
|
end
|
29
29
|
|
30
30
|
def thumbnail( size = :small )
|
31
|
-
|
32
|
-
Imgur.url_for self.image_hash, :filetype =>
|
31
|
+
type = { :small => :jpeg, :medium => :jpg }[size]
|
32
|
+
Imgur.url_for self.image_hash, :filetype => type, :size => size
|
33
33
|
end
|
34
34
|
|
35
35
|
def is_image?
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_imgur
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email: fraser.m.murray@gmail.com
|
@@ -20,6 +20,7 @@ files:
|
|
20
20
|
- lib/imgur_rails.rb
|
21
21
|
- lib/test
|
22
22
|
- lib/imgur.rb
|
23
|
+
- lib/imgur-old.rb
|
23
24
|
homepage: ''
|
24
25
|
licenses: []
|
25
26
|
post_install_message:
|