vlaah 0.9.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.
- data/doc/template.rb +744 -0
- data/lib/vlaah/base.rb +133 -0
- data/lib/vlaah/comment.rb +156 -0
- data/lib/vlaah/comment_list.rb +148 -0
- data/lib/vlaah/locale.rb +32 -0
- data/lib/vlaah/person.rb +157 -0
- data/lib/vlaah/topic.rb +216 -0
- data/lib/vlaah/unit.rb +62 -0
- data/lib/vlaah/version.rb +4 -0
- data/lib/vlaah.rb +31 -0
- metadata +56 -0
data/lib/vlaah/topic.rb
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
require 'vlaah/base'
|
2
|
+
require 'vlaah/unit'
|
3
|
+
require 'rexml/xpath'
|
4
|
+
|
5
|
+
module VLAAH
|
6
|
+
# = Topic: The atomic object of VLAAH.
|
7
|
+
#
|
8
|
+
# It represents topics of VLAAH.
|
9
|
+
# There is no creation or deletion of topics in VLAAH, like pages in wiki.
|
10
|
+
#
|
11
|
+
# == Finding (Or Creating) Topic
|
12
|
+
#
|
13
|
+
# You can find topics with VLAAH::find method.
|
14
|
+
#
|
15
|
+
# topic = VLAAH::Topic.find "Zhuangzi"
|
16
|
+
# topic2 = VLAAH::Topic.find "Common Lisp"
|
17
|
+
#
|
18
|
+
# == Getting Names
|
19
|
+
#
|
20
|
+
# There are three types of names in VLAAH::Topic.
|
21
|
+
#
|
22
|
+
# - +name+ (called name) for respect to the context (e.g. RESTful vs restful)
|
23
|
+
# - +normalized_name+ for equality testing (e.g. iPod touch and iPod Touch)
|
24
|
+
# - +usual_name+ for printing name prettily
|
25
|
+
#
|
26
|
+
# a = VLAAH::Topic.find "RESTful"
|
27
|
+
# b = VLAAH::Topic.find "restful"
|
28
|
+
# a.name, b.name #=> ["RESTful", "restful"]
|
29
|
+
# a.normalized_name #=> "restful"
|
30
|
+
# b.normalized_name #=> "restful"
|
31
|
+
# a == b #=> true
|
32
|
+
#
|
33
|
+
# The most frequent naming becomes +usual_name+.
|
34
|
+
#
|
35
|
+
# a.usual_name, b.usual_name #=> ["RESTful", "RESTful"]
|
36
|
+
#
|
37
|
+
# == Getting Numbers
|
38
|
+
#
|
39
|
+
# The standard unit in VLAAH is the +degree+(+point+).
|
40
|
+
#
|
41
|
+
# topic.degree #=> 300
|
42
|
+
# topic.degree.plus #=> 380
|
43
|
+
# topic.degree.minus #=> 80
|
44
|
+
#
|
45
|
+
# If you want just the number of people that commented to the topic,
|
46
|
+
# call +people+ method.
|
47
|
+
#
|
48
|
+
# topic.people #=> 46
|
49
|
+
# topic.people.plus #=> 38
|
50
|
+
# topic.people.minus #=> 8
|
51
|
+
#
|
52
|
+
# There is the +percent+ method also.
|
53
|
+
#
|
54
|
+
# topic.percent #=> 82
|
55
|
+
# topic.percent.plus #=> 82
|
56
|
+
# topic.percent.minus #=> 17
|
57
|
+
#
|
58
|
+
# The above three methods(+degree+, +people+, +percent+) return an instance
|
59
|
+
# of VLAAH::Unit. It is number-like value object, so it has arithmetic
|
60
|
+
# operators, and it also have +plus+, +minus+ method. That two methods return
|
61
|
+
# just an instance of Fixnum or Float.
|
62
|
+
#
|
63
|
+
# == Getting Comments
|
64
|
+
#
|
65
|
+
# Comments of the topic are gettable with VLAAH::Topic#comments method.
|
66
|
+
#
|
67
|
+
# topic.comments #=> [<VLAAH::Comment name="?1234" ...>,
|
68
|
+
# <VLAAH::Comment name="?567" ...> ... (42 comments)]
|
69
|
+
#
|
70
|
+
# It returns an instance of VLAAH::CommentList. It implements +Enumerable+
|
71
|
+
# and is Array-like.
|
72
|
+
#
|
73
|
+
# == Getting Commenters
|
74
|
+
#
|
75
|
+
# VLAAH::Topic#commenters returns an array of people that commented to the
|
76
|
+
# topic. This array also have two more singleton methods: +plus+ and +minus+.
|
77
|
+
#
|
78
|
+
# topic.commenters #=> [<VLAAH::Person name="~dahlia" ...>,
|
79
|
+
# <VLAAH::Person name="~heungsub" ...> ...]
|
80
|
+
# topic.commenters.plus #=> [<VLAAH::Person name="~dahlia" ...> ...]
|
81
|
+
# topic.commenters.minus #=> [<VLAAH::Person name="~heungsub" ...> ...]
|
82
|
+
class Topic < Base
|
83
|
+
NamePattern = //
|
84
|
+
|
85
|
+
# Returns the topic named +name+.
|
86
|
+
# May be VLAAH::Topic or VLAAH::Person or VLAAH::Comment.
|
87
|
+
def self.find(name)
|
88
|
+
Person.find(name) or Comment.find(name) or new(name)
|
89
|
+
end
|
90
|
+
|
91
|
+
# DO NOT INITIALIZE TOPIC _DIRECTLY_. USE VLAAH::Topic#find INSTEAD.
|
92
|
+
def initialize(name_or_xml)
|
93
|
+
if name_or_xml.is_a? ::REXML::Element
|
94
|
+
@raw_data = name_or_xml
|
95
|
+
elsif name_or_xml.is_a? ::REXML::Document
|
96
|
+
@raw_data = name_or_xml.root
|
97
|
+
else
|
98
|
+
@name = name_or_xml.strip
|
99
|
+
end
|
100
|
+
|
101
|
+
@name = @raw_data.attributes['name'].strip if defined? @raw_data
|
102
|
+
end
|
103
|
+
|
104
|
+
# The name of the topic. It is not normalized.
|
105
|
+
#
|
106
|
+
# Topic.find("VLAAH").name #=> "VLAAH"
|
107
|
+
# Topic.find("vlaah").name #=> "vlaah"
|
108
|
+
def name
|
109
|
+
@name
|
110
|
+
end
|
111
|
+
|
112
|
+
# Its normalized name. It is used for testing equality of topics.
|
113
|
+
#
|
114
|
+
# Topic.find("VLAAH").normalized_name #=> "vlaah"
|
115
|
+
# Topic.find(" vlaah ").normalized_name #=> "vlaah"
|
116
|
+
# Topic.find("블 라").normalized_name #=> "블라"
|
117
|
+
def normalized_name
|
118
|
+
raw_data("@normalized-name").first.value
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns +true+ if it and +operand+ are the same.
|
122
|
+
# (Compare by +normalized_name+.)
|
123
|
+
#
|
124
|
+
# Topic.find("Smalltalk").eql? Topic.find("SmallTalk") #=> true
|
125
|
+
# Topic.find("Smalltalk") == Topic.find("SmallTalk") #=> true
|
126
|
+
# Topic.find("Scheme").eql? Topic.find("Common Lisp") #=> false
|
127
|
+
# Topic.find("Scheme") == Topic.find("Common Lisp") #=> false
|
128
|
+
def eql?(operand)
|
129
|
+
operand.normalized_name.eql? normalized_name
|
130
|
+
end
|
131
|
+
|
132
|
+
alias_method :==, :eql?
|
133
|
+
|
134
|
+
# Its name that called usually.
|
135
|
+
#
|
136
|
+
# Topic.find("vlaah").normalized_name #=> "VLAAH"
|
137
|
+
# Topic.find("SmallTalk").normalized_name #=> "Smalltalk"
|
138
|
+
# Topic.find("~Dahlia").normalized_name #=> "~dahlia"
|
139
|
+
def usual_name
|
140
|
+
raw_data("@usual-name").first.value
|
141
|
+
end
|
142
|
+
|
143
|
+
def degree
|
144
|
+
Unit.new(raw_data("//degree").first) { |x| x.to_i }
|
145
|
+
end
|
146
|
+
|
147
|
+
alias_method :point, :degree
|
148
|
+
|
149
|
+
def people
|
150
|
+
Unit.new(raw_data("//people").first) { |x| x.to_i }
|
151
|
+
end
|
152
|
+
|
153
|
+
def percent
|
154
|
+
Unit.new(raw_data("//percent").first) { |x| x.to_f / 100 }
|
155
|
+
end
|
156
|
+
|
157
|
+
# Returns a list of its comments. (An instance of VLAAH::CommentList.)
|
158
|
+
def comments
|
159
|
+
CommentList.new name, "comments"
|
160
|
+
end
|
161
|
+
|
162
|
+
# Returns an array of people that commented to the topic.
|
163
|
+
def commenters
|
164
|
+
xml = self.class.raw_data(name, "commenters")
|
165
|
+
commenters = ::REXML::XPath.match(xml, "//person") \
|
166
|
+
.map(&Person.method(:new))
|
167
|
+
commenters.instance_variable_set(:@__vlaah_commenters, xml)
|
168
|
+
|
169
|
+
def commenters.plus
|
170
|
+
::REXML::XPath.match(@__vlaah_commenters, "//plus/person") \
|
171
|
+
.map(&Person.method(:new))
|
172
|
+
end
|
173
|
+
|
174
|
+
def commenters.minus
|
175
|
+
::REXML::XPath.match(@__vlaah_commenters, "//minus/person") \
|
176
|
+
.map(&Person.method(:new))
|
177
|
+
end
|
178
|
+
|
179
|
+
return commenters
|
180
|
+
end
|
181
|
+
|
182
|
+
alias_method :to_s, :name
|
183
|
+
|
184
|
+
def inspect
|
185
|
+
"<#{self.class.name} name=#{name.inspect}>"
|
186
|
+
end
|
187
|
+
|
188
|
+
protected
|
189
|
+
|
190
|
+
def raw_data(xpath = nil, retry_ = false)
|
191
|
+
if xpath
|
192
|
+
result = ::REXML::XPath.match(raw_data, xpath)
|
193
|
+
|
194
|
+
if retry_ and result.empty?
|
195
|
+
@raw_data = self.class.raw_data(name)
|
196
|
+
return ::REXML::XPath.match(@raw_data, xpath)
|
197
|
+
else
|
198
|
+
return result
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
@raw_data if defined? @raw_data
|
203
|
+
@raw_data = self.class.raw_data(name)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
class String
|
209
|
+
# Converts string to a VLAAH::Topic.
|
210
|
+
#
|
211
|
+
# "Smalltalk".to_topic #=> <VLAAH::Topic name="Smalltalk">
|
212
|
+
# "VLAAH".to_topic.name #=> "VLAAH"
|
213
|
+
def to_topic
|
214
|
+
VLAAH::Topic.find(self)
|
215
|
+
end
|
216
|
+
end
|
data/lib/vlaah/unit.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
|
2
|
+
module VLAAH
|
3
|
+
class Unit < Numeric
|
4
|
+
def initialize(element)
|
5
|
+
@total = yield element.attributes['total']
|
6
|
+
@plus = yield element.attributes['plus']
|
7
|
+
@minus = yield element.attributes['minus']
|
8
|
+
end
|
9
|
+
|
10
|
+
def plus
|
11
|
+
@plus
|
12
|
+
end
|
13
|
+
|
14
|
+
def minus
|
15
|
+
@minus
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(method, *args)
|
19
|
+
@total.__send__(method, *args)
|
20
|
+
end
|
21
|
+
|
22
|
+
def coerce(operand)
|
23
|
+
[operand, @total.is_a?(Float) ? self.to_f : self.to_i]
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s(*args)
|
27
|
+
@total.to_s(*args)
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_i
|
31
|
+
@total.to_i
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_int
|
35
|
+
@total.to_int
|
36
|
+
end
|
37
|
+
|
38
|
+
def truncate
|
39
|
+
@total.truncate
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_f
|
43
|
+
@total.to_f
|
44
|
+
end
|
45
|
+
|
46
|
+
def +@
|
47
|
+
@total
|
48
|
+
end
|
49
|
+
|
50
|
+
def -@
|
51
|
+
-@total
|
52
|
+
end
|
53
|
+
|
54
|
+
def <=> operand
|
55
|
+
@total <=> operand
|
56
|
+
end
|
57
|
+
|
58
|
+
def inspect
|
59
|
+
"#{@total.inspect} <plus=#{@plus.inspect} minus=#{@minus.inspect}>"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/vlaah.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008 Lunant <http://lunant.net/>
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
$:.unshift File.dirname(__FILE__)
|
24
|
+
|
25
|
+
require 'vlaah/version'
|
26
|
+
require 'vlaah/base'
|
27
|
+
require 'vlaah/unit'
|
28
|
+
require 'vlaah/topic'
|
29
|
+
require 'vlaah/person'
|
30
|
+
require 'vlaah/comment'
|
31
|
+
require 'vlaah/comment_list'
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: vlaah
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.9.0
|
7
|
+
date: 2008-10-19 00:00:00 +09:00
|
8
|
+
summary: VLAAH API client library for Ruby.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email:
|
12
|
+
homepage: http://vlaah.rubyforge.org/
|
13
|
+
rubyforge_project:
|
14
|
+
description: It provides various model classes which wrap the raw protocol of VLAAH API. (http://api.vlaah.com/)
|
15
|
+
autorequire: vlaah
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- doc/template.rb
|
33
|
+
- lib/vlaah
|
34
|
+
- lib/vlaah/base.rb
|
35
|
+
- lib/vlaah/person.rb
|
36
|
+
- lib/vlaah/comment.rb
|
37
|
+
- lib/vlaah/version.rb
|
38
|
+
- lib/vlaah/topic.rb
|
39
|
+
- lib/vlaah/comment_list.rb
|
40
|
+
- lib/vlaah/unit.rb
|
41
|
+
- lib/vlaah/locale.rb
|
42
|
+
- lib/vlaah.rb
|
43
|
+
test_files: []
|
44
|
+
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
extra_rdoc_files: []
|
48
|
+
|
49
|
+
executables: []
|
50
|
+
|
51
|
+
extensions: []
|
52
|
+
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
dependencies: []
|
56
|
+
|