yanapi 0.4.0 → 0.4.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 +4 -0
- data/README +60 -9
- data/lib/yanapi/api.rb +2 -0
- data/lib/yanapi/common.rb +3 -1
- data/lib/yanapi/error.rb +25 -24
- data/lib/yanapi/query.rb +6 -8
- data/lib/yanapi/term_query.rb +1 -1
- data/lib/yanapi/version.rb +1 -1
- metadata +4 -4
data/CHANGELOG
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
== COMPLETED
|
2
|
+
=== 0.4.1
|
3
|
+
Switched to CGI for encoding URLs, a bug with parsing non english characters.
|
4
|
+
|
5
|
+
Enhanced the overall documentation.
|
2
6
|
=== 0.4.0
|
3
7
|
Implemented multivalued parameters as Arrays. You can now search in many regions
|
4
8
|
and categories simultaneously.
|
data/README
CHANGED
@@ -11,19 +11,28 @@
|
|
11
11
|
YANAPI is an API for Yahoo! Answers web services. It has been developed
|
12
12
|
in {Ruby}[http://www.ruby-lang.org].
|
13
13
|
|
14
|
-
YANAPI provides a flexible interface to the
|
14
|
+
YANAPI provides a flexible interface to the
|
15
|
+
{Yahoo! Answers}[http://answers.yahoo.com/] search services. As for this writing
|
16
|
+
it is the <b>most complete library</b> for accessing
|
17
|
+
{Yahoo! Answers}[http://answers.yahoo.com/] compared to other libraries on
|
18
|
+
{RubyGems}[http://rubygems.org/].
|
15
19
|
|
16
20
|
It supports four search types:
|
17
|
-
* key word search;
|
18
|
-
* search based on the category name or the category ID
|
19
|
-
|
20
|
-
* search for a
|
21
|
+
* {key word search}[http://developer.yahoo.com/answers/V1/questionSearch.html];
|
22
|
+
* {search based on the category name or the category ID
|
23
|
+
}[http://developer.yahoo.com/answers/V1/getByCategory.html];
|
24
|
+
* {search for a precise question ID
|
25
|
+
}[http://developer.yahoo.com/answers/V1/getQuestion.html];
|
26
|
+
* {search for a questions posted by a user with an ID
|
27
|
+
}[http://developer.yahoo.com/answers/V1/getByUser.html].
|
28
|
+
|
29
|
+
See CHANGELOG for features which are planned for future releases.
|
21
30
|
|
22
31
|
It is possible to restrict key word based search through a category.
|
23
32
|
|
24
33
|
Question Search and User Search cannot be extended by key words or category IDs.
|
25
34
|
|
26
|
-
YANAPI tries to as flexible as possible. It restricts unallowed parameter
|
35
|
+
YANAPI tries to be as flexible as possible. It restricts unallowed parameter
|
27
36
|
combinations and forces using mandatory ones. But it doesn't care about defaults.
|
28
37
|
For example, as for this writing the default output value is an xml based format.
|
29
38
|
If it changes in the future, the user will be responsible to choose the format.
|
@@ -35,7 +44,8 @@ YANAPI provides the minimal acceptable query.
|
|
35
44
|
To install YANAPI ussue the following command:
|
36
45
|
$ gem install yanapi
|
37
46
|
|
38
|
-
You might want to install versions prior to +0.3.1
|
47
|
+
You might want to install versions prior to +0.3.1+, if you are bound on
|
48
|
+
the old one dimensional parameter hash:
|
39
49
|
$ gem install yanapi -v 0.1.1
|
40
50
|
|
41
51
|
If you want to do a system wide installation, do this as root
|
@@ -72,14 +82,27 @@ A small example shall demostrate the usage:
|
|
72
82
|
api.get # => String
|
73
83
|
|
74
84
|
|
75
|
-
For details on particular keys and defaults see
|
85
|
+
For details on particular keys and defaults see
|
86
|
+
{the official description}[http://developer.yahoo.com/answers/]
|
87
|
+
and the RDoc documentation in this library.
|
76
88
|
|
77
|
-
|
89
|
+
=== Exception Hierarchy
|
78
90
|
While using YANAPI you can face three kinds of errors:
|
79
91
|
* <tt>YANAPI::UserError</tt>;
|
80
92
|
* <tt>YANAPI::ExternalError</tt>;
|
81
93
|
* <tt>YANAPI::ContentError</tt>.
|
82
94
|
|
95
|
+
The errors here are presented in the order they may occur
|
96
|
+
during YANAPI's work.
|
97
|
+
|
98
|
+
First YANAPI checks the user input and throws a <tt>YANAPI::UserError</tt>.
|
99
|
+
|
100
|
+
Then it fetches a response from a remote server, it can result
|
101
|
+
in a <tt>YANAPI::ExternalError</tt>.
|
102
|
+
|
103
|
+
If the response contains no <b>200 OK</b> code or the response body
|
104
|
+
cannot be parsed, the library throws a <tt>YANAPI::ContentError</tt>.
|
105
|
+
|
83
106
|
See the RDoc documentation on semantics of these errors.
|
84
107
|
|
85
108
|
All of them are subcalsses of <tt>YANAPI::Error</tt> which is in turn a subclass
|
@@ -88,9 +111,37 @@ of the standard +RuntimeError+.
|
|
88
111
|
If you want to intercept any and every exception thrown by YANAPI simply rescue
|
89
112
|
<tt>YANAPI::Error</tt>.
|
90
113
|
|
114
|
+
=== Parameter Semantics
|
115
|
+
YANAPI accepts following keys and values (given default values if appropriate):
|
116
|
+
{
|
117
|
+
:query_params => {
|
118
|
+
:appid => 'YahooDemo', # It is required, register your own ID.
|
119
|
+
:callback => 'str', # Only in combination with <:output => 'json'>.
|
120
|
+
:category_id => '123456', # See below.
|
121
|
+
:category_name => 'Wohnen', # See below.
|
122
|
+
:date_range => 'all', # '7'|'7-30'|'30-60'|'60-90'|'more90'
|
123
|
+
:filter => 'question', # 'best_answer'
|
124
|
+
:output => 'xml', # 'json'|'php'|'rss'
|
125
|
+
:query => 'Haus AND Grund',
|
126
|
+
:question_id => '123456',
|
127
|
+
:region => 'us', # 'de'|'uk'|'ca'|'au'|'in'|'es'|'br'|
|
128
|
+
# 'ar'|'mx'|'e1'|'it'|'fr'|'sg'
|
129
|
+
:results => 10, # 0..50 (0 returns the default value)
|
130
|
+
:search_in => "all", # "question" | "best_answer"
|
131
|
+
:sort => 'relevance', # 'date_desc'| 'date_asc'
|
132
|
+
:start => 0, # Now <= 1000, otherwise you'll get an empty response.
|
133
|
+
:type => 'all', # 'resolved'|'open'|'undecided'
|
134
|
+
:user_id => '123456'
|
135
|
+
},
|
136
|
+
:method => 'questionSearch', # 'getByUser'|'getByCategory'|'getQuestion'
|
137
|
+
}
|
138
|
+
|
91
139
|
== SEARCH STRATEGIES
|
92
140
|
This section will describe possible applications of YANAPI.
|
93
141
|
|
142
|
+
=== Boolean Search
|
143
|
+
|
144
|
+
|
94
145
|
== CHANGELOG
|
95
146
|
See CHANGELOG.
|
96
147
|
|
data/lib/yanapi/api.rb
CHANGED
data/lib/yanapi/common.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
module YANAPI
|
2
|
+
# This module is ment to be mixed into *Query classes to avoid duplication.
|
3
|
+
# It implements common parts for semantic checks.
|
2
4
|
module Common
|
3
5
|
private
|
4
6
|
def basic_check(params)
|
@@ -8,7 +10,7 @@ module YANAPI
|
|
8
10
|
def check_semantics(allowed, actual)
|
9
11
|
actual.each_key do |param|
|
10
12
|
unless allowed.include?(param.to_s)
|
11
|
-
raise
|
13
|
+
raise UserError, "The parameter #{param} is not allowed!"
|
12
14
|
end
|
13
15
|
end
|
14
16
|
end
|
data/lib/yanapi/error.rb
CHANGED
@@ -1,24 +1,17 @@
|
|
1
1
|
module YANAPI
|
2
|
+
# YANAPI::Error is not used anywhere in the code.
|
3
|
+
# If you want to intercept all error from YANAPI, simply rescue this one.
|
2
4
|
class Error < RuntimeError; end
|
3
5
|
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
class
|
8
|
-
# it takes a kind of Exception or String as the argument
|
9
|
-
def initialize(arg)
|
10
|
-
if arg.kind_of? String
|
11
|
-
msg = arg
|
12
|
-
else
|
13
|
-
msg = "You got illformed content:\n" +
|
14
|
-
"#{arg.message} (#{arg.class})"
|
15
|
-
end
|
16
|
-
super(msg)
|
17
|
-
end
|
6
|
+
# It is raised if the user has provided unvalid options.
|
7
|
+
# YANAPI intentionally does not handle any defaults,
|
8
|
+
# the user must correct the input hash.
|
9
|
+
class UserError < Error
|
18
10
|
end
|
19
11
|
|
20
12
|
# A technical error accessing the server,
|
21
|
-
# may be caught and handled with a new attempt.
|
13
|
+
# this error may be caught and handled with a new attempt.
|
14
|
+
# The services by Yahoo! are rather unstable and often unavailable.
|
22
15
|
class ExternalError < Error
|
23
16
|
def initialize(arg)
|
24
17
|
msg = "Some external error occured:\n"
|
@@ -29,16 +22,24 @@ module YANAPI
|
|
29
22
|
end
|
30
23
|
super(msg)
|
31
24
|
end
|
32
|
-
end
|
25
|
+
end
|
33
26
|
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
class
|
27
|
+
# The result is illformed (as xml|json|php|rss) and cannot be parsed
|
28
|
+
# or contains an error message (humans should read it carefully).
|
29
|
+
# Yahoo! APIs are irregular concerning errors,
|
30
|
+
# not every input incorrectness results in a HTTP Error Code,
|
31
|
+
# but often in a normal response with an error message in the response body.
|
32
|
+
class ContentError < Error
|
33
|
+
# it takes a kind of Exception or String as the argument
|
34
|
+
def initialize(arg)
|
35
|
+
if arg.kind_of? String
|
36
|
+
msg = arg
|
37
|
+
else
|
38
|
+
msg = "You got illformed content:\n" +
|
39
|
+
"#{arg.message} (#{arg.class})"
|
40
|
+
end
|
41
|
+
super(msg)
|
42
|
+
end
|
40
43
|
end
|
41
44
|
|
42
|
-
class UserError < Error
|
43
|
-
end
|
44
45
|
end # YANAPI
|
data/lib/yanapi/query.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
1
2
|
# This code is based on ideas from "rc_rest" and "yahoo" gems.
|
2
3
|
require 'nokogiri'
|
3
4
|
require 'net/http'
|
4
5
|
require 'uri'
|
6
|
+
require 'cgi'
|
5
7
|
|
6
8
|
require 'yanapi/error'
|
7
9
|
|
@@ -89,17 +91,13 @@ module YANAPI
|
|
89
91
|
end # check_params
|
90
92
|
|
91
93
|
# It returns an URI::HTTP object containing the complete url for the request.
|
94
|
+
# Use <CGI>, not <URI>, the latter is somehow buggy.
|
92
95
|
def build_url(params)
|
93
|
-
# URI does not know if our string contains special characters or not.
|
94
|
-
# That's why it treats them as special, we need a stricter form and
|
95
|
-
# provide a list of characters which should be escaped.
|
96
|
-
reserved_chars = Regexp.new(/[ !*'();:@&=+$,\/?#\]\[]/)
|
97
|
-
|
98
|
-
|
99
96
|
expanded_params = expand_params(params) # It is an array now!
|
100
97
|
escaped_params = expanded_params.map do |k, v|
|
101
|
-
k =
|
102
|
-
v =
|
98
|
+
k = CGI.escape(k.to_s)
|
99
|
+
v = CGI.escape(v.to_s)
|
100
|
+
|
103
101
|
"#{k}=#{v}"
|
104
102
|
end
|
105
103
|
|
data/lib/yanapi/term_query.rb
CHANGED
data/lib/yanapi/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yanapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 1
|
10
|
+
version: 0.4.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrei Beliankou
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-08-
|
18
|
+
date: 2011-08-14 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: nokogiri
|