imgur2 1.0.0 → 1.1.0

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.
Files changed (3) hide show
  1. data/CHANGELOG.rdoc +6 -0
  2. data/lib/imgur2.rb +22 -1
  3. metadata +4 -4
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ === 1.1.0 / 2011-03-16
2
+
3
+ * 1 major enhancement
4
+
5
+ * Follow redirects until imgur returns the real image url.
6
+
1
7
  === 1.0.0 / 2011-03-10
2
8
 
3
9
  * 1 major enhancement
data/lib/imgur2.rb CHANGED
@@ -9,12 +9,13 @@ require 'json'
9
9
  # client.upload f
10
10
  # }
11
11
  class Imgur2 < Struct.new(:key)
12
- VERSION = '1.0.0'
12
+ VERSION = '1.1.0'
13
13
 
14
14
  def self.run argv
15
15
  client = Imgur2.new '65aea9a07b4f6110c90248ffa247d41a'
16
16
  fh = argv[0] ? File.open(argv[0], 'rb') : $stdin
17
17
  link = client.upload(fh)['upload']['links']['original']
18
+ link = client.follow_redirect link
18
19
  client.paste link
19
20
  puts link
20
21
  ensure
@@ -33,6 +34,26 @@ class Imgur2 < Struct.new(:key)
33
34
  }
34
35
  end
35
36
 
37
+ def follow_redirect url
38
+ follower = lambda do |uri, depth|
39
+ raise "too many redirects" if depth > 3
40
+
41
+ parsed = URI.parse uri
42
+ resp = Net::HTTP.start(parsed.host) { |http|
43
+ head = Net::HTTP::Head.new parsed.path
44
+ http.request head
45
+ }
46
+
47
+ if Net::HTTPRedirection === resp
48
+ follower.call resp['Location'], depth + 1
49
+ else
50
+ uri
51
+ end
52
+ end
53
+
54
+ follower.call url, 0
55
+ end
56
+
36
57
  ##
37
58
  # Tries to find clipboard copy executable and if found puts +link+ in your
38
59
  # clipboard.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imgur2
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-03-10 00:00:00.000000000 -08:00
12
+ date: 2011-03-16 00:00:00.000000000 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: hoe
17
- requirement: &2151870680 !ruby/object:Gem::Requirement
17
+ requirement: &2160778600 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: 2.9.1
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2151870680
25
+ version_requirements: *2160778600
26
26
  description: Upload stuff to imgur. Yay.
27
27
  email:
28
28
  - aaron@tenderlovemaking.com