chupa-text 1.0.2 → 1.0.3
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/doc/text/news.md +8 -0
- data/lib/chupa-text.rb +1 -0
- data/lib/chupa-text/attributes.rb +126 -0
- data/lib/chupa-text/data.rb +3 -2
- data/lib/chupa-text/error.rb +18 -1
- data/lib/chupa-text/version.rb +2 -2
- data/test/test-attributes.rb +104 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c546de1729dc7d16dc0cdb177e34831f2d8ad75
|
4
|
+
data.tar.gz: 1e09aa57e331286ba22396611cb00d95e33361a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f8f26b9d4616dcceec080628191aa5842881766f080ab91f939a5ab9aa3c8308f402d4a1df9059914ee421db635557dd925db5ea4b25a6b4319dc27983db4a7
|
7
|
+
data.tar.gz: 2e6052a5172b2c974311441099afdf2612641668ce003be79335081bf39caa54033de2b951d22d1bdefd3b7dcb1500db34421b60431856bea289fde039b2234c
|
data/doc/text/news.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# News
|
2
2
|
|
3
|
+
## 1.0.3: 2014-02-17
|
4
|
+
|
5
|
+
* Added `ChupaText::EncryptedError`.
|
6
|
+
* Added `ChupaText::InvalidDataError`.
|
7
|
+
* Added `ChupaText::Attributes`.
|
8
|
+
* `ChupaText::Data#attributes` returns `ChupaText::Attributes` instead
|
9
|
+
of `Hash`.
|
10
|
+
|
3
11
|
## 1.0.2: 2014-02-15
|
4
12
|
|
5
13
|
* Added `ChupaText::SizeParser`.
|
data/lib/chupa-text.rb
CHANGED
@@ -0,0 +1,126 @@
|
|
1
|
+
# Copyright (C) 2014 Kouhei Sutou <kou@clear-code.com>
|
2
|
+
#
|
3
|
+
# This library is free software; you can redistribute it and/or
|
4
|
+
# modify it under the terms of the GNU Lesser General Public
|
5
|
+
# License as published by the Free Software Foundation; either
|
6
|
+
# version 2.1 of the License, or (at your option) any later version.
|
7
|
+
#
|
8
|
+
# This library is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
11
|
+
# Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public
|
14
|
+
# License along with this library; if not, write to the Free Software
|
15
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
16
|
+
|
17
|
+
module ChupaText
|
18
|
+
# Attributes of data.
|
19
|
+
class Attributes < Struct.new(:title,
|
20
|
+
:author,
|
21
|
+
:encoding,
|
22
|
+
:created_time,
|
23
|
+
:modified_time)
|
24
|
+
include Enumerable
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
super
|
28
|
+
@members = members
|
29
|
+
@extra_data = {}
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_h
|
33
|
+
super.merge(@extra_data)
|
34
|
+
end
|
35
|
+
|
36
|
+
def inspect
|
37
|
+
super.gsub(/>\z/) do
|
38
|
+
" @extra_data=#{@extra_data.inspect}>"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# @yield [name, value] Gives attribute name and value to the block.
|
43
|
+
# @yieldparam [Symbol] name The attribute name.
|
44
|
+
# @yieldparam [Object] value The attribute value.
|
45
|
+
def each(&block)
|
46
|
+
if block.nil?
|
47
|
+
Enumerator.new(self, :each)
|
48
|
+
else
|
49
|
+
each_pair(&block)
|
50
|
+
@extra_data.each_pair(&block)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Gets the value of attribute named `name`.
|
55
|
+
#
|
56
|
+
# @param [Symbol, String] name The attribute name.
|
57
|
+
# @return [Object] The attribute value.
|
58
|
+
def [](name)
|
59
|
+
name = normalize_name(name)
|
60
|
+
if @members.key?(name)
|
61
|
+
super
|
62
|
+
else
|
63
|
+
@extra_data[name]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Sets `value` as the value of attribute named `name`.
|
68
|
+
#
|
69
|
+
# @param [Symbol, String] name The attribute name.
|
70
|
+
# @param [Object] value The attribute value.
|
71
|
+
def []=(name, value)
|
72
|
+
name = normalize_name(name)
|
73
|
+
if @members.key?(name)
|
74
|
+
send("#{name}=", value)
|
75
|
+
else
|
76
|
+
@extra_data[name] = value
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Sets `encoding` as the `encoding` attribute value.
|
81
|
+
#
|
82
|
+
# @param [String, Encoding, nil] encoding The encoding.
|
83
|
+
def encoding=(encoding)
|
84
|
+
super(normalize_encoding(encoding))
|
85
|
+
end
|
86
|
+
|
87
|
+
# Sets `time` as the `created_time` attribute value.
|
88
|
+
#
|
89
|
+
# @param [String, Integer, Time, nil] time The created time.
|
90
|
+
# If `time` is `Integer`, it is used as UNIX time.
|
91
|
+
def created_time=(time)
|
92
|
+
super(normalize_time(time))
|
93
|
+
end
|
94
|
+
|
95
|
+
# Sets `time` as the `modified_time` attribute value.
|
96
|
+
#
|
97
|
+
# @param [String, Integer, Time, nil] time The modified time.
|
98
|
+
# If `time` is `Integer`, it is used as UNIX time.
|
99
|
+
def modified_time=(time)
|
100
|
+
super(normalize_time(time))
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
def normalize_name(name)
|
105
|
+
name.to_sym
|
106
|
+
end
|
107
|
+
|
108
|
+
def normalize_encoding(encoding)
|
109
|
+
if encoding.is_a?(String)
|
110
|
+
encoding = Encoding.find(encoding)
|
111
|
+
end
|
112
|
+
encoding
|
113
|
+
end
|
114
|
+
|
115
|
+
def normalize_time(time)
|
116
|
+
case time
|
117
|
+
when String
|
118
|
+
Time.parse(time)
|
119
|
+
when Integer
|
120
|
+
Time.at(time).utc
|
121
|
+
else
|
122
|
+
time
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
data/lib/chupa-text/data.rb
CHANGED
@@ -43,7 +43,8 @@ module ChupaText
|
|
43
43
|
# text and meta-data.
|
44
44
|
attr_accessor :path
|
45
45
|
|
46
|
-
|
46
|
+
# @return [Attributes] The attributes of the data.
|
47
|
+
attr_reader :attributes
|
47
48
|
|
48
49
|
# @return [Data, nil] The source of the data. For example, text
|
49
50
|
# data (`hello.txt`) in archive data (`hello.tar`) have the
|
@@ -56,7 +57,7 @@ module ChupaText
|
|
56
57
|
@size = nil
|
57
58
|
@path = nil
|
58
59
|
@mime_type = nil
|
59
|
-
@attributes =
|
60
|
+
@attributes = Attributes.new
|
60
61
|
@source = nil
|
61
62
|
end
|
62
63
|
|
data/lib/chupa-text/error.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (C) 2013 Kouhei Sutou <kou@clear-code.com>
|
1
|
+
# Copyright (C) 2013-2014 Kouhei Sutou <kou@clear-code.com>
|
2
2
|
#
|
3
3
|
# This library is free software; you can redistribute it and/or
|
4
4
|
# modify it under the terms of the GNU Lesser General Public
|
@@ -17,4 +17,21 @@
|
|
17
17
|
module ChupaText
|
18
18
|
class Error < StandardError
|
19
19
|
end
|
20
|
+
|
21
|
+
class EncryptedError < Error
|
22
|
+
attr_reader :data
|
23
|
+
def initialize(data)
|
24
|
+
@data = data
|
25
|
+
super("Encrypted data: <#{data.path}>(#{data.mime_type})")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class InvalidDataError < Error
|
30
|
+
attr_reader :data, :detail
|
31
|
+
def initialize(data, detail)
|
32
|
+
@data = data
|
33
|
+
@detail = detail
|
34
|
+
super("Invalid data: <#{data.path}>(#{data.mime_type}): <#{detail}>")
|
35
|
+
end
|
36
|
+
end
|
20
37
|
end
|
data/lib/chupa-text/version.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (C) 2013 Kouhei Sutou <kou@clear-code.com>
|
1
|
+
# Copyright (C) 2013-2014 Kouhei Sutou <kou@clear-code.com>
|
2
2
|
#
|
3
3
|
# This library is free software; you can redistribute it and/or
|
4
4
|
# modify it under the terms of the GNU Lesser General Public
|
@@ -15,5 +15,5 @@
|
|
15
15
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
16
16
|
|
17
17
|
module ChupaText
|
18
|
-
VERSION = "1.0.
|
18
|
+
VERSION = "1.0.3"
|
19
19
|
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# Copyright (C) 2014 Kouhei Sutou <kou@clear-code.com>
|
2
|
+
#
|
3
|
+
# This library is free software; you can redistribute it and/or
|
4
|
+
# modify it under the terms of the GNU Lesser General Public
|
5
|
+
# License as published by the Free Software Foundation; either
|
6
|
+
# version 2.1 of the License, or (at your option) any later version.
|
7
|
+
#
|
8
|
+
# This library is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
11
|
+
# Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public
|
14
|
+
# License along with this library; if not, write to the Free Software
|
15
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
16
|
+
|
17
|
+
class TestAttributes < Test::Unit::TestCase
|
18
|
+
def setup
|
19
|
+
@attributes = ChupaText::Attributes.new
|
20
|
+
end
|
21
|
+
|
22
|
+
sub_test_case("title") do
|
23
|
+
def test_accessor
|
24
|
+
assert_nil(@attributes.title)
|
25
|
+
@attributes.title = "Title"
|
26
|
+
assert_equal("Title", @attributes.title)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
sub_test_case("author") do
|
31
|
+
def test_accessor
|
32
|
+
assert_nil(@attributes.author)
|
33
|
+
@attributes.author = "Alice"
|
34
|
+
assert_equal("Alice", @attributes.author)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
sub_test_case("encoding") do
|
39
|
+
def test_string
|
40
|
+
@attributes.encoding = "UTF-8"
|
41
|
+
assert_equal(Encoding::UTF_8, @attributes.encoding)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_encoding
|
45
|
+
@attributes.encoding = Encoding::UTF_8
|
46
|
+
assert_equal(Encoding::UTF_8, @attributes.encoding)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_nil
|
50
|
+
@attributes.encoding = nil
|
51
|
+
assert_nil(@attributes.encoding)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
sub_test_case("created_time") do
|
56
|
+
def test_string
|
57
|
+
@attributes.created_time = "2014-02-17T23:14:30+09:00"
|
58
|
+
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
|
59
|
+
@attributes.created_time)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_integer
|
63
|
+
@attributes.created_time = 1392646470
|
64
|
+
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
|
65
|
+
@attributes.created_time)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_time
|
69
|
+
@attributes.created_time = Time.parse("2014-02-17T23:14:30+09:00")
|
70
|
+
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
|
71
|
+
@attributes.created_time)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_nil
|
75
|
+
@attributes.created_time = nil
|
76
|
+
assert_nil(@attributes.created_time)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
sub_test_case("modified_time") do
|
81
|
+
def test_string
|
82
|
+
@attributes.modified_time = "2014-02-17T23:14:30+09:00"
|
83
|
+
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
|
84
|
+
@attributes.modified_time)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_integer
|
88
|
+
@attributes.modified_time = 1392646470
|
89
|
+
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
|
90
|
+
@attributes.modified_time)
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_time
|
94
|
+
@attributes.modified_time = Time.parse("2014-02-17T23:14:30+09:00")
|
95
|
+
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
|
96
|
+
@attributes.modified_time)
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_nil
|
100
|
+
@attributes.modified_time = nil
|
101
|
+
assert_nil(@attributes.modified_time)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chupa-text
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kouhei Sutou
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -127,6 +127,7 @@ files:
|
|
127
127
|
- lib/chupa-text/input-data.rb
|
128
128
|
- lib/chupa-text/mime-type-registry.rb
|
129
129
|
- lib/chupa-text/default-logger.rb
|
130
|
+
- lib/chupa-text/attributes.rb
|
130
131
|
- lib/chupa-text.rb
|
131
132
|
- doc/text/decomposer.md
|
132
133
|
- doc/text/command-line.md
|
@@ -150,6 +151,7 @@ files:
|
|
150
151
|
- test/fixture/command/chupa-text/no-decomposer.conf
|
151
152
|
- test/fixture/command/chupa-text/hello.txt
|
152
153
|
- test/fixture/extractor/hello.txt
|
154
|
+
- test/test-attributes.rb
|
153
155
|
- test/test-external-command.rb
|
154
156
|
- test/test-size-parser.rb
|
155
157
|
- test/decomposers/test-csv.rb
|