http_range 1.0.0

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: 6f193e3a32d1e236790886073848643050da5d0b
4
+ data.tar.gz: 99b3dc5188a26fb91d4bf6565cb253bf735757eb
5
+ SHA512:
6
+ metadata.gz: 06330411edb69fa887cb44cec4d9168951d0db998ad90e0db0887adb987f76d6bd45895870aa4219f4ea329b283a8e27061a4838d26af68cea7f3802ef1f5fc5
7
+ data.tar.gz: 3ad46bb2d5c68534374b87efdb6735ed57cc7de81ef1225adc6e2ae868e6f4e973bac19116f328f0e795b0ed8a8d985573b9aa12fbc7997fcd69bb4bd99504b1
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in http_range.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Brad Fults, The Last Guide Company
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,44 @@
1
+ # HTTPRange
2
+
3
+ A library to parse an HTTP `Range` header with semantics similar to
4
+ [Heroku's Range headers][1].
5
+
6
+ [1]: https://devcenter.heroku.com/articles/platform-api-reference#ranges
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'http_range'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install http_range
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ http_range = HTTPRange.parse('Range: id 29f99177-36e9-466c-baef-f855e1ab731e..29f99177-36e9-466c-baef-f855e1ab731e[; max=100')
28
+
29
+ http_range.attribute # => "id"
30
+ http_range.first # => "29f99177-36e9-466c-baef-f855e1ab731e"
31
+ http_range.last # => "29f99177-36e9-466c-baef-f855e1ab731e"
32
+ http_range.max # => "100"
33
+ http_range.order # => nil
34
+ http_range.first_inclusive # => true
35
+ http_range.last_inclusive # => false
36
+ ```
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( https://github.com/[my-github-username]/http_range/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'http_range/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'http_range'
8
+ spec.version = HTTPRange::VERSION
9
+ spec.authors = ["Brad Fults"]
10
+ spec.email = ["bfults@gmail.com"]
11
+ spec.summary = %q{Assists in parsing HTTP Range headers.}
12
+ spec.description = %q{}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.1'
24
+ end
@@ -0,0 +1,95 @@
1
+ class HTTPRange
2
+ class Parser
3
+ # Accepts the HTTP Range header string to parse.
4
+ #
5
+ def initialize(header_string)
6
+ @header_string = header_string.dup
7
+ end
8
+
9
+ # Runs the parser on its given `header_string` and returns a hash of the
10
+ # extracted parts.
11
+ #
12
+ # @return [Hash]
13
+ #
14
+ def extract_parts_hash
15
+ parts_hash = {}
16
+
17
+ @header_string.sub!(/\ARange:\s*/, '')
18
+
19
+ range_spec_string, params_strings = split_header_string(@header_string)
20
+
21
+ parts_hash.merge! extract_range_spec_hash(range_spec_string)
22
+ parts_hash.merge! extract_params_hash(params_strings)
23
+
24
+ return parts_hash
25
+ end
26
+
27
+ private
28
+
29
+ def blank?(value)
30
+ value.nil? || value == '' || value == false
31
+ end
32
+
33
+ # Takes raw params strings and returns a hash of key/value pairs.
34
+ #
35
+ # @param params_strings [Array<String>]
36
+ #
37
+ # @return [Hash<String,String>]
38
+ #
39
+ def extract_params_hash(params_strings)
40
+ hash = Hash[params_strings.map { |p| p.split('=').map { |v| v.strip } }]
41
+ hash.detect do |k, v|
42
+ raise MalformedRangeHeaderError, "Params key malformed." if blank?(k)
43
+ raise MalformedRangeHeaderError, "Params value missing: #{k}" if blank?(v)
44
+ raise MalformedRangeHeaderError, "Invalid order value: #{v}" if k == 'order' && !%w[asc desc].include?(v)
45
+ end
46
+ hash
47
+ end
48
+
49
+ RANGE_SPEC_REGEX = /\A
50
+ (?<attribute>[a-zA-Z][\w\.-]*)
51
+ \s+
52
+ (?<first_exclusive>\])?
53
+ (?<first>[^\.]*?)
54
+ \.\.
55
+ (?<last>[^\.]*?)
56
+ (?<last_exclusive>\[)?
57
+ \z/x
58
+
59
+ # Takes a raw range spec string (<attr> <first>..<last>) and returns a hash
60
+ # of key/value pairs for the extracted parts.
61
+ #
62
+ # @param range_spec_string [String]
63
+ #
64
+ # @return [Hash<String,String>]
65
+ #
66
+ def extract_range_spec_hash(range_spec_string)
67
+ hash = {}
68
+ values = range_spec_string.match(RANGE_SPEC_REGEX)
69
+ if values
70
+ hash.merge!({
71
+ 'attribute' => values['attribute'],
72
+ 'first' => values['first'],
73
+ 'last' => values['last'],
74
+ 'first_inclusive' => values['first_exclusive'].nil?,
75
+ 'last_inclusive' => values['last_exclusive'].nil?,
76
+ })
77
+ else
78
+ raise MalformedRangeHeaderError, "Invalid range spec."
79
+ end
80
+ hash
81
+ end
82
+
83
+ # Takes the value portion of a Range header string and splits it into the
84
+ # range spec and any params delimited by ;. Also strips whitespace.
85
+ #
86
+ # @param header_string [String]
87
+ #
88
+ # @return [Array<String,Array<String>>]
89
+ #
90
+ def split_header_string(header_string)
91
+ range_spec, *params = header_string.split(';')
92
+ [range_spec.strip, params.map { |s| s.strip }]
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ class HTTPRange
2
+ VERSION = "1.0.0"
3
+ end
data/lib/http_range.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'http_range/version'
2
+
3
+ class HTTPRange
4
+
5
+ class MalformedRangeHeaderError < StandardError; end
6
+
7
+ attr_reader :attribute
8
+ attr_reader :first
9
+ attr_reader :last
10
+ attr_reader :first_inclusive
11
+ attr_reader :last_inclusive
12
+ attr_reader :order
13
+ attr_reader :max
14
+
15
+ def initialize(fields)
16
+ fields.each { |field, value| instance_variable_set(:"@#{field}", value) }
17
+ end
18
+
19
+ def self.parse(header_string)
20
+ new(Parser.new(header_string).extract_parts_hash)
21
+ end
22
+ end
23
+
24
+ require 'http_range/parser'
@@ -0,0 +1,66 @@
1
+ require 'http_range'
2
+ require 'spec_helper'
3
+
4
+ RSpec.describe HTTPRange do
5
+ describe '.parse' do
6
+ subject { described_class.parse(header) }
7
+
8
+ context "for a correctly formatted Range header" do
9
+ let(:header) { "Range: id 29f99177-36e9-466c-baef-f855e1ab731e..6c714be1-d901-4c40-adb3-22c9a8cda950[; max=100" }
10
+
11
+ it "returns an instance of the class" do
12
+ expect(subject).to be_an(HTTPRange)
13
+ end
14
+
15
+ it { expect(subject.attribute).to eq('id') }
16
+ it { expect(subject.first).to eq('29f99177-36e9-466c-baef-f855e1ab731e') }
17
+ it { expect(subject.last).to eq('6c714be1-d901-4c40-adb3-22c9a8cda950') }
18
+ it { expect(subject.first_inclusive).to eq(true) }
19
+ it { expect(subject.last_inclusive).to eq(false) }
20
+ it { expect(subject.max).to eq('100') }
21
+ it { expect(subject.order).to be_nil }
22
+ end
23
+
24
+ context "for a Range header with a wacky but valid attribute name" do
25
+ let(:header) { "Range: purple.cycle-cleaner_knob ]1e3f..2e5a; order=desc" }
26
+
27
+ it { expect(subject.attribute).to eq('purple.cycle-cleaner_knob') }
28
+ it { expect(subject.first).to eq('1e3f') }
29
+ it { expect(subject.last).to eq('2e5a') }
30
+ it { expect(subject.first_inclusive).to eq(false) }
31
+ it { expect(subject.last_inclusive).to eq(true) }
32
+ it { expect(subject.max).to be_nil }
33
+ it { expect(subject.order).to eq('desc') }
34
+ end
35
+
36
+ context "for a Range header with an invalid order param" do
37
+ let(:header) { "Range: purple.cycle-cleaner_knob ]1e3f..2e5a; order=true" }
38
+
39
+ it { expect { subject }.to raise_error(HTTPRange::MalformedRangeHeaderError) }
40
+ end
41
+
42
+ context "for a Range header with an invalid range spec syntax" do
43
+ let(:header) { "Range: id 1e3f...2e5a" }
44
+
45
+ it { expect { subject }.to raise_error(HTTPRange::MalformedRangeHeaderError) }
46
+ end
47
+
48
+ context "for a Range header with an invalid range attribute name" do
49
+ let(:header) { "Range: !!!amazing!!! 1e3f...2e5a" }
50
+
51
+ it { expect { subject }.to raise_error(HTTPRange::MalformedRangeHeaderError) }
52
+ end
53
+
54
+ context "for a Range header without an attribute name" do
55
+ let(:header) { "Range: 1..2" }
56
+
57
+ it { expect { subject }.to raise_error(HTTPRange::MalformedRangeHeaderError) }
58
+ end
59
+
60
+ context "for a Range header without a range" do
61
+ let(:header) { "Range: hi" }
62
+
63
+ it { expect { subject }.to raise_error(HTTPRange::MalformedRangeHeaderError) }
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ require 'rspec/core'
2
+
3
+ RSpec.configure do |config|
4
+ config.disable_monkey_patching!
5
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_range
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brad Fults
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-18 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ description: ''
56
+ email:
57
+ - bfults@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - http_range.gemspec
68
+ - lib/http_range.rb
69
+ - lib/http_range/parser.rb
70
+ - lib/http_range/version.rb
71
+ - spec/http_range_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.3.0
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Assists in parsing HTTP Range headers.
97
+ test_files:
98
+ - spec/http_range_spec.rb
99
+ - spec/spec_helper.rb