yanapi 0.1.1 → 0.3.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.
- data/CHANGELOG +22 -0
- data/README +89 -7
- data/lib/yanapi.rb +1 -7
- data/lib/yanapi/api.rb +97 -0
- data/lib/yanapi/category_query.rb +14 -1
- data/lib/yanapi/error.rb +3 -0
- data/lib/yanapi/query.rb +82 -109
- data/lib/yanapi/question_query.rb +8 -2
- data/lib/yanapi/term_query.rb +16 -1
- data/lib/yanapi/user_query.rb +16 -3
- data/lib/yanapi/version.rb +1 -1
- data/test/data/bad_xml.txt +0 -221
- data/test/data/code_400.txt +15 -0
- data/test/data/code_403.txt +15 -0
- data/test/data/code_503.txt +15 -0
- data/test/integration_test_data/test_category_query.txt +61 -0
- data/test/integration_test_data/test_empty_response.txt +13 -0
- data/test/integration_test_data/test_question_query.txt +49 -0
- data/test/integration_test_data/test_term_query.txt +62 -0
- data/test/integration_test_data/test_user_query.txt +62 -0
- data/test/test_api.rb +154 -0
- data/test/test_category_query.rb +12 -1
- data/test/test_common.rb +1 -1
- data/test/test_helper.rb +31 -0
- data/test/test_integration.rb +129 -0
- data/test/test_query.rb +131 -66
- data/test/test_question_query.rb +14 -1
- data/test/test_term_query.rb +20 -10
- data/test/test_user_query.rb +15 -1
- data/test/test_version.rb +12 -0
- data/test/test_yanapi.rb +3 -2
- metadata +60 -14
- data/README.rdoc +0 -23
- data/Rakefile +0 -14
data/test/test_query.rb
CHANGED
@@ -1,107 +1,172 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require 'yanapi'
|
2
|
+
require 'yanapi/query'
|
3
3
|
require 'fakeweb'
|
4
4
|
|
5
5
|
class TestQuery < Test::Unit::TestCase
|
6
6
|
def setup
|
7
|
-
@params = {
|
7
|
+
@params = {
|
8
|
+
:method => 'questionSearch',
|
9
|
+
:query_params => {
|
10
|
+
:appid => 'YahooDemo',
|
11
|
+
:query => 'Haus',
|
12
|
+
:output => 'xml'
|
13
|
+
}
|
14
|
+
}
|
8
15
|
end
|
9
16
|
def teardown
|
10
17
|
end
|
11
18
|
|
19
|
+
## Structural tests
|
20
|
+
|
21
|
+
# It should have a public method <get>.
|
12
22
|
def test_public_methods
|
13
23
|
q = YANAPI::Query.new(@params)
|
14
24
|
assert_respond_to(q, :get)
|
15
25
|
end
|
16
26
|
|
27
|
+
# This class should declare the following constants.
|
17
28
|
def test_constants
|
18
|
-
|
29
|
+
defined_constants = [:HOST,
|
30
|
+
:SERVICE,
|
31
|
+
:SERVICE_VERSION,
|
32
|
+
:VALID_PARAMS,
|
33
|
+
:REQUIRED_PARAMS,
|
34
|
+
:VALID_OUTPUT_FORMATS
|
35
|
+
]
|
36
|
+
defined_constants.each do |const|
|
37
|
+
assert(YANAPI::Query.const_defined?(const))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# The following constants should have the given value and type.
|
42
|
+
def test_values_of_constants
|
43
|
+
assert_equal('http://answers.yahooapis.com',
|
44
|
+
YANAPI::Query::HOST)
|
19
45
|
assert_equal('AnswersService', YANAPI::Query::SERVICE)
|
20
46
|
assert_equal('V1', YANAPI::Query::SERVICE_VERSION)
|
21
|
-
assert(YANAPI::VERSION.is_a?(String) && ! YANAPI::VERSION.empty?)
|
22
47
|
end
|
23
48
|
|
49
|
+
## Interface tests
|
50
|
+
|
51
|
+
# It should reject the input hash without <:appid> parameter.
|
24
52
|
def test_missing_appid
|
25
|
-
@params.delete(:appid)
|
26
|
-
assert_raises(YANAPI::
|
27
|
-
q = YANAPI::Query.new(@params)
|
28
|
-
end
|
53
|
+
@params[:query_params].delete(:appid)
|
54
|
+
assert_raises(YANAPI::UserError) { YANAPI::Query.new(@params) }
|
29
55
|
end
|
30
56
|
|
31
|
-
#
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
57
|
+
# It should accept only predefined values of <:output>:
|
58
|
+
# xml|json|php|rss|nil
|
59
|
+
def test_output_semantics
|
60
|
+
@params[:query_params][:output] = 'trash'
|
61
|
+
assert_raises(YANAPI::UserError) { YANAPI::Query.new(@params) }
|
37
62
|
end
|
63
|
+
|
64
|
+
# It should infer the output type in every case.
|
65
|
+
def test_output_type
|
66
|
+
@params[:query_params].delete(:output)
|
67
|
+
q = YANAPI::Query.new(@params)
|
68
|
+
output = q.instance_variable_get(:@output)
|
69
|
+
assert_equal('xml', output)
|
38
70
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
assert_raises(YANAPI::Error) do
|
44
|
-
q = YANAPI::Query.new(@params)
|
45
|
-
end
|
71
|
+
@params[:query_params][:output] = 'php'
|
72
|
+
q = YANAPI::Query.new(@params)
|
73
|
+
output = q.instance_variable_get(:@output)
|
74
|
+
assert_equal('php', output)
|
46
75
|
end
|
47
76
|
|
48
|
-
# :
|
49
|
-
def
|
50
|
-
@params
|
51
|
-
|
52
|
-
|
77
|
+
# It should allow only the combination: <:callback> and <'json'>.
|
78
|
+
def test_parameters_pairing
|
79
|
+
@params[:query_params][:output] = 'json'
|
80
|
+
@params[:query_params][:callback] = 'wrapper'
|
81
|
+
# It is analoguous to <assert_nothing_raised>.
|
82
|
+
YANAPI::Query.new(@params)
|
83
|
+
|
84
|
+
@params[:query_params][:output] = 'xml'
|
85
|
+
assert_raises(YANAPI::UserError) { YANAPI::Query.new(@params) }
|
86
|
+
@params[:query_params].delete(:output)
|
87
|
+
assert_raises(YANAPI::UserError) { YANAPI::Query.new(@params) }
|
53
88
|
end
|
54
89
|
|
55
|
-
#
|
56
|
-
def
|
57
|
-
@params[:
|
58
|
-
assert_raises(YANAPI::
|
59
|
-
q = YANAPI::Query.new(@params)
|
60
|
-
end
|
90
|
+
# It should reject not unique values.
|
91
|
+
def test_uniqueness_of_values
|
92
|
+
@params[:query_params][:category_id] = ['123', 345]
|
93
|
+
assert_raises(YANAPI::UserError) { YANAPI::Query.new(@params) }
|
61
94
|
end
|
62
|
-
|
63
95
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
assert_raises(YANAPI::Error) {YANAPI::Query.new(@params)}
|
71
|
-
@params.delete(:output)
|
72
|
-
assert_raises(YANAPI::Error) {YANAPI::Query.new(@params)}
|
73
|
-
end
|
74
|
-
|
75
|
-
# we can get an empty response set with valid xml structure
|
76
|
-
# and the status code 200 HTTP OK
|
77
|
-
# but we should declene this
|
96
|
+
|
97
|
+
## Functional tests
|
98
|
+
|
99
|
+
# It should return <nil> if the server response contains
|
100
|
+
# no answer. It is possible if we out of range or search criteria
|
101
|
+
# are too strict.
|
78
102
|
def test_empty_response
|
79
103
|
q = YANAPI::Query.new(@params)
|
80
104
|
page = File.open('test/data/empty_result.txt') {|f| f.read}
|
81
|
-
|
82
|
-
|
105
|
+
url = q.instance_variable_get(:@url)
|
106
|
+
FakeWeb.register_uri(:get, url, :response => page)
|
107
|
+
assert_equal(nil, q.get)
|
83
108
|
end
|
84
109
|
|
110
|
+
# It should return a valid answer.
|
111
|
+
def test_successfull_response
|
112
|
+
file = 'test/data/successfull_response.txt'
|
113
|
+
http_answer = File.open(file) { |f| f.read }
|
114
|
+
# Deleting the http answer header, leaving xml structure.
|
115
|
+
expected_api_answer = http_answer.sub(/^[^<]+/m, '')
|
116
|
+
|
117
|
+
query = YANAPI::Query.new(@params)
|
118
|
+
url = query.instance_variable_get(:@url)
|
119
|
+
FakeWeb.register_uri(:get, url, :response => http_answer)
|
120
|
+
|
121
|
+
actual_api_answer = query.get
|
122
|
+
assert_instance_of(String, actual_api_answer, 'Not String!')
|
123
|
+
assert_equal(expected_api_answer, actual_api_answer)
|
124
|
+
|
125
|
+
warn "\nAdd tests for <php>, <json> and <rss>!\n"
|
126
|
+
end
|
127
|
+
|
128
|
+
# It should fail with an appropriate exception
|
129
|
+
# with a response containing errors.
|
85
130
|
def test_response_with_error
|
86
|
-
q =
|
87
|
-
|
88
|
-
|
89
|
-
FakeWeb.register_uri(:get, q.url, :response => page)
|
90
|
-
assert_raises(YANAPI::ContentError) {q.get}
|
91
|
-
|
92
|
-
page = File.open('test/data/error_code.txt') {|f| f.read}
|
93
|
-
FakeWeb.register_uri(:get, q.url, :response => page)
|
94
|
-
assert_raises(YANAPI::ContentError) {q.get}
|
131
|
+
q = create_query('test/data/response_with_error.txt')
|
132
|
+
assert_raises(YANAPI::ContentError) { q.get }
|
133
|
+
end
|
95
134
|
|
96
|
-
|
97
|
-
|
98
|
-
|
135
|
+
# It should fail with an appropriate exception
|
136
|
+
# with a non valid xml response.
|
137
|
+
def test_non_valid_xml
|
138
|
+
q = create_query('test/data/bad_xml.txt')
|
139
|
+
assert_raises(YANAPI::ContentError) { q.get }
|
140
|
+
end
|
141
|
+
|
142
|
+
# It should fail with an appropriate exception
|
143
|
+
# if given the error code 400
|
144
|
+
def test_code_400
|
145
|
+
q = create_query('test/data/code_400.txt')
|
146
|
+
assert_raises(YANAPI::ExternalError) { q.get }
|
99
147
|
end
|
100
148
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
149
|
+
# It should fail with an appropriate exception
|
150
|
+
# if given the error code 403
|
151
|
+
def test_code_403
|
152
|
+
q = create_query('test/data/code_403.txt')
|
153
|
+
assert_raises(YANAPI::ExternalError) { q.get }
|
154
|
+
end
|
155
|
+
|
156
|
+
# It should fail with an appropriate exception
|
157
|
+
# if given the error code 503
|
158
|
+
def test_code_503
|
159
|
+
q = create_query('test/data/code_503.txt')
|
160
|
+
assert_raises(YANAPI::ExternalError) { q.get }
|
161
|
+
end
|
162
|
+
|
163
|
+
# It is a helper method for asserting external and content errors.
|
164
|
+
def create_query(file)
|
165
|
+
query = YANAPI::Query.new(@params)
|
166
|
+
url = query.instance_variable_get(:@url)
|
167
|
+
page = File.open(file) { |f| f.read }
|
168
|
+
FakeWeb.register_uri(:get, url, :response => page)
|
169
|
+
|
170
|
+
query
|
106
171
|
end
|
107
172
|
end
|
data/test/test_question_query.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require 'yanapi'
|
2
|
+
require 'yanapi/question_query'
|
3
3
|
|
4
4
|
class TestQuestionQuery < Test::Unit::TestCase
|
5
5
|
def setup
|
@@ -7,4 +7,17 @@ class TestQuestionQuery < Test::Unit::TestCase
|
|
7
7
|
def teardown
|
8
8
|
end
|
9
9
|
|
10
|
+
# This class should declare the following constants.
|
11
|
+
def test_constants
|
12
|
+
defined_constants = [:VALID_PARAMS,
|
13
|
+
:REQUIRED_PARAMS
|
14
|
+
]
|
15
|
+
|
16
|
+
defined_constants.each do |const|
|
17
|
+
assert(YANAPI::QuestionQuery.const_defined?(const))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_sample
|
22
|
+
end
|
10
23
|
end #TestQuestionQuery
|
data/test/test_term_query.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require 'yanapi'
|
2
|
+
require 'yanapi/term_query'
|
3
3
|
require 'fakeweb'
|
4
4
|
|
5
5
|
class TestTermQuery < Test::Unit::TestCase
|
@@ -12,14 +12,27 @@ class TestTermQuery < Test::Unit::TestCase
|
|
12
12
|
def teardown
|
13
13
|
end
|
14
14
|
|
15
|
-
#
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
# This class should declare the following constants.
|
16
|
+
def test_constants
|
17
|
+
defined_constants = [:REQUIRED_PARAMS,
|
18
|
+
:VALID_PARAMS
|
19
|
+
]
|
20
|
+
|
21
|
+
defined_constants.each do |const|
|
22
|
+
assert(YANAPI::TermQuery.const_defined?(const))
|
23
|
+
end
|
21
24
|
end
|
25
|
+
|
26
|
+
# !!! OBSOLETE, TESTED IN test_query.rb
|
27
|
+
# questionSearch needs a unique :query
|
28
|
+
# def test_missing_query
|
29
|
+
# @params.delete(:query)
|
30
|
+
# assert_raises(YANAPI::Error) do
|
31
|
+
# YANAPI::TermQuery.new(@params)
|
32
|
+
# end
|
33
|
+
# end
|
22
34
|
|
35
|
+
# !!! AMBIGUOUS, SPLIT Query AND TermQuery
|
23
36
|
# parameters can be only: query|search_in|category_id|category_name|
|
24
37
|
# region|date_range|sort|appid|type|start|results|output|callback|method
|
25
38
|
def test_parameters_semantics
|
@@ -30,7 +43,4 @@ class TestTermQuery < Test::Unit::TestCase
|
|
30
43
|
end
|
31
44
|
end
|
32
45
|
|
33
|
-
# validates presense of query
|
34
|
-
# validates uniqueness of query
|
35
|
-
|
36
46
|
end #TestTermQuery
|
data/test/test_user_query.rb
CHANGED
@@ -1,9 +1,23 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require 'yanapi'
|
2
|
+
require 'yanapi/user_query'
|
3
3
|
|
4
4
|
class TestUserQuery < Test::Unit::TestCase
|
5
5
|
def setup
|
6
6
|
end
|
7
7
|
def teardown
|
8
8
|
end
|
9
|
+
|
10
|
+
# This class should declare the following constants.
|
11
|
+
def test_constants
|
12
|
+
defined_constants = [:VALID_PARAMS,
|
13
|
+
:REQUIRED_PARAMS
|
14
|
+
]
|
15
|
+
|
16
|
+
defined_constants.each do |const|
|
17
|
+
assert(YANAPI::UserQuery.const_defined?(const))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_sample
|
22
|
+
end
|
9
23
|
end #TestUserQuery
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'yanapi/version'
|
3
|
+
|
4
|
+
class TestVersion < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# This module should declare the following constants.
|
7
|
+
def test_constants
|
8
|
+
assert(YANAPI.const_defined?(:VERSION))
|
9
|
+
assert(YANAPI::VERSION.is_a?(String))
|
10
|
+
assert_equal(false, YANAPI::VERSION.empty?)
|
11
|
+
end
|
12
|
+
end
|
data/test/test_yanapi.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
|
3
1
|
require 'test_query'
|
4
2
|
require 'test_term_query'
|
5
3
|
require 'test_category_query'
|
6
4
|
require 'test_user_query'
|
7
5
|
require 'test_question_query'
|
8
6
|
require 'test_common'
|
7
|
+
require 'test_version'
|
8
|
+
require 'test_api'
|
9
|
+
require 'test_integration'
|
metadata
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yanapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 17
|
4
5
|
prerelease:
|
5
|
-
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 1
|
10
|
+
version: 0.3.1
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
13
|
- Andrei Beliankou
|
@@ -10,8 +15,7 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date: 2011-
|
14
|
-
default_executable:
|
18
|
+
date: 2011-08-05 00:00:00 Z
|
15
19
|
dependencies:
|
16
20
|
- !ruby/object:Gem::Dependency
|
17
21
|
name: nokogiri
|
@@ -21,6 +25,9 @@ dependencies:
|
|
21
25
|
requirements:
|
22
26
|
- - ">="
|
23
27
|
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
24
31
|
version: "0"
|
25
32
|
type: :runtime
|
26
33
|
version_requirements: *id001
|
@@ -32,6 +39,9 @@ dependencies:
|
|
32
39
|
requirements:
|
33
40
|
- - ">="
|
34
41
|
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
35
45
|
version: "0"
|
36
46
|
type: :development
|
37
47
|
version_requirements: *id002
|
@@ -42,8 +52,9 @@ executables: []
|
|
42
52
|
extensions: []
|
43
53
|
|
44
54
|
extra_rdoc_files:
|
45
|
-
- README
|
55
|
+
- README
|
46
56
|
- LICENSE
|
57
|
+
- CHANGELOG
|
47
58
|
files:
|
48
59
|
- lib/yanapi/error.rb
|
49
60
|
- lib/yanapi/user_query.rb
|
@@ -52,32 +63,43 @@ files:
|
|
52
63
|
- lib/yanapi/common.rb
|
53
64
|
- lib/yanapi/category_query.rb
|
54
65
|
- lib/yanapi/version.rb
|
66
|
+
- lib/yanapi/api.rb
|
55
67
|
- lib/yanapi/query.rb
|
56
68
|
- lib/yanapi.rb
|
57
|
-
- README.rdoc
|
58
|
-
- LICENSE
|
59
|
-
- Rakefile
|
60
69
|
- README
|
70
|
+
- LICENSE
|
71
|
+
- CHANGELOG
|
61
72
|
- test/test_yanapi.rb
|
73
|
+
- test/test_api.rb
|
62
74
|
- test/test_user_query.rb
|
75
|
+
- test/test_integration.rb
|
76
|
+
- test/integration_test_data/test_question_query.txt
|
77
|
+
- test/integration_test_data/test_category_query.txt
|
78
|
+
- test/integration_test_data/test_empty_response.txt
|
79
|
+
- test/integration_test_data/test_term_query.txt
|
80
|
+
- test/integration_test_data/test_user_query.txt
|
63
81
|
- test/test_query.rb
|
64
82
|
- test/test_category_query.rb
|
65
83
|
- test/test_term_query.rb
|
84
|
+
- test/test_version.rb
|
85
|
+
- test/test_helper.rb
|
66
86
|
- test/test_common.rb
|
67
87
|
- test/test_question_query.rb
|
88
|
+
- test/data/code_400.txt
|
68
89
|
- test/data/bad_xml.txt
|
90
|
+
- test/data/code_503.txt
|
69
91
|
- test/data/empty_result.txt
|
70
92
|
- test/data/error_code.txt
|
71
93
|
- test/data/response_with_error.txt
|
72
94
|
- test/data/successfull_response.txt
|
73
|
-
|
95
|
+
- test/data/code_403.txt
|
74
96
|
homepage: http://www.uni-trier.de/index.php?id=34451
|
75
97
|
licenses: []
|
76
98
|
|
77
99
|
post_install_message:
|
78
100
|
rdoc_options:
|
79
101
|
- -m
|
80
|
-
- README
|
102
|
+
- README
|
81
103
|
require_paths:
|
82
104
|
- lib
|
83
105
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -85,25 +107,49 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
107
|
requirements:
|
86
108
|
- - ">="
|
87
109
|
- !ruby/object:Gem::Version
|
110
|
+
hash: 31
|
111
|
+
segments:
|
112
|
+
- 1
|
113
|
+
- 8
|
88
114
|
version: "1.8"
|
89
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
116
|
none: false
|
91
117
|
requirements:
|
92
118
|
- - ">="
|
93
119
|
- !ruby/object:Gem::Version
|
120
|
+
hash: 3
|
121
|
+
segments:
|
122
|
+
- 0
|
94
123
|
version: "0"
|
95
124
|
requirements: []
|
96
125
|
|
97
126
|
rubyforge_project: yanapi
|
98
|
-
rubygems_version: 1.
|
127
|
+
rubygems_version: 1.7.2
|
99
128
|
signing_key:
|
100
129
|
specification_version: 3
|
101
130
|
summary: YANAPI is an API for Yahoo! Answers web services.
|
102
131
|
test_files:
|
132
|
+
- test/test_yanapi.rb
|
133
|
+
- test/test_api.rb
|
134
|
+
- test/test_user_query.rb
|
135
|
+
- test/test_integration.rb
|
136
|
+
- test/integration_test_data/test_question_query.txt
|
137
|
+
- test/integration_test_data/test_category_query.txt
|
138
|
+
- test/integration_test_data/test_empty_response.txt
|
139
|
+
- test/integration_test_data/test_term_query.txt
|
140
|
+
- test/integration_test_data/test_user_query.txt
|
141
|
+
- test/test_query.rb
|
103
142
|
- test/test_category_query.rb
|
143
|
+
- test/test_term_query.rb
|
144
|
+
- test/test_version.rb
|
145
|
+
- test/test_helper.rb
|
104
146
|
- test/test_common.rb
|
105
|
-
- test/test_query.rb
|
106
147
|
- test/test_question_query.rb
|
107
|
-
- test/
|
108
|
-
- test/
|
109
|
-
- test/
|
148
|
+
- test/data/code_400.txt
|
149
|
+
- test/data/bad_xml.txt
|
150
|
+
- test/data/code_503.txt
|
151
|
+
- test/data/empty_result.txt
|
152
|
+
- test/data/error_code.txt
|
153
|
+
- test/data/response_with_error.txt
|
154
|
+
- test/data/successfull_response.txt
|
155
|
+
- test/data/code_403.txt
|