http_content_type 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3bdaf6bde10e228f63867b5994cc3c9e1d6c514
4
+ data.tar.gz: 99274fe10583a1cbe8d04dd260874b29507ed8ec
5
+ SHA512:
6
+ metadata.gz: 151838a3c37cb555a9de9aecfb3aa8fa774f75bd1ab30b67c295dbf2aa90ce66488c59120d34c138c99c5e94ddee7f7ad7627e6c915f3ee627a0d13a2e05581c
7
+ data.tar.gz: 7155b8a36f44e6d8e3000f7743573cd6acae8961acf2b034801aa4ce8391b4f372e3016cdbd7fff1bb87e9a0eaaffde51323b1114fe3c61650c5417091d5aa45
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## Master
2
+
3
+ ### 0.0.1 - 4 September, 2013
4
+
5
+ - Initial release. ([@rymai][])
6
+ <!--- The following link definition list is generated by PimpMyChangelog --->
7
+ [@rymai]: https://github.com/rymai
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jilion SA
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.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # HttpContentType
2
+ [![Gem Version](https://badge.fury.io/rb/http_content_type.png)](http://badge.fury.io/rb/http_content_type) [![Build Status](https://travis-ci.org/jilion/http_content_type.png?branch=master)](https://travis-ci.org/jilion/http_content_type) [![Dependency Status](https://gemnasium.com/jilion/http_content_type.png)](https://gemnasium.com/jilion/http_content_type) [![Code Climate](https://codeclimate.com/github/jilion/http_content_type.png)](https://codeclimate.com/github/jilion/http_content_type) [![Coverage Status](https://coveralls.io/repos/jilion/http_content_type/badge.png?branch=master)](https://coveralls.io/r/jilion/http_content_type)
3
+
4
+ This gem allows you to check the Content-Type of any asset accessible via HTTP.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'http_content_type'
12
+ ```
13
+ And then execute:
14
+
15
+ ```shell
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```shell
22
+ $ gem install http_content_type
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ checker = HttpContentType::Checker.new('http://domain.com/video.mp4')
29
+
30
+ checker.found? # => true (asset doesn't return a 404)
31
+ checker.expected_content_type # => 'video/mp4' (the expected content type is based on file extension)
32
+ checker.content_type # => 'video/mp4'
33
+ checker.valid_content_type? # => true
34
+ ```
35
+
36
+ ## Development
37
+
38
+ * Documentation hosted at [RubyDoc](http://rubydoc.info/github/jilion/http_content_type/master/frames).
39
+ * Source hosted at [GitHub](https://github.com/jilion/http_content_type).
40
+
41
+ Pull requests are very welcome! Please try to follow these simple rules if applicable:
42
+
43
+ * Please create a topic branch for every separate change you make.
44
+ * Make sure your patches are well tested. All specs must pass on [Travis CI](https://travis-ci.org/jilion/http_content_type).
45
+ * Update the [Yard](http://yardoc.org/) documentation.
46
+ * Update the [README](https://github.com/jilion/http_content_type/blob/master/README.md).
47
+ * Update the [CHANGELOG](https://github.com/jilion/http_content_type/blob/master/CHANGELOG.md) for noteworthy changes (don't forget to run `bundle exec pimpmychangelog` and watch the magic happen)!
48
+ * Please **do not change** the version number.
49
+
50
+ ### Author
51
+
52
+ * [Rémy Coutable](https://github.com/rymai) ([@rymai](http://twitter.com/rymai), [rymai.me](http://rymai.me))
53
+
54
+ ### Contributors
55
+
56
+ [https://github.com/jilion/http_content_type/graphs/contributors](https://github.com/jilion/http_content_type/contributors)
@@ -0,0 +1,2 @@
1
+ require 'http_content_type/version'
2
+ require 'http_content_type/checker'
@@ -0,0 +1,70 @@
1
+ require 'net/http'
2
+
3
+ module HttpContentType
4
+
5
+ class Checker
6
+ UNKNOWN_CONTENT_TYPE_RESPONSE = { 'found' => true, 'content-type' => 'unknown' }
7
+ FILE_NOT_FOUND_RESPONSE = { 'found' => false, 'content-type' => 'unknown' }
8
+
9
+ def initialize(asset_url)
10
+ @asset_url = asset_url
11
+ end
12
+
13
+ def found?
14
+ _head['found']
15
+ end
16
+
17
+ def expected_content_type
18
+ @expected_content_type ||= case File.extname(@asset_url).sub(/^\./, '')
19
+ when 'mp4', 'm4v', 'mov'
20
+ 'video/mp4'
21
+ when 'webm'
22
+ 'video/webm'
23
+ when 'ogv', 'ogg'
24
+ 'video/ogg'
25
+ else
26
+ 'unknown'
27
+ end
28
+ end
29
+
30
+ def content_type
31
+ _head['content-type']
32
+ end
33
+
34
+ def valid_content_type?
35
+ content_type == expected_content_type
36
+ end
37
+
38
+ private
39
+
40
+ def _clean_uri
41
+ @_clean_uri ||= URI.parse(URI.escape(@asset_url))
42
+ end
43
+
44
+ def _head_options
45
+ @_head_options ||= { use_ssl: _clean_uri.scheme == 'https', read_timeout: 3 }
46
+ end
47
+
48
+ def _head
49
+ @response ||= begin
50
+ response = Net::HTTP.start(_clean_uri.host, _clean_uri.port, _head_options) do |http|
51
+ http.head(_clean_uri.path)
52
+ end
53
+
54
+ case response
55
+ when Net::HTTPSuccess, Net::HTTPRedirection
56
+ { 'found' => true, 'content-type' => response['content-type'] }
57
+ when Net::HTTPClientError
58
+ FILE_NOT_FOUND_RESPONSE
59
+ else
60
+ UNKNOWN_CONTENT_TYPE_RESPONSE
61
+ end
62
+
63
+ rescue
64
+ UNKNOWN_CONTENT_TYPE_RESPONSE
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,3 @@
1
+ module HttpContentType
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ require 'http_content_type/checker'
4
+
5
+ describe HttpContentType::Checker do
6
+ let(:checker) { described_class.new('http://foo.com/bar.mp4') }
7
+
8
+ describe '#found?' do
9
+ context 'asset is not found' do
10
+ before { checker.stub(:_head).and_return({ 'found' => false }) }
11
+
12
+ it 'return false' do
13
+ checker.should_not be_found
14
+ end
15
+ end
16
+
17
+ context 'asset is found' do
18
+ before { checker.stub(:_head).and_return({ 'found' => true, 'content-type' => 'video/mp4' }) }
19
+
20
+ it 'return true' do
21
+ checker.should be_found
22
+ end
23
+ end
24
+ end
25
+
26
+ describe '#expected_content_type' do
27
+ context '.mp4 video' do
28
+ subject { described_class.new('http://foo.com/bar.mp4') }
29
+ its(:expected_content_type) { eq 'video/mp4' }
30
+ end
31
+
32
+ context '.m4v video' do
33
+ subject { described_class.new('http://foo.com/bar.m4v') }
34
+ its(:expected_content_type) { eq 'video/mp4' }
35
+ end
36
+
37
+ context '.mov video' do
38
+ subject { described_class.new('http://foo.com/bar.mov') }
39
+ its(:expected_content_type) { eq 'video/mp4' }
40
+ end
41
+
42
+ context '.webm video' do
43
+ subject { described_class.new('http://foo.com/bar.webm') }
44
+ its(:expected_content_type) { eq 'video/webm' }
45
+ end
46
+
47
+ context '.ogg video' do
48
+ subject { described_class.new('http://foo.com/bar.ogg') }
49
+ its(:expected_content_type) { eq 'video/ogg' }
50
+ end
51
+
52
+ context '.ogv video' do
53
+ subject { described_class.new('http://foo.com/bar.ogv') }
54
+ its(:expected_content_type) { eq 'video/ogg' }
55
+ end
56
+ end
57
+
58
+ describe '#content_type' do
59
+ context 'asset has valid content type' do
60
+ before { checker.stub(:_head).and_return({ 'found' => true, 'content-type' => 'video/mp4' }) }
61
+
62
+ it 'returns the actual content type' do
63
+ expect(checker.content_type).to eq 'video/mp4'
64
+ end
65
+ end
66
+ end
67
+
68
+ describe '#valid_content_type?' do
69
+ context 'asset has valid content type' do
70
+ before { checker.stub(:_head).and_return({ 'found' => true, 'content-type' => 'video/mp4' }) }
71
+
72
+ it 'returns true' do
73
+ checker.should be_valid_content_type
74
+ end
75
+ end
76
+
77
+ context 'asset has invalid content type' do
78
+ before { checker.stub(:_head).and_return({ 'found' => true, 'content-type' => 'video/mov' }) }
79
+
80
+ it 'returns false' do
81
+ checker.should_not be_valid_content_type
82
+ end
83
+ end
84
+ end
85
+
86
+ end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+
5
+ require 'http_content_type'
6
+ require 'rspec'
7
+
8
+ RSpec.configure do |config|
9
+ config.color_enabled = true
10
+ config.order = :random
11
+ config.filter_run :focus => true
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_content_type
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rémy Coutable
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-04 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
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
+ description: This gem allows you to check the Content-Type of any HTTP-accessible
42
+ asset.
43
+ email:
44
+ - remy@rymai.me
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/http_content_type/checker.rb
50
+ - lib/http_content_type/version.rb
51
+ - lib/http_content_type.rb
52
+ - CHANGELOG.md
53
+ - LICENSE.md
54
+ - README.md
55
+ - spec/http_content_type/checker_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage: http://rubygems.org/gems/http_content_type
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.0.7
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Check the Content-Type of any HTTP-accessible asset.
81
+ test_files:
82
+ - spec/http_content_type/checker_spec.rb
83
+ - spec/spec_helper.rb