rest_uri 0.0.1 → 1.0.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.
- checksums.yaml +4 -4
- data/.github/PULL_REQUEST_TEMPLATE.md +1 -0
- data/.gitignore +14 -45
- data/.travis.yml +4 -3
- data/CONTRIBUTING.md +1 -1
- data/README.md +1 -0
- data/lib/rest_uri/resource.rb +13 -11
- data/lib/rest_uri/version.rb +1 -1
- data/lib/rest_uri.rb +19 -13
- data/spec/rest_uri/resource_spec.rb +23 -3
- data/spec/rest_uri_spec.rb +39 -21
- metadata +1 -3
- data/Gemfile.lock +0 -49
- data/LICENSE +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a19b7df93211dc0bb4fdd43bab6748c6a7c2ad5
|
4
|
+
data.tar.gz: cf8df8c962fd7f4b3a6d7ea56d3e83c39661381f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 804caf8124f65d100e1dad6d02efac8e88b0d1dd439d2a2cbb1328974411f87491b316ecf050068034525222cc994012919d9bb260a6a3009d799594b9353c08
|
7
|
+
data.tar.gz: e012d8066f26706ebe4640bb7a0cc7b1b62a5b9dd4d54014a75d12e3c7cdaeb585a4a25dd3b0950e609a2981a15a27339b32db311bd9d2cd42a70764875cef05
|
data/.gitignore
CHANGED
@@ -1,50 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
4
5
|
/coverage/
|
5
|
-
/
|
6
|
+
/doc/
|
6
7
|
/pkg/
|
7
8
|
/spec/reports/
|
8
|
-
/spec/examples.txt
|
9
|
-
/test/tmp/
|
10
|
-
/test/version_tmp/
|
11
9
|
/tmp/
|
10
|
+
*.bundle
|
11
|
+
*.so
|
12
|
+
*.o
|
13
|
+
*.a
|
14
|
+
mkmf.log
|
12
15
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
.dat*
|
18
|
-
.repl_history
|
19
|
-
build/
|
20
|
-
*.bridgesupport
|
21
|
-
build-iPhoneOS/
|
22
|
-
build-iPhoneSimulator/
|
23
|
-
|
24
|
-
## Specific to RubyMotion (use of CocoaPods):
|
25
|
-
#
|
26
|
-
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
-
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
-
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
-
#
|
30
|
-
# vendor/Pods/
|
31
|
-
|
32
|
-
## Documentation cache and generated files:
|
33
|
-
/.yardoc/
|
34
|
-
/_yardoc/
|
35
|
-
/doc/
|
36
|
-
/rdoc/
|
37
|
-
|
38
|
-
## Environment normalization:
|
39
|
-
/.bundle/
|
40
|
-
/vendor/bundle
|
41
|
-
/lib/bundler/man/
|
42
|
-
|
43
|
-
# for a library or gem, you might want to ignore these files since the code is
|
44
|
-
# intended to run in multiple environments; otherwise, check them in:
|
45
|
-
# Gemfile.lock
|
46
|
-
# .ruby-version
|
47
|
-
# .ruby-gemset
|
48
|
-
|
49
|
-
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
-
.rvmrc
|
16
|
+
!/.github/
|
17
|
+
!/.editorconfig
|
18
|
+
!/.rspec
|
19
|
+
!/.travis.yml
|
data/.travis.yml
CHANGED
data/CONTRIBUTING.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Contributing
|
2
2
|
===
|
3
3
|
|
4
|
-
1. Fork it ( https://github.com/announce/
|
4
|
+
1. Fork it ( https://github.com/announce/rest_uri/fork )
|
5
5
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
6
6
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
7
7
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/README.md
CHANGED
data/lib/rest_uri/resource.rb
CHANGED
@@ -1,38 +1,40 @@
|
|
1
|
-
require 'active_support/inflector'
|
2
1
|
require 'rest_uri/uri'
|
3
2
|
|
4
3
|
module RestUri
|
5
4
|
module Resource
|
6
|
-
|
7
5
|
# Take a URI string or URI object and return its ID
|
8
6
|
# @example
|
9
|
-
# resource_id = RestUri::Resource
|
7
|
+
# resource_id = RestUri::Resource.resource_id('https://twitter.com/sferik/resource/540897316908331009/')
|
10
8
|
# resource_id #=> 540897316908331009
|
11
9
|
# @param name [String] Resource name
|
12
10
|
# @param object [Integer, String, URI] An ID, URI, or object.
|
11
|
+
# @option parser [Integer, String, URI] An ID, URI, or object.
|
13
12
|
# @return [Integer, NilClass]
|
14
|
-
def self.resource_id(name, object, parser
|
13
|
+
def self.resource_id(name, object, parser: DEFAULT_PARSER)
|
14
|
+
return nil if not name.respond_to? :empty? or name.empty?
|
15
15
|
case object
|
16
16
|
when ::Integer
|
17
17
|
object
|
18
18
|
when ::String
|
19
|
-
|
19
|
+
resource_id(name, parser.parse(object), parser: parser)
|
20
20
|
when parser
|
21
|
-
|
21
|
+
resource_id(name, Uri.hashbang_path_or_path(object).split('/'))
|
22
22
|
when Enumerable
|
23
|
-
Uri.find_resource_id(object) {|s|
|
23
|
+
Uri.find_resource_id(object) {|s|
|
24
|
+
/\A(?:#{name.underscore.singularize}|#{name.underscore.pluralize})\z/ === s
|
25
|
+
}
|
24
26
|
else
|
25
27
|
nil
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
29
|
-
def
|
30
|
-
Resource.resource_id(name
|
31
|
+
def resource(name)
|
32
|
+
Resource.resource_id(name, @uri, parser: @parser)
|
31
33
|
end
|
32
34
|
|
33
35
|
def method_missing(name, *args)
|
34
|
-
super unless name.to_s.end_with?
|
35
|
-
|
36
|
+
super unless name.to_s.end_with? @identifier.to_s
|
37
|
+
resource(name[0...-@identifier.to_s.size])
|
36
38
|
end
|
37
39
|
|
38
40
|
end
|
data/lib/rest_uri/version.rb
CHANGED
data/lib/rest_uri.rb
CHANGED
@@ -1,32 +1,38 @@
|
|
1
1
|
require 'uri'
|
2
|
+
require 'active_support/inflector'
|
2
3
|
require 'rest_uri/version'
|
3
4
|
require 'rest_uri/resource'
|
4
5
|
|
5
6
|
module RestUri
|
6
7
|
DEFAULT_PARSER = URI
|
8
|
+
DEFAULT_IDENTIFIER = :_id
|
7
9
|
|
8
10
|
# Retrieve resource ID from REST URI
|
9
|
-
# @example
|
10
|
-
# require 'addressable/uri'
|
11
|
-
# require 'rest_uri'
|
12
|
-
# rest_uri = RestUri.parse(
|
13
|
-
# 'https://twitter.com/yukihiro_matz/status/755950562227605504#尾骶骨',
|
14
|
-
# parser: Addressable::URI)
|
15
|
-
# rest_uri.status_id #=> 755950562227605504
|
16
11
|
class Base
|
17
12
|
include Resource
|
18
|
-
attr_accessor :uri, :parser
|
19
|
-
def initialize(uri,
|
13
|
+
attr_accessor :uri, :parser, :identifier
|
14
|
+
def initialize(uri, parser: DEFAULT_PARSER, identifier: DEFAULT_IDENTIFIER)
|
15
|
+
raise ArgumentError, 'Empty identifier' if not identifier.respond_to? :empty? or identifier.empty?
|
20
16
|
@uri = uri
|
21
|
-
@parser =
|
17
|
+
@parser = parser
|
18
|
+
@identifier = identifier
|
22
19
|
end
|
23
20
|
end
|
24
21
|
|
22
|
+
# Parse REST URI
|
25
23
|
# @param uri [String, URI]
|
26
24
|
# @param
|
27
|
-
# @option
|
28
|
-
|
29
|
-
|
25
|
+
# @option options [Object] :parser URI parser such as Addressable::URI
|
26
|
+
# @option options [Symbol] :identifier Suffix of method name to specify resource
|
27
|
+
# @example
|
28
|
+
# require 'addressable/uri'
|
29
|
+
# require 'rest_uri'
|
30
|
+
# rest_uri = RestUri.parse(
|
31
|
+
# 'https://twitter.com/yukihiro_matz/status/755950562227605504#尾骶骨',
|
32
|
+
# parser: Addressable::URI)
|
33
|
+
# rest_uri.status_id #=> 755950562227605504
|
34
|
+
def self.parse(uri, **options)
|
35
|
+
Base.new(uri, options)
|
30
36
|
end
|
31
37
|
|
32
38
|
end
|
@@ -14,16 +14,36 @@ describe RestUri::Resource do
|
|
14
14
|
it { is_expected.to equal status_id }
|
15
15
|
end
|
16
16
|
|
17
|
-
describe 'takes
|
17
|
+
describe 'takes plural name and return resource ID' do
|
18
18
|
subject { resource.resource_id('statuses', "https://twitter.com/sferik/status/#{status_id}/") }
|
19
19
|
it { is_expected.to equal status_id }
|
20
20
|
end
|
21
21
|
|
22
|
-
describe 'takes
|
23
|
-
subject { resource.resource_id('
|
22
|
+
describe 'takes capital case name and return resource ID' do
|
23
|
+
subject { resource.resource_id('Status', "https://twitter.com/sferik/status/#{status_id}/") }
|
24
|
+
it { is_expected.to equal status_id }
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'takes camel case name and return resource ID' do
|
28
|
+
subject { resource.resource_id('UserStatus', "https://twitter.com/sferik/user_status/#{status_id}/") }
|
29
|
+
it { is_expected.to equal status_id }
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'takes underscored name and return resource ID' do
|
33
|
+
subject { resource.resource_id('user_status', "https://twitter.com/sferik/user_status/#{status_id}/") }
|
24
34
|
it { is_expected.to equal status_id }
|
25
35
|
end
|
26
36
|
|
37
|
+
describe 'takes unknown resource name' do
|
38
|
+
subject { resource.resource_id('foo', "https://twitter.com/sferik/status/#{status_id}/") }
|
39
|
+
it { is_expected.to be_nil }
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'takes empty resource name' do
|
43
|
+
subject { resource.resource_id('', "https://twitter.com/sferik/status/#{status_id}/") }
|
44
|
+
it { is_expected.to be_nil }
|
45
|
+
end
|
46
|
+
|
27
47
|
end
|
28
48
|
|
29
49
|
end
|
data/spec/rest_uri_spec.rb
CHANGED
@@ -8,35 +8,53 @@ describe RestUri do
|
|
8
8
|
|
9
9
|
let!(:username) { 'i05' }
|
10
10
|
let!(:status_id) { 658113992410292224 }
|
11
|
+
let!(:photo_id) { 1 }
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
13
|
+
context 'with singular resource' do
|
14
|
+
subject(:rest_uri) { RestUri.parse("https://twitter.com/#{username}/status/#{status_id}") }
|
15
|
+
it { expect(rest_uri).to be_instance_of(RestUri::Base)}
|
16
|
+
it { expect(rest_uri.status_id).to eq status_id }
|
17
|
+
it { expect(rest_uri.statuses_id).to eq status_id }
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'with plural resource' do
|
21
|
+
subject(:rest_uri) { RestUri.parse("https://twitter.com/#{username}/statuses/#{status_id}") }
|
22
|
+
it { expect(rest_uri).to be_instance_of(RestUri::Base)}
|
23
|
+
it { expect(rest_uri.status_id).to eq status_id }
|
24
|
+
it { expect(rest_uri.statuses_id).to eq status_id }
|
25
|
+
end
|
26
26
|
|
27
|
+
context 'with multiple resources' do
|
28
|
+
subject(:rest_uri) { RestUri.parse("https://twitter.com/#{username}/status/#{status_id}/photo/#{photo_id}") }
|
29
|
+
it { expect(rest_uri).to be_instance_of(RestUri::Base)}
|
30
|
+
it { expect(rest_uri.status_id).to eq status_id }
|
31
|
+
it { expect(rest_uri.photo_id).to eq photo_id }
|
27
32
|
end
|
28
33
|
|
29
34
|
context 'with parser option' do
|
30
|
-
|
35
|
+
subject(:rest_uri) {
|
31
36
|
require 'addressable/uri'
|
37
|
+
RestUri.parse("https://漢字.com/#{username}/status/#{status_id}", parser: Addressable::URI)
|
38
|
+
}
|
39
|
+
it { expect(rest_uri).to be_instance_of(RestUri::Base)}
|
40
|
+
it { expect(rest_uri.parser).to be Addressable::URI}
|
41
|
+
it { expect(rest_uri.status_id).to eq status_id }
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'with identifier option' do
|
45
|
+
subject(:rest_uri) {
|
32
46
|
RestUri.parse(
|
33
|
-
"https://
|
34
|
-
|
47
|
+
"https://example.com/#{username}/status/#{status_id}",
|
48
|
+
identifier: :_foo)
|
35
49
|
}
|
50
|
+
it { expect(rest_uri).to be_instance_of(RestUri::Base)}
|
51
|
+
it { expect(rest_uri.status_foo).to eq status_id }
|
36
52
|
|
37
|
-
|
38
|
-
|
39
|
-
|
53
|
+
subject(:empty_identifier) {
|
54
|
+
RestUri.parse(
|
55
|
+
"https://example.com/#{username}/status/#{status_id}",
|
56
|
+
identifier: :'')
|
57
|
+
}
|
58
|
+
it { expect {empty_identifier}.to raise_error(ArgumentError)}
|
40
59
|
end
|
41
|
-
|
42
60
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_uri
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenta Yamamoto
|
@@ -94,8 +94,6 @@ files:
|
|
94
94
|
- ".travis.yml"
|
95
95
|
- CONTRIBUTING.md
|
96
96
|
- Gemfile
|
97
|
-
- Gemfile.lock
|
98
|
-
- LICENSE
|
99
97
|
- LICENSE.txt
|
100
98
|
- README.md
|
101
99
|
- Rakefile
|
data/Gemfile.lock
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
rest_uri (0.0.1)
|
5
|
-
activesupport (~> 5.0)
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
activesupport (5.0.0.1)
|
11
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
-
i18n (~> 0.7)
|
13
|
-
minitest (~> 5.1)
|
14
|
-
tzinfo (~> 1.1)
|
15
|
-
addressable (2.4.0)
|
16
|
-
concurrent-ruby (1.0.2)
|
17
|
-
diff-lcs (1.2.5)
|
18
|
-
i18n (0.7.0)
|
19
|
-
minitest (5.9.0)
|
20
|
-
rake (10.5.0)
|
21
|
-
rspec (3.5.0)
|
22
|
-
rspec-core (~> 3.5.0)
|
23
|
-
rspec-expectations (~> 3.5.0)
|
24
|
-
rspec-mocks (~> 3.5.0)
|
25
|
-
rspec-core (3.5.2)
|
26
|
-
rspec-support (~> 3.5.0)
|
27
|
-
rspec-expectations (3.5.0)
|
28
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
-
rspec-support (~> 3.5.0)
|
30
|
-
rspec-mocks (3.5.0)
|
31
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
-
rspec-support (~> 3.5.0)
|
33
|
-
rspec-support (3.5.0)
|
34
|
-
thread_safe (0.3.5)
|
35
|
-
tzinfo (1.2.2)
|
36
|
-
thread_safe (~> 0.1)
|
37
|
-
|
38
|
-
PLATFORMS
|
39
|
-
ruby
|
40
|
-
|
41
|
-
DEPENDENCIES
|
42
|
-
addressable
|
43
|
-
bundler (~> 1.7)
|
44
|
-
rake (~> 10.0)
|
45
|
-
rest_uri!
|
46
|
-
rspec
|
47
|
-
|
48
|
-
BUNDLED WITH
|
49
|
-
1.12.5
|
data/LICENSE
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2016
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
13
|
-
copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
-
SOFTWARE.
|