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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb72238c752bdafc3a8e986139d4e68fc1ea326e
4
- data.tar.gz: 0781cbbdac948bf2a65c96827bb025780b5106f7
3
+ metadata.gz: 8a19b7df93211dc0bb4fdd43bab6748c6a7c2ad5
4
+ data.tar.gz: cf8df8c962fd7f4b3a6d7ea56d3e83c39661381f
5
5
  SHA512:
6
- metadata.gz: 5b845d9adbc9117bab76f65c094a8d385253073db5a1258497ef099274236a3428a936a98a345bddfa19be284dfdec444f1f9b57611d5fd95bc33f18089a88b1
7
- data.tar.gz: 5464c41caf644a3ffa7d19ccdc47a78f8e9ae64442168b3c2a61778fb329cced37f5c7d74d17ea2051c0fefe357f0ba696940d3a550cd3fd9e2f6db527564192
6
+ metadata.gz: 804caf8124f65d100e1dad6d02efac8e88b0d1dd439d2a2cbb1328974411f87491b316ecf050068034525222cc994012919d9bb260a6a3009d799594b9353c08
7
+ data.tar.gz: e012d8066f26706ebe4640bb7a0cc7b1b62a5b9dd4d54014a75d12e3c7cdaeb585a4a25dd3b0950e609a2981a15a27339b32db311bd9d2cd42a70764875cef05
@@ -1,5 +1,6 @@
1
1
  | Q | A
2
2
  | ------------- | ---
3
+ | Branch? | "master"
3
4
  | Bug fix? | yes/no
4
5
  | New feature? | yes/no
5
6
  | BC breaks? | yes/no
data/.gitignore CHANGED
@@ -1,50 +1,19 @@
1
- *.gem
2
- *.rbc
3
- /.config
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
4
5
  /coverage/
5
- /InstalledFiles
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
- # Used by dotenv library to load environment variables.
14
- # .env
15
-
16
- ## Specific to RubyMotion:
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
@@ -1,5 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2
4
- - 2.1
5
- - 2.0
3
+ - 2.2.4
4
+ - 2.3.0
5
+ - ruby-head
6
+
data/CONTRIBUTING.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Contributing
2
2
  ===
3
3
 
4
- 1. Fork it ( https://github.com/announce/tweet_url/fork )
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
@@ -2,6 +2,7 @@ RestUri
2
2
  ===
3
3
 
4
4
  [![Build Status](https://travis-ci.org/announce/rest_uri.svg?branch=master)](https://travis-ci.org/announce/rest_uri)
5
+ [![Gem Version](https://badge.fury.io/rb/rest_uri.svg)](https://rubygems.org/gems/rest_uri/)
5
6
 
6
7
  ## Installation
7
8
 
@@ -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::resource_id('https://twitter.com/sferik/resource/540897316908331009/')
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 = DEFAULT_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
- Resource.resource_id(name, parser.parse(object), parser)
19
+ resource_id(name, parser.parse(object), parser: parser)
20
20
  when parser
21
- Resource.resource_id(name, Uri.hashbang_path_or_path(object).split('/'))
21
+ resource_id(name, Uri.hashbang_path_or_path(object).split('/'))
22
22
  when Enumerable
23
- Uri.find_resource_id(object) {|s| /\A(?:#{name.singularize}|#{name.pluralize})\z/ === 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 resource_id(name)
30
- Resource.resource_id(name.underscore, @uri, @parser)
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? :_id.to_s
35
- resource_id(name[0...-:_id.to_s.size])
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
@@ -1,3 +1,3 @@
1
1
  module RestUri
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
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, opts = {})
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 = opts.has_key?(:parser) ? opts[:parser] : DEFAULT_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 opts [Object] :parser URI parser such as Addressable::URI
28
- def self.parse(uri, opts = {})
29
- Base.new(uri, opts)
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 URL string and plural name' do
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 URL object and extract ID' do
23
- subject { resource.resource_id('statuses', "https://twitter.com/sferik/status/#{status_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
@@ -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
- describe RestUri::Base do
13
- context 'Singular resource' do
14
- let(:tweet_url) { RestUri.parse("https://twitter.com/#{username}/status/#{status_id}") }
15
- it { expect(tweet_url).to be_instance_of(RestUri::Base)}
16
- it { expect(tweet_url.status_id).to eq status_id }
17
- it { expect(tweet_url.statuses_id).to eq status_id }
18
- end
19
-
20
- context 'Plural resource' do
21
- let(:tweet_url) { RestUri.parse("https://twitter.com/#{username}/status/#{status_id}") }
22
- it { expect(tweet_url).to be_instance_of(RestUri::Base)}
23
- it { expect(tweet_url.status_id).to eq status_id }
24
- it { expect(tweet_url.statuses_id).to eq status_id }
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
- let(:tweet_url) {
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://twitter.com/#{username}/status/#{status_id}#マルチバイト",
34
- parser: Addressable::URI)
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
- it { expect(tweet_url).to be_instance_of(RestUri::Base)}
38
- it { expect(tweet_url.parser).to be Addressable::URI}
39
- it { expect(tweet_url.status_id).to eq status_id }
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.1
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.