imgur_url 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.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
18
+ .autotest
19
+ .rspec
data/.rvmrc ADDED
@@ -0,0 +1,3 @@
1
+ rvm_install_on_use_flag=1
2
+ rvm_gemset_create_on_use_flag=1
3
+ rvm 1.9.3@imgur_url
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - jruby-19mode
7
+ - jruby-20mode
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ Ruby gem for dealing with [imgur](http://imgur.com) image URLs.
2
+
3
+ [![Build Status](https://secure.travis-ci.org/6/imgur_url.png?branch=master)](http://travis-ci.org/6/imgur_url)
4
+
5
+
6
+ #### Example usage
7
+ ```ruby
8
+ i = ImgurUrl::Image.new("http://imgur.com/dhuY8")
9
+ i.id
10
+ # => "dhuY8"
11
+ i.url
12
+ # => "http://i.imgur.com/dhuY8.jpg"
13
+ i.url(:small)
14
+ # => "http://i.imgur.com/dhuY8s.jpg"
15
+ i.download_url
16
+ # => "http://imgur.com/download/dhuY8"
17
+ i.permalink_url
18
+ # => "http://imgur.com/dhuY8"
19
+ ```
20
+
21
+ #### Recognized input URL formats
22
+ - `http://imgur.com/{image_id}`
23
+ - `http://i.imgur.com/{image_id}.jpg`
24
+ - `http://imgur.com/download/{image_id}`
25
+ - `http://imgur.com/gallery/{image_id}`
26
+
27
+ #### Supported `url` sizes
28
+ - `:original` (default)
29
+ - `:huge`
30
+ - `:large`
31
+ - `:medium`
32
+ - `:thumb`
33
+ - `:small`
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
data/imgur_url.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'imgur_url/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "imgur_url"
9
+ gem.authors = ["Peter Graham"]
10
+ gem.version = ImgurUrl::VERSION
11
+ gem.summary = %q{Parse imgur URLs}
12
+ gem.description = %q{Parse imgur.com image URLs.}
13
+ gem.files = `git ls-files`.split($/)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.require_paths = ["lib"]
17
+ gem.homepage = 'http://github.com/6/imgur_url'
18
+
19
+ gem.add_dependency "andand"
20
+ gem.add_dependency "activesupport"
21
+ gem.add_development_dependency "rake"
22
+ gem.add_development_dependency "rspec"
23
+ gem.add_development_dependency 'rspec-nc'
24
+ end
@@ -0,0 +1,10 @@
1
+ module ImgurUrl
2
+ class Exception < StandardError
3
+ end
4
+
5
+ class InvalidUrl < ImgurUrl::Exception
6
+ end
7
+
8
+ class InvalidSize < ImgurUrl::Exception
9
+ end
10
+ end
@@ -0,0 +1,39 @@
1
+ module ImgurUrl
2
+ class Image
3
+ SIZES = {
4
+ :small => 's',
5
+ :thumb => 't',
6
+ :medium => 'm',
7
+ :large => 'l',
8
+ :huge => 'h',
9
+ :original => '',
10
+ }
11
+
12
+ ALBUM_PATH = "a"
13
+
14
+ def initialize(url)
15
+ @original_url = url
16
+ unless id.present? && id != ALBUM_PATH
17
+ raise InvalidUrl
18
+ end
19
+ end
20
+
21
+ def id
22
+ @id ||= @original_url.match(%r{imgur\.com/(?:(?:gallery|download)/)?([^.#?/]+)}).andand[1]
23
+ end
24
+
25
+ def url(size = :original)
26
+ raise InvalidSize unless SIZES[size]
27
+ @direct_urls ||= {}
28
+ @direct_urls[size] ||= "http://i.imgur.com/#{id}#{SIZES[size]}.jpg"
29
+ end
30
+
31
+ def permalink_url
32
+ @permalink_url ||= "http://imgur.com/#{id}"
33
+ end
34
+
35
+ def download_url
36
+ @download_url ||= "http://imgur.com/download/#{id}"
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module ImgurUrl
2
+ VERSION = "0.1"
3
+ end
data/lib/imgur_url.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'andand'
2
+ require 'active_support/core_ext/string'
3
+ files = []
4
+ files.concat(Dir[File.join(File.dirname(__FILE__), 'imgur_url','**', '*.rb')])
5
+ files.each { |f| require f }
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ module ImgurUrl
4
+ describe Image do
5
+ shared_examples "with a valid imgur image URL" do
6
+ subject { described_class.new(url) }
7
+
8
+ its(:id) { should == "dhuY8" }
9
+ its(:permalink_url) { should == "http://imgur.com/dhuY8" }
10
+ its(:download_url) { should == "http://imgur.com/download/dhuY8" }
11
+ its(:url) { should == "http://i.imgur.com/dhuY8.jpg" }
12
+
13
+ describe "#url" do
14
+ it "returns the direct URL for the given size" do
15
+ subject.url(:original).should == "http://i.imgur.com/dhuY8.jpg"
16
+ subject.url(:huge).should == "http://i.imgur.com/dhuY8h.jpg"
17
+ subject.url(:large).should == "http://i.imgur.com/dhuY8l.jpg"
18
+ subject.url(:medium).should == "http://i.imgur.com/dhuY8m.jpg"
19
+ subject.url(:thumb).should == "http://i.imgur.com/dhuY8t.jpg"
20
+ subject.url(:small).should == "http://i.imgur.com/dhuY8s.jpg"
21
+ end
22
+
23
+ context "with an invalid size parameter" do
24
+ it "raises an InvalidSize exception" do
25
+ expect { subject.url(:invalid) }.to raise_error(ImgurUrl::InvalidSize)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ context "a default permalink URL" do
32
+ let(:url) { "http://imgur.com/dhuY8" }
33
+ it_behaves_like "with a valid imgur image URL"
34
+ end
35
+
36
+ context "a default permalink URL with query params" do
37
+ let(:url) { "http://imgur.com/dhuY8?tags" }
38
+ it_behaves_like "with a valid imgur image URL"
39
+ end
40
+
41
+ context "a direct URL" do
42
+ let(:url) { "http://i.imgur.com/dhuY8.jpg" }
43
+ it_behaves_like "with a valid imgur image URL"
44
+ end
45
+
46
+ context "a gallery URL" do
47
+ let(:url) { "http://imgur.com/gallery/dhuY8" }
48
+ it_behaves_like "with a valid imgur image URL"
49
+ end
50
+
51
+ context "a download URL" do
52
+ let(:url) { "http://imgur.com/download/dhuY8" }
53
+ it_behaves_like "with a valid imgur image URL"
54
+ end
55
+
56
+ shared_examples "with an invalid URL" do
57
+ it "raises an InvalidUrl error on initialization" do
58
+ expect { described_class.new(url) }.to raise_error(ImgurUrl::InvalidUrl)
59
+ end
60
+ end
61
+
62
+ context "with an imgur album URL" do
63
+ let(:url) { "http://imgur.com/a/CrSqu" }
64
+ it_behaves_like "with an invalid URL"
65
+ end
66
+
67
+ context "with a non-imgur URL" do
68
+ let(:url) { "http://google.com" }
69
+ it_behaves_like "with an invalid URL"
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,10 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+ Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
3
+ require './lib/imgur_url'
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.order = 'random'
10
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imgur_url
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Peter Graham
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: andand
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec-nc
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Parse imgur.com image URLs.
95
+ email:
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files: []
99
+ files:
100
+ - .gitignore
101
+ - .rvmrc
102
+ - .travis.yml
103
+ - Gemfile
104
+ - README.md
105
+ - Rakefile
106
+ - imgur_url.gemspec
107
+ - lib/imgur_url.rb
108
+ - lib/imgur_url/exception.rb
109
+ - lib/imgur_url/image.rb
110
+ - lib/imgur_url/version.rb
111
+ - spec/lib/image_spec.rb
112
+ - spec/spec_helper.rb
113
+ homepage: http://github.com/6/imgur_url
114
+ licenses: []
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 1.8.25
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Parse imgur URLs
137
+ test_files:
138
+ - spec/lib/image_spec.rb
139
+ - spec/spec_helper.rb