tweet_url 0.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.
- checksums.yaml +7 -0
- data/.editorconfig +10 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +10 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CONTRIBUTING.md +8 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +7 -0
- data/lib/tweet_url/status.rb +32 -0
- data/lib/tweet_url/uri.rb +20 -0
- data/lib/tweet_url/username.rb +25 -0
- data/lib/tweet_url/version.rb +3 -0
- data/lib/tweet_url.rb +35 -0
- data/spec/Gemfile +14 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/tweet_url/status_spec.rb +49 -0
- data/spec/tweet_url/username_spec.rb +11 -0
- data/spec/tweet_url_spec.rb +34 -0
- data/tweet_url.gemspec +23 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8d513978b99ca008a74aaac00d7aca0ef0e7babd
|
4
|
+
data.tar.gz: 0341d3a86d5a4086b5fe361f578ed47d640e7978
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 605953c0fc283f5241dadf28b418b7ec93db6f3511f6112e5e4a9a07b37591f843cd69b9c13c059252989a16b7c212809f47b4b2f0d44bc9dece40c87d9224c1
|
7
|
+
data.tar.gz: 791d5988cff45d838f03fb15a4227ade583e31aeb5dcb6c277dfcab22b2d850059ba65a11592fad461593394a351b03a0979a270e9218f39c05b45dd84dea835
|
data/.editorconfig
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
| Q | A
|
2
|
+
| ------------- | ---
|
3
|
+
| Bug fix? | yes/no
|
4
|
+
| New feature? | yes/no
|
5
|
+
| BC breaks? | yes/no
|
6
|
+
| Deprecations? | yes/no
|
7
|
+
| Tests pass? | yes/no
|
8
|
+
| Fixed tickets | comma-separated list of tickets fixed by the PR, if any
|
9
|
+
| License | MIT
|
10
|
+
| Doc PR | reference to the documentation PR, if any
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Contributing
|
2
|
+
===
|
3
|
+
|
4
|
+
1. Fork it ( https://github.com/announce/tweet_url/fork )
|
5
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
6
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
7
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
8
|
+
5. Create a new Pull Request
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Kenta Yamamoto
|
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,45 @@
|
|
1
|
+
TweetUrl
|
2
|
+
===
|
3
|
+
|
4
|
+
[](https://travis-ci.org/announce/tweet_url)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'tweet_url'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install tweet_url
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Simply,
|
25
|
+
|
26
|
+
```rb
|
27
|
+
require 'tweet_url'
|
28
|
+
|
29
|
+
tweet_url = TweetUrl.parse('https://twitter.com/yukihiro_matz/status/755950562227605504')
|
30
|
+
tweet_url.username #=> "yukihiro_matz"
|
31
|
+
tweet_url.status_id #=> 755950562227605504
|
32
|
+
```
|
33
|
+
|
34
|
+
To handle URL containing multibyte chars, run `gem install addressable` and
|
35
|
+
|
36
|
+
```rb
|
37
|
+
require 'addressable/uri'
|
38
|
+
require 'tweet_url'
|
39
|
+
|
40
|
+
tweet_url = TweetUrl.parse(
|
41
|
+
'https://twitter.com/yukihiro_matz/status/755950562227605504#尾骶骨',
|
42
|
+
parser: Addressable::URI)
|
43
|
+
tweet_url.username #=> "yukihiro_matz"
|
44
|
+
tweet_url.status_id #=> 755950562227605504
|
45
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'tweet_url/uri'
|
2
|
+
|
3
|
+
module TweetUrl
|
4
|
+
module Status
|
5
|
+
|
6
|
+
# Take a URI string or URI object and return its ID
|
7
|
+
# @example
|
8
|
+
# status_id = TweetUrl::Status::status_id('https://twitter.com/sferik/status/540897316908331009/')
|
9
|
+
# status_id #=> 540897316908331009
|
10
|
+
# @param object [Integer, String, URI] An ID, URI, or object.
|
11
|
+
# @return [Integer, NilClass]
|
12
|
+
def self.status_id(object, parser = DEFAULT_PARSER)
|
13
|
+
case object
|
14
|
+
when ::Integer
|
15
|
+
object
|
16
|
+
when ::String
|
17
|
+
Status::status_id(parser.parse(object), parser)
|
18
|
+
when parser
|
19
|
+
Status::status_id(Uri.hashbang_path_or_path(object).split('/'))
|
20
|
+
when Enumerable
|
21
|
+
Uri::find_resource_id(object) {|s| /\Astatus(?:es)?\z/ === s}
|
22
|
+
else
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def status_id
|
28
|
+
Status::status_id(@url, @parser)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module TweetUrl
|
2
|
+
module Uri
|
3
|
+
|
4
|
+
def self.find_resource_id(list, &block)
|
5
|
+
index = list.index(&block)
|
6
|
+
return index if index.nil?
|
7
|
+
target = list[index + 1]
|
8
|
+
target.nil? ? target : target.to_i
|
9
|
+
end
|
10
|
+
|
11
|
+
# @param object [URI]
|
12
|
+
# @return [String]
|
13
|
+
def self.hashbang_path_or_path(object)
|
14
|
+
(not object.fragment.nil? and object.fragment.chars.first == '!') ?
|
15
|
+
object.fragment :
|
16
|
+
object.path
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'tweet_url/uri'
|
2
|
+
|
3
|
+
module TweetUrl
|
4
|
+
module Username
|
5
|
+
# Take a URI string or URI object and return its username
|
6
|
+
# @param object [String, URI] An ID, URI, or object.
|
7
|
+
# @return [String, NilClass]
|
8
|
+
def self.username(object, parser = DEFAULT_PARSER)
|
9
|
+
case object
|
10
|
+
when ::String
|
11
|
+
Username::username(parser.parse(object), parser)
|
12
|
+
when URI, parser
|
13
|
+
Username::username(Uri.hashbang_path_or_path(object).split('/'))
|
14
|
+
when Enumerable
|
15
|
+
object[1]
|
16
|
+
else
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def username
|
22
|
+
Username::username(@url, @parser)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/tweet_url.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'tweet_url/version'
|
3
|
+
require 'tweet_url/username'
|
4
|
+
require 'tweet_url/status'
|
5
|
+
|
6
|
+
# Extract username and status_id from tweet URL
|
7
|
+
# @example
|
8
|
+
# require 'addressable/uri'
|
9
|
+
# require 'tweet_url'
|
10
|
+
# tweet_url = TweetUrl.parse(
|
11
|
+
# 'https://twitter.com/yukihiro_matz/status/755950562227605504#尾骶骨',
|
12
|
+
# parser: Addressable::URI)
|
13
|
+
# tweet_url.username #=> "yukihiro_matz"
|
14
|
+
# tweet_url.status_id #=> 755950562227605504
|
15
|
+
module TweetUrl
|
16
|
+
DEFAULT_PARSER = URI
|
17
|
+
|
18
|
+
class Base
|
19
|
+
include Username
|
20
|
+
include Status
|
21
|
+
attr_accessor :url, :parser
|
22
|
+
def initialize(url, opts = {})
|
23
|
+
@url = url
|
24
|
+
@parser = opts.has_key?(:parser) ? opts[:parser] : DEFAULT_PARSER
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param url [String, URI]
|
29
|
+
# @param
|
30
|
+
# @option opts [Object] :parser URI parser such as Addressable::URI
|
31
|
+
def self.parse(url, opts = {})
|
32
|
+
Base.new(url, opts)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/spec/Gemfile
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TweetUrl::Status do
|
4
|
+
Status = TweetUrl::Status
|
5
|
+
describe '.status_id' do
|
6
|
+
let (:status_id) { 540897316908331009 }
|
7
|
+
describe 'takes string URL and extract ID' do
|
8
|
+
subject { Status::status_id("https://twitter.com/sferik/status/#{status_id}/") }
|
9
|
+
it { is_expected.to equal status_id }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'takes URL object and extract ID' do
|
13
|
+
subject { Status::status_id(URI.parse("https://twitter.com/sferik/status/#{status_id}/")) }
|
14
|
+
it { is_expected.to equal status_id }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'takes integer ID and return ID' do
|
18
|
+
subject { Status::status_id(status_id) }
|
19
|
+
it { is_expected.to equal status_id }
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'takes URL followed by additional paths, and extract ID' do
|
23
|
+
subject { Status::status_id("https://twitter.com/sferik/status/#{status_id}/photo/1/") }
|
24
|
+
it { is_expected.to equal status_id }
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'takes plural style URL, and extract ID' do
|
28
|
+
subject { Status::status_id("https://twitter.com/#!/sferik/statuses/#{status_id}/") }
|
29
|
+
it { is_expected.to equal status_id }
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'takes irrelevant URL and return nil' do
|
33
|
+
subject { Status::status_id('https://example.com/foo/bar/') }
|
34
|
+
it { is_expected.to be_nil }
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'takes string ID and return nil' do
|
38
|
+
subject { Status::status_id(status_id.to_s) }
|
39
|
+
it { is_expected.to be_nil }
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'takes no ID URL, and return nil' do
|
43
|
+
subject { Status::status_id('https://twitter.com/sferik/status/') }
|
44
|
+
it { is_expected.to be_nil }
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TweetUrl::Username do
|
4
|
+
describe '.username' do
|
5
|
+
let (:username) { 'i05' }
|
6
|
+
describe 'takes string URL and extract username' do
|
7
|
+
subject { TweetUrl::Username::username("https://twitter.com/#{username}/status/658113992410292224") }
|
8
|
+
it { is_expected.to eq username }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TweetUrl do
|
4
|
+
|
5
|
+
it 'has a version number' do
|
6
|
+
expect(TweetUrl::VERSION).not_to be nil
|
7
|
+
end
|
8
|
+
|
9
|
+
let!(:username) { 'i05' }
|
10
|
+
let!(:status_id) { 658113992410292224 }
|
11
|
+
|
12
|
+
describe TweetUrl::Base do
|
13
|
+
let(:tweet_url) { TweetUrl.parse("https://twitter.com/#{username}/status/#{status_id}") }
|
14
|
+
|
15
|
+
it { expect(tweet_url).to be_instance_of(TweetUrl::Base)}
|
16
|
+
it { expect(tweet_url.username).to eq username }
|
17
|
+
it { expect(tweet_url.status_id).to eq status_id }
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'with parser option' do
|
21
|
+
let(:tweet_url) {
|
22
|
+
require 'addressable/uri'
|
23
|
+
TweetUrl.parse(
|
24
|
+
"https://twitter.com/#{username}/status/#{status_id}#マルチバイト",
|
25
|
+
parser: Addressable::URI)
|
26
|
+
}
|
27
|
+
|
28
|
+
it { expect(tweet_url).to be_instance_of(TweetUrl::Base)}
|
29
|
+
it { expect(tweet_url.parser).to be Addressable::URI}
|
30
|
+
it { expect(tweet_url.username).to eq username }
|
31
|
+
it { expect(tweet_url.status_id).to eq status_id }
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/tweet_url.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tweet_url/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'tweet_url'
|
8
|
+
spec.version = TweetUrl::VERSION
|
9
|
+
spec.authors = ['Kenta Yamamoto']
|
10
|
+
spec.email = ['ymkjp@jaist.ac.jp']
|
11
|
+
spec.summary = 'Twitter URL parser'
|
12
|
+
spec.description = 'Extract username and status_id from tweet URL'
|
13
|
+
spec.homepage = 'https://github.com/announce/tweet_url'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
20
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
21
|
+
spec.add_development_dependency 'rspec'
|
22
|
+
spec.add_development_dependency 'addressable'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tweet_url
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kenta Yamamoto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-11 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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: addressable
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Extract username and status_id from tweet URL
|
70
|
+
email:
|
71
|
+
- ymkjp@jaist.ac.jp
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".editorconfig"
|
77
|
+
- ".github/PULL_REQUEST_TEMPLATE.md"
|
78
|
+
- ".gitignore"
|
79
|
+
- ".rspec"
|
80
|
+
- ".travis.yml"
|
81
|
+
- CONTRIBUTING.md
|
82
|
+
- Gemfile
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- lib/tweet_url.rb
|
87
|
+
- lib/tweet_url/status.rb
|
88
|
+
- lib/tweet_url/uri.rb
|
89
|
+
- lib/tweet_url/username.rb
|
90
|
+
- lib/tweet_url/version.rb
|
91
|
+
- spec/Gemfile
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/tweet_url/status_spec.rb
|
94
|
+
- spec/tweet_url/username_spec.rb
|
95
|
+
- spec/tweet_url_spec.rb
|
96
|
+
- tweet_url.gemspec
|
97
|
+
homepage: https://github.com/announce/tweet_url
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.6.6
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Twitter URL parser
|
121
|
+
test_files:
|
122
|
+
- spec/Gemfile
|
123
|
+
- spec/spec_helper.rb
|
124
|
+
- spec/tweet_url/status_spec.rb
|
125
|
+
- spec/tweet_url/username_spec.rb
|
126
|
+
- spec/tweet_url_spec.rb
|