ws-aboutme 0.5.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.
@@ -0,0 +1,22 @@
1
+ module WebService #:nodoc:
2
+ module Aboutme #:nodoc:
3
+ module Version #:nodoc:
4
+ MAJOR = 0
5
+ MINOR = 5
6
+ TINY = 1
7
+
8
+ STRING = [MAJOR, MINOR, TINY].join('.')
9
+ NAME = [MAJOR, MINOR, TINY].join('_')
10
+
11
+ class << self
12
+ def to_version
13
+ STRING
14
+ end
15
+
16
+ def to_name
17
+ NAME
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,110 @@
1
+ require 'date'
2
+ require 'rexml/document'
3
+
4
+ module WebService #:nodoc:
5
+ module Aboutme #:nodoc:
6
+ module XmlObjectMapping #:nodoc:
7
+ def XmlObjectMapping.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods #:nodoc:
12
+ def attr_mapping(name, args = {})
13
+ unless args[:private]
14
+ attr_accessor name
15
+ end
16
+
17
+ mapping = {
18
+ :name => name,
19
+ :type => args[:type] || :string,
20
+ :array => args[:array] || false,
21
+ :subnode => args[:subnode],
22
+ }
23
+ path = args[:path] || name.to_s
24
+
25
+ @attributes ||= {}
26
+ @attributes[path] = mapping
27
+ end
28
+
29
+ def attr_array(name, args = {})
30
+ attr_mapping(name, args.merge(:array => true))
31
+ end
32
+ end
33
+
34
+ class Base #:nodoc:
35
+ include XmlObjectMapping
36
+
37
+ def self.unmarshal(xml)
38
+ self.new.__send__ :populate, xml
39
+ end
40
+
41
+
42
+ private
43
+
44
+ def populate(xml)
45
+ attributes = collect_mappings(self.class)
46
+
47
+ xml.elements.each do |node|
48
+ name = node.name
49
+ mapping = attributes[name]
50
+ next unless mapping
51
+ array = mapping[:array]
52
+
53
+ value = array ? get_attribute_array(node, mapping) : get_attribute_value(node, mapping)
54
+ instance_variable_set("@#{mapping[:name]}", value)
55
+ end
56
+
57
+ post_population
58
+ self
59
+ end
60
+
61
+ def collect_mappings(clazz)
62
+ if clazz == Base
63
+ {}
64
+ else
65
+ base_mappings = collect_mappings(clazz.superclass)
66
+ self_mappings = clazz.class_eval { @attributes } || {}
67
+ base_mappings.merge(self_mappings)
68
+ end
69
+ end
70
+
71
+ def post_population
72
+ end
73
+
74
+ def get_attribute_array(node, mapping)
75
+ list = []
76
+ m = mapping.dup
77
+ subnode = m.delete :subnode
78
+ node.elements.each(subnode) do |e|
79
+ list << get_attribute_value(e, m)
80
+ end
81
+ list
82
+ end
83
+
84
+ def get_attribute_value(node, mapping)
85
+ type = mapping[:type]
86
+ str = node.text
87
+ case type
88
+ when :string
89
+ str
90
+ when :integer
91
+ str.to_i
92
+ when :date
93
+ Date.parse(str)
94
+ when :datetime
95
+ DateTime.parse(str)
96
+ else
97
+ subnode = mapping[:subnode]
98
+ if subnode
99
+ nodes = node.get_elements(subnode)
100
+ nodes.empty? ? nil : type.unmarshal(nodes[0])
101
+ else
102
+ type.unmarshal(node)
103
+ end
104
+ end
105
+ end
106
+
107
+ end
108
+ end
109
+ end
110
+ end
data/test/tc_config.rb ADDED
@@ -0,0 +1,54 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ #
4
+ require File.join(File.dirname(__FILE__), 'test_helper')
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'webservice', 'aboutme', 'api')
6
+
7
+ require 'test/unit'
8
+
9
+ class ConfigTest < Test::Unit::TestCase
10
+
11
+ include TestHelper
12
+ include WebService::Aboutme
13
+
14
+ def test_new
15
+ config = API.new.config
16
+ assert_equal 'api.aboutme.jp', config.request_domain
17
+ assert_equal 80, config.request_port
18
+ assert_equal '/api/v1/', config.request_path_base
19
+ assert config.open_timeout > 0
20
+ assert config.read_timeout > 0
21
+ assert_not_nil config.user_agent
22
+ end
23
+
24
+ def test_new_with_parameters
25
+ config = API.new(:request_domain => 'testdomain',
26
+ :request_port => 8080,
27
+ :open_timeout => 22,
28
+ :read_timeout => 33
29
+ ).config
30
+ assert_equal 'testdomain', config.request_domain
31
+ assert_equal 8080, config.request_port
32
+ assert_equal '/api/v1/', config.request_path_base
33
+ assert_equal 22, config.open_timeout
34
+ assert_equal 33, config.read_timeout
35
+ assert_not_nil config.user_agent
36
+ end
37
+
38
+ def test_new_with_block
39
+ config = API.new do |c|
40
+ c.request_domain = 'test.domain'
41
+ c.request_port = 8888
42
+ c.open_timeout = 77
43
+ c.read_timeout = 88
44
+ c.user_agent = 'testUA'
45
+ end.config
46
+
47
+ assert_equal 'test.domain', config.request_domain
48
+ assert_equal 8888, config.request_port
49
+ assert_equal '/api/v1/', config.request_path_base
50
+ assert_equal 77, config.open_timeout
51
+ assert_equal 88, config.read_timeout
52
+ assert_equal 'testUA', config.user_agent
53
+ end
54
+ end
@@ -0,0 +1,59 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ #
4
+ require File.join(File.dirname(__FILE__), 'test_helper')
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'webservice', 'aboutme', 'api')
6
+
7
+ require 'date'
8
+ require 'test/unit'
9
+
10
+ class SearchAnswerResponseTest < Test::Unit::TestCase
11
+
12
+ include TestHelper
13
+ include WebService::Aboutme
14
+
15
+ def test_unmarshal
16
+ xml = parse_xml('tc_search_answer_response.xml')
17
+ response = SearchAnswerResponse.unmarshal(xml.root)
18
+ assert_not_nil response
19
+
20
+ # response.status.*
21
+ s = response.status
22
+ assert_not_nil s
23
+ assert_equal 200, s.code
24
+ assert_equal 'OK', s.message
25
+ assert_equal 'ja', s.language
26
+
27
+ # response.summary.*
28
+ summary = response.summary
29
+ assert_not_nil summary
30
+ assert_equal 999, summary.total
31
+ assert_equal 111, summary.page
32
+ assert_equal 222, summary.page_count
33
+
34
+ # response.answers
35
+ as = response.answers
36
+ assert_not_nil as
37
+ assert_equal 2, as.size
38
+
39
+ # response.answers[0].*
40
+ a = as[0]
41
+ assert_equal 99999, a.id
42
+ assert_equal 'answer99999', a.title
43
+ assert_equal 101010, a.question_id
44
+ assert_equal DateTime.parse('2007-07-31T11:22:33+09:00'), a.updated_at
45
+ assert_equal 1234, a.answerer.id
46
+ assert_equal 'test-user1234', a.answerer.nickname
47
+
48
+ # response.answers[1].*
49
+ a = as[1]
50
+ assert_equal 88888, a.id
51
+ assert_equal 'answer88888', a.title
52
+ assert_equal 101010, a.question_id
53
+ assert_equal DateTime.parse('2007-07-31T22:33:44+09:00'), a.updated_at
54
+ assert_equal 9876, a.answerer.id
55
+ assert_equal 'test-user9876', a.answerer.nickname
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,40 @@
1
+ <?xml version="1.0" encoding="UTF-8" ?>
2
+ <response>
3
+ <status>
4
+ <code>200</code>
5
+ <message>OK</message>
6
+ <language>ja</language>
7
+ </status>
8
+
9
+ <result>
10
+ <summary>
11
+ <total>999</total>
12
+ <page>111</page>
13
+ <page_count>222</page_count>
14
+ </summary>
15
+
16
+ <answers>
17
+ <answer>
18
+ <id>99999</id>
19
+ <title>answer99999</title>
20
+ <question_id>101010</question_id>
21
+ <updated_at>2007-07-31T11:22:33+09:00</updated_at>
22
+ <answerer>
23
+ <id>1234</id>
24
+ <nickname>test-user1234</nickname>
25
+ </answerer>
26
+ </answer>
27
+
28
+ <answer>
29
+ <id>88888</id>
30
+ <title>answer88888</title>
31
+ <question_id>101010</question_id>
32
+ <updated_at>2007-07-31T22:33:44+09:00</updated_at>
33
+ <answerer>
34
+ <id>9876</id>
35
+ <nickname>test-user9876</nickname>
36
+ </answerer>
37
+ </answer>
38
+ </answers>
39
+ </result>
40
+ </response>
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ #
4
+ require File.join(File.dirname(__FILE__), 'test_helper')
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'webservice', 'aboutme', 'api')
6
+
7
+ require 'date'
8
+ require 'test/unit'
9
+
10
+ class SearchQuestionResponseTest < Test::Unit::TestCase
11
+
12
+ include TestHelper
13
+ include WebService::Aboutme
14
+
15
+ def test_unmarshal
16
+ xml = parse_xml('tc_search_question_response.xml')
17
+ response = SearchQuestionResponse.unmarshal(xml.root)
18
+ assert_not_nil response
19
+
20
+ # response.status.*
21
+ s = response.status
22
+ assert_not_nil s
23
+ assert_equal 200, s.code
24
+ assert_equal 'OK', s.message
25
+ assert_equal 'ja', s.language
26
+
27
+ # response.summary.*
28
+ summary = response.summary
29
+ assert_not_nil summary
30
+ assert_equal 123, summary.total
31
+ assert_equal 99, summary.page
32
+ assert_equal 12, summary.page_count
33
+
34
+ # response.questions
35
+ qs = response.questions
36
+ assert_not_nil qs
37
+ assert_equal 2, qs.size
38
+
39
+ # response.questions[0].*
40
+ q = qs[0]
41
+ assert_equal 98765, q.id
42
+ assert_equal 'test98765', q.title
43
+ assert_equal 'http://aboutme.jp/question/show/98765', q.url
44
+ assert_equal 1234, q.creator.id
45
+ assert_equal 2, q.choices.size
46
+
47
+ # response.questions[1].*
48
+ q = qs[1]
49
+ assert_equal 12345, q.id
50
+ assert_equal 'test12345', q.title
51
+ assert_equal 'http://aboutme.jp/question/show/12345', q.url
52
+ assert_equal 1111, q.creator.id
53
+ assert_equal 3, q.choices.size
54
+ end
55
+
56
+ end
@@ -0,0 +1,65 @@
1
+ <?xml version="1.0" encoding="UTF-8" ?>
2
+ <response>
3
+ <status>
4
+ <code>200</code>
5
+ <message>OK</message>
6
+ <language>ja</language>
7
+ </status>
8
+
9
+ <result>
10
+ <summary>
11
+ <total>123</total>
12
+ <page>99</page>
13
+ <page_count>12</page_count>
14
+ </summary>
15
+
16
+ <questions>
17
+ <question>
18
+ <id>98765</id>
19
+ <title>test98765</title>
20
+ <url>http://aboutme.jp/question/show/98765</url>
21
+ <creator>
22
+ <id>1234</id>
23
+ <nickname>test-user1234</nickname>
24
+ </creator>
25
+ <created_at>2007-07-31T12:34:56+09:00</created_at>
26
+ <options>
27
+ <option>
28
+ <priority>1</priority>
29
+ <title>test98765-1</title>
30
+ </option>
31
+ <option>
32
+ <priority>2</priority>
33
+ <title>test98765-2</title>
34
+ </option>
35
+ </options>
36
+ </question>
37
+
38
+ <question>
39
+ <id>12345</id>
40
+ <title>test12345</title>
41
+ <url>http://aboutme.jp/question/show/12345</url>
42
+ <creator>
43
+ <id>1111</id>
44
+ <nickname>test-user1111</nickname>
45
+ </creator>
46
+ <created_at>2007-07-31T11:22:33+09:00</created_at>
47
+ <options>
48
+ <option>
49
+ <priority>1</priority>
50
+ <title>test12345-1</title>
51
+ </option>
52
+ <option>
53
+ <priority>2</priority>
54
+ <title>test12345-2</title>
55
+ </option>
56
+ <option>
57
+ <priority>3</priority>
58
+ <title>test12345-3</title>
59
+ </option>
60
+ </options>
61
+ </question>
62
+ </questions>
63
+
64
+ </result>
65
+ </response>
@@ -0,0 +1,52 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ #
4
+ require File.join(File.dirname(__FILE__), 'test_helper')
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'webservice', 'aboutme', 'api')
6
+
7
+ require 'date'
8
+ require 'test/unit'
9
+
10
+ class SearchUserResponseTest < Test::Unit::TestCase
11
+
12
+ include TestHelper
13
+ include WebService::Aboutme
14
+
15
+ def test_unmarshal
16
+ xml = parse_xml('tc_search_user_response.xml')
17
+ response = SearchUserResponse.unmarshal(xml.root)
18
+ assert_not_nil response
19
+
20
+ # response.status.*
21
+ s = response.status
22
+ assert_not_nil s
23
+ assert_equal 200, s.code
24
+ assert_equal 'OK', s.message
25
+ assert_equal 'ja', s.language
26
+
27
+ # response.summary.*
28
+ summary = response.summary
29
+ assert_not_nil summary
30
+ assert_equal 99, summary.total
31
+ assert_equal 2, summary.page
32
+ assert_equal 10, summary.page_count
33
+
34
+ # response.users[*]
35
+ users = response.users
36
+ assert_not_nil users
37
+ assert_equal 2, users.size
38
+
39
+ # response.users[0].*
40
+ u = users[0]
41
+ assert_equal 123, u.id
42
+ assert_equal 'test-user123', u.nickname
43
+ assert_equal 'http://test-user123.aboutme.jp/', u.url
44
+
45
+ # response.users[1].*
46
+ u = users[1]
47
+ assert_equal 9876, u.id
48
+ assert_equal 'test-user9876', u.nickname
49
+ assert_equal 'http://test-user9876.aboutme.jp/', u.url
50
+ end
51
+
52
+ end