imgur_direct 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7d029e91e4ecbd85783152c157a4236559bcaae9
4
+ data.tar.gz: 35f0a1c5ea567086f787ba9c0c7f51c744487211
5
+ SHA512:
6
+ metadata.gz: db5782e3a389cdaf0c1504433e0b249b11e97a37d146ee65162231cc781dd2179e6580dcf1e228c215505ac9d1bfca232c2124beb7e0e8107072c5d3ead10bbf
7
+ data.tar.gz: 97b31a84688eafc86f3ef86a93dc03da75291b531622d30aba20c2182eac4446441bbfb66d3611fa423124261c924bc0508196aae2c9d192350edfefc465873f
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in imgur_direct.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tadas Tamosauskas
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.
@@ -0,0 +1,62 @@
1
+ # ImgurDirect
2
+
3
+ ImgurDirect takes any imgur url and returns direct urls to images within the link
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'imgur_direct'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install imgur_direct
18
+
19
+ ## Usage
20
+
21
+ Make sure to register for [Imgur API usage](http://api.imgur.com/oauth2/addclient) and set `IMGUR_CLIENT_ID` in your environment. Then you can use it in your code as per examples below.
22
+
23
+ Note that it always returns an array. This is to make the api consistent for galleries, albums and direct links.
24
+
25
+ ```ruby
26
+ ENV['IMGUR_CLIENT_ID'] = 'xxxxyyyzzzz'
27
+ require 'imgur_direct'
28
+
29
+ # Direct links (that actually don't need resolving)
30
+ ImgurDirect.new('https://i.imgur.com/neKar7M.gif').urls
31
+ # => %w(https://i.imgur.com/neKar7M.gif)
32
+
33
+ # Gallery links
34
+ ImgurDirect.new('http://imgur.com/gallery/40NpN').urls
35
+ # => ["http://i.imgur.com/JOCymfx.png", "http://i.imgur.com/u0sWGjK.png", "http://i.imgur.com/1cj1Of1.png"
36
+
37
+ # Album links
38
+ ImgurDirect.new('http://imgur.com/a/gYZp4').urls
39
+ # => ["http://i.imgur.com/q7f0zNh.gif", "http://i.imgur.com/y0lEyeH.gif", "http://i.imgur.com/fy7liyk.gif", "http://i.imgur.com/KhB46eF.gif", "http://i.imgur.com/0TUJIHT.gif"]
40
+
41
+ # Indirect links
42
+ ImgurDirect.new('http://imgur.com/cp3b6Ra').urls
43
+ # => %w(http://i.imgur.com/cp3b6Ra.gif)
44
+
45
+ # Returns the same url in case the given url is non-imgur (default on)
46
+ ImgurDirect.new('http://google.com').urls
47
+ # => ["http://google.com"]
48
+ ImgurDirect.new('http://google.com').urls(:fallback_plz)
49
+ # => ["http://google.com"]
50
+
51
+ # Returns empty array in case the given url is non-imgur
52
+ ImgurDirect.new('http://google.com').urls(false)
53
+ # => []
54
+ ```
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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_direct/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "imgur_direct"
8
+ spec.version = ImgurDirect::VERSION
9
+ spec.authors = ["Tadas Tamosauskas"]
10
+ spec.email = ["tadas@pdfcv.com"]
11
+ spec.description = %q{Takes any imgur url and returns direct links to images within the link}
12
+ spec.summary = %q{Any imgur link to direct image urls}
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_development_dependency "rspec"
24
+ spec.add_development_dependency "rspec-context-private"
25
+ end
@@ -0,0 +1,62 @@
1
+ require "imgur_direct/version"
2
+ require "imgur_direct/api"
3
+
4
+ class ImgurDirect
5
+ attr_reader :url
6
+
7
+ def initialize(url)
8
+ @url = url.to_s.strip
9
+ end
10
+
11
+ def urls(fall_back_to_original = true)
12
+ if !resolvable?
13
+ return fall_back_to_original ? [url] : []
14
+ end
15
+
16
+ strategy, image_id = strategy_and_id
17
+ if strategy == 'direct_link'
18
+ [url]
19
+ else
20
+ Api.new(strategy).urls(image_id)
21
+ end
22
+ end
23
+
24
+ def resolvable?
25
+ pattern = /http(s)?:\/\/((m\.)|((www)\.)|((i)\.))?imgur.com\/(a\/)?[a-zA-Z0-9&]+((\.jpg)|(\.gif)|(\.png))?/i
26
+ pattern =~ url
27
+ end
28
+
29
+ private
30
+
31
+ ALBUM_URL_PATTERN = /^https?:\/\/(?:www\.)?imgur\.com\/a\/([a-zA-Z0-9]+)/i
32
+ GALLERY_URL_PATTERN = /^https?:\/\/(?:www\.)?imgur\.com\/gallery\/([a-zA-Z0-9]+)/i
33
+ HASHES_PATTERN = /imgur\.com\/(([a-zA-Z0-9]{5,7}[&,]?)+)/i
34
+ DIRECT_PATTERN = /^https?:\/\/(www\.)?(i\.)?imgur\.com\/(.{3,7})\.((jpg)|(gif)|(png))/i
35
+ IMAGE_PATTERN = /^https?:\/\/(www\.)?(i\.)?imgur\.com\/(.{3,7})$/i
36
+
37
+ def strategy_and_id
38
+ @_strategy_and_id ||= begin
39
+
40
+ case url
41
+ when DIRECT_PATTERN
42
+ return 'direct_link', $3
43
+ when ALBUM_URL_PATTERN
44
+ return 'album', $1
45
+ when GALLERY_URL_PATTERN
46
+ return 'gallery', $1
47
+ when IMAGE_PATTERN
48
+ return 'image', $3
49
+ when HASHES_PATTERN
50
+ return 'image', $1
51
+ else
52
+ raise "Unknown imgur url pattern"
53
+ end
54
+
55
+ end
56
+ end
57
+
58
+ def strategy
59
+ strategy, _ = strategy_and_id
60
+ strategy
61
+ end
62
+ end
@@ -0,0 +1,39 @@
1
+ require 'json'
2
+
3
+ class ImgurDirect
4
+ class Api
5
+ API_URI = URI.parse('https://api.imgur.com')
6
+ CLIENT_ID = "Client-ID #{ENV['IMGUR_CLIENT_ID']}"
7
+ API_VERSION = 3
8
+
9
+ def initialize(endpoint)
10
+ @endpoint = endpoint
11
+ end
12
+
13
+ def urls(image_id)
14
+ request_uri = "#{API_URI.request_uri}#{API_VERSION}/#{@endpoint}/#{image_id}"
15
+ request = Net::HTTP::Get.new(request_uri)
16
+ request.add_field('Authorization', CLIENT_ID)
17
+
18
+ response = web_client.request(request).body
19
+
20
+ data = JSON.parse(response)
21
+ if data['success'] && data['data']['images']
22
+ data['data']['images'].map { |img| img['link'] }
23
+ elsif data['success']
24
+ Array(data['data']['link'])
25
+ else
26
+ raise data.to_s
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def web_client
33
+ http = Net::HTTP.new(API_URI.host, API_URI.port)
34
+ http.use_ssl = true
35
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE # Ain't nobody got time for that :(
36
+ http
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ class ImgurDirect
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe ImgurDirect do
4
+ describe "#resolvable?" do
5
+ it "resolves gallery links" do
6
+ expect(described_class.new('http://imgur.com/gallery/40NpN')).to be_resolvable
7
+ end
8
+
9
+ it "resolves album links" do
10
+ expect(described_class.new('http://imgur.com/a/gYZp4')).to be_resolvable
11
+ end
12
+
13
+ it "resolves direct links" do
14
+ expect(described_class.new('https://i.imgur.com/neKar7M.gif')).to be_resolvable
15
+ end
16
+
17
+ it "resolves indirect links" do
18
+ expect(described_class.new('http://imgur.com/cp3b6Ra')).to be_resolvable
19
+ end
20
+
21
+ it "is false for random links" do
22
+ expect(described_class.new('http://google.com')).to_not be_resolvable
23
+ end
24
+ end
25
+
26
+ describe "#urls" do
27
+ it "resolves gallery links", :vcr do
28
+ expect(described_class.new('http://imgur.com/gallery/40NpN').urls).to eq %w(http://i.imgur.com/JOCymfx.png http://i.imgur.com/u0sWGjK.png http://i.imgur.com/1cj1Of1.png)
29
+ end
30
+
31
+ it "resolves album links", :vcr do
32
+ expect(described_class.new('http://imgur.com/a/gYZp4').urls).to match_array ["http://i.imgur.com/q7f0zNh.gif", "http://i.imgur.com/y0lEyeH.gif", "http://i.imgur.com/fy7liyk.gif", "http://i.imgur.com/KhB46eF.gif", "http://i.imgur.com/0TUJIHT.gif"]
33
+ end
34
+
35
+ it "resolves direct links", :vcr do
36
+ expect(described_class.new('https://i.imgur.com/neKar7M.gif').urls).to eq %w(https://i.imgur.com/neKar7M.gif)
37
+ end
38
+
39
+ it "resolves indirect links", :vcr do
40
+ expect(described_class.new('http://imgur.com/cp3b6Ra').urls).to eq %w(http://i.imgur.com/cp3b6Ra.gif)
41
+ end
42
+
43
+ it "is empty for for non-imgur with no fallback" do
44
+ expect(described_class.new('http://google.com').urls(false)).to eq([])
45
+ end
46
+
47
+ it "is the original link for non-imgur links with fallback" do
48
+ expect(described_class.new('http://google.com').urls(:fallback_plz)).to eq %w[http://google.com]
49
+ end
50
+ end
51
+
52
+ describe "#strategy", :private do
53
+ it "picks known strategies" do
54
+ expect(described_class.new('https://i.imgur.com/neKar7M.gif').strategy).to eq 'direct_link'
55
+ expect(described_class.new('http://imgur.com/gallery/40NpN').strategy).to eq 'gallery'
56
+ expect(described_class.new('http://imgur.com/a/gYZp4').strategy).to eq 'album'
57
+ expect(described_class.new('http://imgur.com/cp3b6Ra').strategy).to eq 'image'
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,7 @@
1
+ require 'imgur_direct'
2
+ require 'rspec/context/private'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.order = 'random'
7
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imgur_direct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tadas Tamosauskas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-05 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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-context-private
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Takes any imgur url and returns direct links to images within the link
70
+ email:
71
+ - tadas@pdfcv.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - imgur_direct.gemspec
82
+ - lib/imgur_direct.rb
83
+ - lib/imgur_direct/api.rb
84
+ - lib/imgur_direct/version.rb
85
+ - spec/imgur_direct_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Any imgur link to direct image urls
111
+ test_files:
112
+ - spec/imgur_direct_spec.rb
113
+ - spec/spec_helper.rb
114
+ has_rdoc: