thebigdb 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/Gemfile.lock +3 -1
- data/README.md +30 -35
- data/lib/thebigdb/helpers.rb +49 -0
- data/lib/thebigdb/request.rb +27 -57
- data/lib/thebigdb/resources/statement.rb +54 -0
- data/lib/thebigdb/version.rb +1 -1
- data/lib/thebigdb.rb +8 -4
- data/spec/helpers_spec.rb +72 -0
- data/spec/request_spec.rb +10 -60
- data/spec/resources/statement_spec.rb +61 -11
- data/spec/spec_helper.rb +2 -0
- data/thebigdb.gemspec +1 -1
- metadata +22 -5
- data/lib/thebigdb/resources/toolbox.rb +0 -12
- data/spec/resources/toolbox_spec.rb +0 -29
data/.travis.yml
ADDED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
thebigdb (1.
|
4
|
+
thebigdb (1.2.0)
|
5
5
|
rack (~> 1.4)
|
6
6
|
|
7
7
|
GEM
|
@@ -29,6 +29,7 @@ GEM
|
|
29
29
|
method_source (~> 0.8)
|
30
30
|
slop (~> 3.4)
|
31
31
|
rack (1.5.2)
|
32
|
+
rake (10.0.3)
|
32
33
|
rb-fchange (0.0.6)
|
33
34
|
ffi
|
34
35
|
rb-fsevent (0.9.3)
|
@@ -54,6 +55,7 @@ PLATFORMS
|
|
54
55
|
|
55
56
|
DEPENDENCIES
|
56
57
|
guard-rspec (~> 2.3)
|
58
|
+
rake (~> 10.0.3)
|
57
59
|
rb-fchange
|
58
60
|
rb-fsevent
|
59
61
|
rb-inotify
|
data/README.md
CHANGED
@@ -1,49 +1,41 @@
|
|
1
1
|
# TheBigDB Ruby Wrapper
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://secure.travis-ci.org/thebigdb/thebigdb-ruby.png)](http://travis-ci.org/thebigdb/thebigdb-ruby)
|
4
|
+
|
5
|
+
A simple ruby wrapper for making requests to the API of [TheBigDB.com](http://thebigdb.com). [Full API documentation](http://developers.thebigdb.com/api).
|
4
6
|
|
5
7
|
## Install
|
6
8
|
|
7
9
|
gem install thebigdb
|
8
10
|
|
9
|
-
##
|
10
|
-
|
11
|
-
Make your requests using this structure:
|
12
|
-
|
11
|
+
## Simple usage
|
13
12
|
|
14
|
-
|
13
|
+
The following actions return a TheBigDB::StatementRequest object, on which you can add params using ``.with(hash_of_params)``.
|
14
|
+
The request will be executed once you call regular methods of Hash on it (``each_pair``, ``[key]``, etc.), or force it with ``execute``.
|
15
|
+
The Hash returned represents the server's JSON response.
|
15
16
|
|
17
|
+
### Search \([api doc](http://developers.thebigdb.com/api#statements-search)\)
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
TheBigDB.search("iPhone").with(page: 2)
|
20
|
+
TheBigDB.search({match: "James"}, "job", "President of the United States")
|
21
|
+
TheBigDB.search({match: "Facebook"}, "job", {match: "Executive"})
|
19
22
|
|
23
|
+
### Create \([api doc](http://developers.thebigdb.com/api#statements-create)\)
|
20
24
|
|
21
|
-
|
22
|
-
|
23
|
-
request = TheBigDB::Statement(:search,
|
24
|
-
{
|
25
|
-
nodes: [{search: ""}, "job", "President of the United States"],
|
26
|
-
period: {from: "2000-01-01 00:00:00", to: "2002-01-01 00:00:00"}
|
27
|
-
}
|
28
|
-
)
|
29
|
-
|
30
|
-
puts request.response
|
25
|
+
TheBigDB.create("iPhone 5", "weight", "112 grams")
|
26
|
+
TheBigDB.create("Bill Clinton", "job", "President of the United States").with(period: {from: "1993-01-20 12:00:00", to: "2001-01-20 11:59:59"})
|
31
27
|
|
32
|
-
|
28
|
+
### Show \([api doc](http://developers.thebigdb.com/api#statements-show)\), Upvote \([api doc](http://developers.thebigdb.com/api#statements-upvote)\) and Downvote \([api doc](http://developers.thebigdb.com/api#statements-downvote)\)
|
33
29
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
{"nodes" => ["Bill Clinton", "job", "President of the United States"], "id" => "8e6aec890c942b6f7854d2d7a9f0d002f5ddd0c0", "period"=>{"from" => "1993-01-20 00:00:00", "to" => "2001-01-20 00:00:00"}},
|
38
|
-
{"nodes" => ["George W. Bush", "job", "President of the United States"], "id" => "3f27673816455054032bd46e65bbe4db8ccf9076", "period"=>{"from" => "2001-01-20 00:00:00", "to" => "2009-01-20 00:00:00"}}
|
39
|
-
]
|
40
|
-
}
|
30
|
+
TheBigDB.show("id-of-the-sentence")
|
31
|
+
TheBigDB.upvote("id-of-the-sentence")
|
32
|
+
TheBigDB.downvote("id-of-the-sentence")
|
41
33
|
|
42
34
|
That's it!
|
43
35
|
|
44
|
-
##
|
36
|
+
## TheBigDB::Request object
|
45
37
|
|
46
|
-
|
38
|
+
If you want more details on what is sent and what is received, you can use the generic TheBigDB::Statement method. It returns a TheBigDB::Request object.
|
47
39
|
It has several readable attributes:
|
48
40
|
|
49
41
|
request = TheBigDB::Statement(:search, {nodes: ["iPhone", "weight"]})
|
@@ -59,20 +51,26 @@ It has several readable attributes:
|
|
59
51
|
You can access other parts of the API in the same way as statements:
|
60
52
|
|
61
53
|
TheBigDB::User(action, parameters)
|
62
|
-
TheBigDB::Toolbox::Unit(action, parameters)
|
63
54
|
|
64
55
|
# Examples
|
65
56
|
TheBigDB::User(:show, {login: "christophe"}).response["user"]["karma"]
|
66
|
-
TheBigDB::Toolbox::Unit(:compare, {values: ["100 g", "1.2 kg"]}).response["result"]
|
67
57
|
|
68
58
|
You can modify the TheBigDB module with several configuration options:
|
69
59
|
|
70
60
|
TheBigDB.api_key = "your-private-api-key" # default: nil
|
71
61
|
TheBigDB.use_ssl = true # default: false
|
72
62
|
|
63
|
+
# If true, and a request response has {"status" => "error"}, it will raise a TheBigDB::Request::ApiStatusError exception with the API error code
|
64
|
+
TheBigDB.raise_on_api_status_error = true # default: false
|
65
|
+
|
73
66
|
# Both of them take a Proc or a Lambda, the TheBigDB::Request instance is passed as argument
|
74
|
-
TheBigDB.before_request_execution = ->(request){
|
75
|
-
TheBigDB.after_request_execution = ->(request){
|
67
|
+
TheBigDB.before_request_execution = ->(request){ logger.info(request.data_sent) } # default: Proc.new{}
|
68
|
+
TheBigDB.after_request_execution = ->(request){ logger.info(request.data_received) } # default: Proc.new{}
|
69
|
+
|
70
|
+
# You can also modify the configuration temporarily
|
71
|
+
TheBigDB.with_configuration(use_ssl: true) do
|
72
|
+
# your code here
|
73
|
+
end
|
76
74
|
|
77
75
|
|
78
76
|
## Contributing
|
@@ -86,6 +84,3 @@ Don't hesitate to send a pull request !
|
|
86
84
|
## License
|
87
85
|
|
88
86
|
This software is distributed under the MIT License. Copyright (c) 2013, Christophe Maximin <christophe@thebigdb.com>
|
89
|
-
|
90
|
-
|
91
|
-
[0]: http://thebigdb.com
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module TheBigDB
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
# serialize_query_params({house: "bricks", animals: ["cat", "dog"], computers: {cool: true, drives: ["hard", "flash"]}})
|
5
|
+
# => house=bricks&animals%5B%5D=cat&animals%5B%5D=dog&computers%5Bcool%5D=true&computers%5Bdrives%5D%5B%5D=hard&computers%5Bdrives%5D%5B%5D=flash
|
6
|
+
# which will be read by the server as:
|
7
|
+
# => house=bricks&animals[]=cat&animals[]=dog&computers[cool]=true&computers[drives][]=hard&computers[drives][]=flash
|
8
|
+
def self.serialize_query_params(params, prefix = nil)
|
9
|
+
ret = []
|
10
|
+
params.each_pair do |key, value|
|
11
|
+
param_key = prefix ? "#{prefix}[#{key}]" : key
|
12
|
+
|
13
|
+
if value.is_a?(Hash)
|
14
|
+
ret << self.serialize_query_params(value, param_key.to_s)
|
15
|
+
elsif value.is_a?(Array)
|
16
|
+
sub_hash = {}
|
17
|
+
value.each_with_index do |value_item, i|
|
18
|
+
sub_hash[i.to_s] = value_item
|
19
|
+
end
|
20
|
+
ret << self.serialize_query_params(sub_hash, param_key.to_s)
|
21
|
+
else
|
22
|
+
ret << URI.encode_www_form_component(param_key.to_s) + "=" + URI.encode_www_form_component(value.to_s)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
ret.join("&")
|
26
|
+
end
|
27
|
+
|
28
|
+
# flatten_params_keys({house: "bricks", animals: ["cat", "dog"], computers: {cool: true, drives: ["hard", "flash"]}})
|
29
|
+
# => {"house" => "bricks", "animals[0]" => "cat", "animals[1]" => "dog", "computers[cool]" => "true", "computers[drives][0]" => "hard", "computers[drives][1]" => "flash"}
|
30
|
+
def self.flatten_params_keys(params)
|
31
|
+
serialized_params = self.serialize_query_params(params)
|
32
|
+
new_params = {}
|
33
|
+
serialized_params.split("&").each do |assign|
|
34
|
+
key, value = assign.split("=")
|
35
|
+
new_params[URI.decode(key)] = URI.decode(value)
|
36
|
+
end
|
37
|
+
new_params
|
38
|
+
end
|
39
|
+
|
40
|
+
# Inspired by on ActiveSupport's stringify_keys
|
41
|
+
def self.stringify_keys(hash)
|
42
|
+
hash.inject({}) do |options, (key, value)|
|
43
|
+
options[key.to_s] = value
|
44
|
+
options
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
data/lib/thebigdb/request.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module TheBigDB
|
2
2
|
class Request
|
3
|
+
class ApiStatusError < StandardError; end
|
4
|
+
|
3
5
|
attr_reader :http, :http_request, :http_response, :data_sent, :data_received, :response
|
4
6
|
|
5
7
|
# Prepares the basic @http object with the current values of the module (host, port, ...)
|
@@ -24,11 +26,11 @@ module TheBigDB
|
|
24
26
|
request_uri = "/v#{TheBigDB.api_version}" + (request_uri.start_with?("/") ? request_uri : "/#{request_uri}")
|
25
27
|
|
26
28
|
if method == "get"
|
27
|
-
encoded_params = TheBigDB::
|
29
|
+
encoded_params = TheBigDB::Helpers::serialize_query_params(params)
|
28
30
|
@http_request = Net::HTTP::Get.new(request_uri + "?" + encoded_params)
|
29
31
|
elsif method == "post"
|
30
32
|
@http_request = Net::HTTP::Post.new(request_uri)
|
31
|
-
@http_request.set_form_data(TheBigDB::
|
33
|
+
@http_request.set_form_data(TheBigDB::Helpers::flatten_params_keys(params))
|
32
34
|
else
|
33
35
|
raise ArgumentError, "The request method must be 'get' or 'post'"
|
34
36
|
end
|
@@ -50,28 +52,14 @@ module TheBigDB
|
|
50
52
|
# Actually makes the request prepared in @http_request, and sets @http_response
|
51
53
|
def execute
|
52
54
|
# Here is the order of operations:
|
55
|
+
# -> setting @data_sent
|
53
56
|
# -> executing before_request_execution callback
|
54
57
|
# -> executing the HTTP request
|
55
58
|
# -> setting @response
|
56
|
-
# -> setting @data_sent
|
57
59
|
# -> setting @data_received
|
58
60
|
# -> executing after_request_execution callback
|
59
61
|
|
60
|
-
#
|
61
|
-
TheBigDB.before_request_execution.call(self)
|
62
|
-
|
63
|
-
# Here is where the request is actually executed
|
64
|
-
@http_response = @http.request(@http_request)
|
65
|
-
|
66
|
-
# Setting @response
|
67
|
-
begin
|
68
|
-
# We parse the JSON answer and return it.
|
69
|
-
@response = JSON(@http_response.body)
|
70
|
-
rescue JSON::ParserError => e
|
71
|
-
@response = {"error" => {"code" => "0000", "description" => "The server gave an invalid JSON body:\n#{@http_response.body}"}}
|
72
|
-
end
|
73
|
-
|
74
|
-
# Setting @data_sent and @data_received
|
62
|
+
# Setting @data_sent
|
75
63
|
params = Rack::Utils.parse_nested_query(URI.parse(@http_request.path).query)
|
76
64
|
# Since that's how it will be interpreted anyway on the server, we merge the POST params to the GET params,
|
77
65
|
# but it's not supposed to happen: either every params is prepared for GET/query params, or as POST body
|
@@ -92,54 +80,36 @@ module TheBigDB
|
|
92
80
|
"params" => params
|
93
81
|
}
|
94
82
|
|
83
|
+
# Executing callback
|
84
|
+
TheBigDB.before_request_execution.call(self)
|
85
|
+
|
86
|
+
# Here is where the request is actually executed
|
87
|
+
@http_response = @http.request(@http_request)
|
88
|
+
|
89
|
+
# Setting @response
|
90
|
+
begin
|
91
|
+
# We parse the JSON answer and return it.
|
92
|
+
@response = JSON(@http_response.body)
|
93
|
+
rescue JSON::ParserError => e
|
94
|
+
@response = {"status" => "error", "error" => {"code" => "0000", "description" => "The server gave an invalid JSON body:\n#{@http_response.body}"}}
|
95
|
+
end
|
96
|
+
|
97
|
+
# Setting @data_received
|
95
98
|
@data_received = {
|
96
99
|
"headers" => Hash[@http_response.to_hash.map{|k,v| [k, v.join] }],
|
97
100
|
"content" => @response
|
98
101
|
}
|
99
102
|
|
103
|
+
# Executing callback
|
100
104
|
TheBigDB.after_request_execution.call(self)
|
101
105
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
##############
|
106
|
-
## Engine Helpers
|
107
|
-
##############
|
108
|
-
|
109
|
-
# serialize_query_params({house: "bricks", animals: ["cat", "dog"], computers: {cool: true, drives: ["hard", "flash"]}})
|
110
|
-
# => house=bricks&animals%5B%5D=cat&animals%5B%5D=dog&computers%5Bcool%5D=true&computers%5Bdrives%5D%5B%5D=hard&computers%5Bdrives%5D%5B%5D=flash
|
111
|
-
# which will be read by the server as:
|
112
|
-
# => house=bricks&animals[]=cat&animals[]=dog&computers[cool]=true&computers[drives][]=hard&computers[drives][]=flash
|
113
|
-
def self.serialize_query_params(params, prefix = nil)
|
114
|
-
ret = []
|
115
|
-
params.each_pair do |key, value|
|
116
|
-
param_key = prefix ? "#{prefix}[#{key}]" : key
|
117
|
-
|
118
|
-
if value.is_a?(Hash)
|
119
|
-
ret << self.serialize_query_params(value, param_key.to_s)
|
120
|
-
elsif value.is_a?(Array)
|
121
|
-
sub_hash = {}
|
122
|
-
value.each_with_index do |value_item, i|
|
123
|
-
sub_hash[i.to_s] = value_item
|
124
|
-
end
|
125
|
-
ret << self.serialize_query_params(sub_hash, param_key.to_s)
|
126
|
-
else
|
127
|
-
ret << URI.encode_www_form_component(param_key.to_s) + "=" + URI.encode_www_form_component(value.to_s)
|
128
|
-
end
|
106
|
+
# Raising exception if asked
|
107
|
+
if TheBigDB.raise_on_api_status_error and @response["status"] == "error"
|
108
|
+
raise ApiStatusError.new(@response["error"]["code"])
|
129
109
|
end
|
130
|
-
ret.join("&")
|
131
|
-
end
|
132
110
|
|
133
|
-
|
134
|
-
# => {"house" => "bricks", "animals[0]" => "cat", "animals[1]" => "dog", "computers[cool]" => "true", "computers[drives][0]" => "hard", "computers[drives][1]" => "flash"}
|
135
|
-
def self.flatten_params_keys(params)
|
136
|
-
serialized_params = self.serialize_query_params(params)
|
137
|
-
new_params = {}
|
138
|
-
serialized_params.split("&").each do |assign|
|
139
|
-
key, value = assign.split("=")
|
140
|
-
new_params[URI.decode(key)] = URI.decode(value)
|
141
|
-
end
|
142
|
-
new_params
|
111
|
+
self
|
143
112
|
end
|
113
|
+
|
144
114
|
end
|
145
115
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
module TheBigDB
|
2
|
+
# Generic request to statements executor
|
2
3
|
def self.Statement(action, params)
|
3
4
|
method = ["get", "show", "search"].include?(action.to_s) ? "GET" : "POST"
|
4
5
|
path = "/statements/#{action}"
|
@@ -7,4 +8,57 @@ module TheBigDB
|
|
7
8
|
request.prepare(method, path, params)
|
8
9
|
request.execute
|
9
10
|
end
|
11
|
+
|
12
|
+
# Beautified builder
|
13
|
+
class StatementRequest
|
14
|
+
attr_reader :action, :params
|
15
|
+
|
16
|
+
def initialize(action)
|
17
|
+
@action = action
|
18
|
+
@params = {}
|
19
|
+
@response = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def with(params = {})
|
23
|
+
@response = nil
|
24
|
+
@params.merge!(TheBigDB::Helpers::stringify_keys(params))
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing(method_name, *arguments, &block)
|
29
|
+
@response ||= TheBigDB::Statement(@action, @params).response
|
30
|
+
@response.send(method_name, *arguments, &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
def execute
|
34
|
+
to_hash # goes through method_missing
|
35
|
+
end
|
36
|
+
alias_method :response, :execute
|
37
|
+
|
38
|
+
def execute! # forces the request to be re-executed
|
39
|
+
@response = nil
|
40
|
+
to_hash
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Shortcuts to actions
|
45
|
+
def self.search(*nodes)
|
46
|
+
StatementRequest.new("search").with(nodes: nodes)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.create(*nodes)
|
50
|
+
StatementRequest.new("create").with(nodes: nodes)
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.show(id = "")
|
54
|
+
StatementRequest.new("show").with(id: id)
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.upvote(id = "")
|
58
|
+
StatementRequest.new("upvote").with(id: id)
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.downvote(id = "")
|
62
|
+
StatementRequest.new("downvote").with(id: id)
|
63
|
+
end
|
10
64
|
end
|
data/lib/thebigdb/version.rb
CHANGED
data/lib/thebigdb.rb
CHANGED
@@ -7,9 +7,9 @@ require "rack"
|
|
7
7
|
aliases
|
8
8
|
request
|
9
9
|
version
|
10
|
+
helpers
|
10
11
|
resources/statement
|
11
12
|
resources/user
|
12
|
-
resources/toolbox
|
13
13
|
).each do |file_name|
|
14
14
|
require File.join(File.dirname(__FILE__), 'thebigdb', file_name)
|
15
15
|
end
|
@@ -23,7 +23,8 @@ module TheBigDB
|
|
23
23
|
"use_ssl" => false,
|
24
24
|
"verify_ssl_certificates" => false,
|
25
25
|
"before_request_execution" => Proc.new{},
|
26
|
-
"after_request_execution" => Proc.new{}
|
26
|
+
"after_request_execution" => Proc.new{},
|
27
|
+
"raise_on_api_status_error" => false
|
27
28
|
}
|
28
29
|
|
29
30
|
DEFAULT_CONFIGURATION.each_key do |configuration_key|
|
@@ -66,8 +67,11 @@ module TheBigDB
|
|
66
67
|
def self.with_configuration(new_configuration, &block)
|
67
68
|
current_configuration = Hash[DEFAULT_CONFIGURATION.keys.map{|k| [k, send(k)] }]
|
68
69
|
new_configuration.each_pair{|k,v| send(k.to_s + "=", v) }
|
69
|
-
|
70
|
-
|
70
|
+
begin
|
71
|
+
yield
|
72
|
+
ensure
|
73
|
+
current_configuration.each_pair{|k,v| send(k.to_s + "=", v) }
|
74
|
+
end
|
71
75
|
end
|
72
76
|
|
73
77
|
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Helpers" do
|
4
|
+
describe "serialize_query_params" do
|
5
|
+
it "works with simple a=b&c=d params" do
|
6
|
+
TheBigDB::Helpers::serialize_query_params(a: "b", c: "d").should == "a=b&c=d"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "works with more complex imbricated params" do
|
10
|
+
TheBigDB::Helpers::serialize_query_params({
|
11
|
+
house: "bricks",
|
12
|
+
animals: ["cat", "dog"],
|
13
|
+
computers: {cool: true, drives: ["hard", "flash"]}
|
14
|
+
}).should == "house=bricks&animals%5B0%5D=cat&animals%5B1%5D=dog&computers%5Bcool%5D=true&computers%5Bdrives%5D%5B0%5D=hard&computers%5Bdrives%5D%5B1%5D=flash"
|
15
|
+
|
16
|
+
# and with a hash instead of an array
|
17
|
+
TheBigDB::Helpers::serialize_query_params({
|
18
|
+
house: "bricks",
|
19
|
+
animals: {"0" => "cat", "1" => "dog"},
|
20
|
+
computers: {cool: true, drives: ["hard", "flash"]}
|
21
|
+
}).should == "house=bricks&animals%5B0%5D=cat&animals%5B1%5D=dog&computers%5Bcool%5D=true&computers%5Bdrives%5D%5B0%5D=hard&computers%5Bdrives%5D%5B1%5D=flash"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "flatten_params_keys" do
|
26
|
+
it "works with simple a=b&c=d params" do
|
27
|
+
TheBigDB::Helpers::flatten_params_keys(a: "b", c: "d").should == {"a" => "b", "c" => "d"}
|
28
|
+
end
|
29
|
+
|
30
|
+
it "works with more complex imbricated params" do
|
31
|
+
TheBigDB::Helpers::flatten_params_keys({
|
32
|
+
house: "bricks",
|
33
|
+
animals: ["cat", "dog"],
|
34
|
+
computers: {cool: true, drives: ["hard", "flash"]}
|
35
|
+
}).should == {
|
36
|
+
"house" => "bricks",
|
37
|
+
"animals[0]" => "cat",
|
38
|
+
"animals[1]" => "dog",
|
39
|
+
"computers[cool]" => "true",
|
40
|
+
"computers[drives][0]" => "hard",
|
41
|
+
"computers[drives][1]" => "flash"
|
42
|
+
}
|
43
|
+
|
44
|
+
# and with a hash instead of an array
|
45
|
+
TheBigDB::Helpers::flatten_params_keys({
|
46
|
+
house: "bricks",
|
47
|
+
animals: {"0" => "cat", "1" => "dog"},
|
48
|
+
computers: {cool: true, drives: ["hard", "flash"]}
|
49
|
+
}).should == {
|
50
|
+
"house" => "bricks",
|
51
|
+
"animals[0]" => "cat",
|
52
|
+
"animals[1]" => "dog",
|
53
|
+
"computers[cool]" => "true",
|
54
|
+
"computers[drives][0]" => "hard",
|
55
|
+
"computers[drives][1]" => "flash"
|
56
|
+
}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "stringify_keys" do
|
61
|
+
it "works with simple mixed hash" do
|
62
|
+
TheBigDB::Helpers::stringify_keys({
|
63
|
+
:a => "foo",
|
64
|
+
"b" => "bar",
|
65
|
+
"a" => "foo2"
|
66
|
+
}).should == {
|
67
|
+
"a" => "foo2",
|
68
|
+
"b" => "bar"
|
69
|
+
}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/spec/request_spec.rb
CHANGED
@@ -82,6 +82,15 @@ describe "executing" do
|
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
|
+
context "failing requests" do
|
86
|
+
it "can raise an exception" do
|
87
|
+
stub_request(:get, /#{TheBigDB.api_host}/).to_return(:body => "invalid json")
|
88
|
+
|
89
|
+
TheBigDB.raise_on_api_status_error = true
|
90
|
+
lambda { TheBigDB.send_request(:get, "/") }.should raise_error(TheBigDB::Request::ApiStatusError, "0000")
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
85
94
|
context "GET requests" do
|
86
95
|
context "with basic params" do
|
87
96
|
before do
|
@@ -253,63 +262,4 @@ describe "executing" do
|
|
253
262
|
end
|
254
263
|
end
|
255
264
|
end
|
256
|
-
end
|
257
|
-
|
258
|
-
describe "Request helper" do
|
259
|
-
describe "serialize_query_params" do
|
260
|
-
it "works with simple a=b&c=d params" do
|
261
|
-
TheBigDB::Request.serialize_query_params(a: "b", c: "d").should == "a=b&c=d"
|
262
|
-
end
|
263
|
-
|
264
|
-
it "works with more complex imbricated params" do
|
265
|
-
TheBigDB::Request.serialize_query_params({
|
266
|
-
house: "bricks",
|
267
|
-
animals: ["cat", "dog"],
|
268
|
-
computers: {cool: true, drives: ["hard", "flash"]}
|
269
|
-
}).should == "house=bricks&animals%5B0%5D=cat&animals%5B1%5D=dog&computers%5Bcool%5D=true&computers%5Bdrives%5D%5B0%5D=hard&computers%5Bdrives%5D%5B1%5D=flash"
|
270
|
-
|
271
|
-
# and with a hash instead of an array
|
272
|
-
TheBigDB::Request.serialize_query_params({
|
273
|
-
house: "bricks",
|
274
|
-
animals: {"0" => "cat", "1" => "dog"},
|
275
|
-
computers: {cool: true, drives: ["hard", "flash"]}
|
276
|
-
}).should == "house=bricks&animals%5B0%5D=cat&animals%5B1%5D=dog&computers%5Bcool%5D=true&computers%5Bdrives%5D%5B0%5D=hard&computers%5Bdrives%5D%5B1%5D=flash"
|
277
|
-
end
|
278
|
-
end
|
279
|
-
|
280
|
-
describe "flatten_params_keys" do
|
281
|
-
it "works with simple a=b&c=d params" do
|
282
|
-
TheBigDB::Request.flatten_params_keys(a: "b", c: "d").should == {"a" => "b", "c" => "d"}
|
283
|
-
end
|
284
|
-
|
285
|
-
it "works with more complex imbricated params" do
|
286
|
-
TheBigDB::Request.flatten_params_keys({
|
287
|
-
house: "bricks",
|
288
|
-
animals: ["cat", "dog"],
|
289
|
-
computers: {cool: true, drives: ["hard", "flash"]}
|
290
|
-
}).should == {
|
291
|
-
"house" => "bricks",
|
292
|
-
"animals[0]" => "cat",
|
293
|
-
"animals[1]" => "dog",
|
294
|
-
"computers[cool]" => "true",
|
295
|
-
"computers[drives][0]" => "hard",
|
296
|
-
"computers[drives][1]" => "flash"
|
297
|
-
}
|
298
|
-
|
299
|
-
# and with a hash instead of an array
|
300
|
-
TheBigDB::Request.flatten_params_keys({
|
301
|
-
house: "bricks",
|
302
|
-
animals: {"0" => "cat", "1" => "dog"},
|
303
|
-
computers: {cool: true, drives: ["hard", "flash"]}
|
304
|
-
}).should == {
|
305
|
-
"house" => "bricks",
|
306
|
-
"animals[0]" => "cat",
|
307
|
-
"animals[1]" => "dog",
|
308
|
-
"computers[cool]" => "true",
|
309
|
-
"computers[drives][0]" => "hard",
|
310
|
-
"computers[drives][1]" => "flash"
|
311
|
-
}
|
312
|
-
end
|
313
|
-
end
|
314
|
-
end
|
315
|
-
|
265
|
+
end
|
@@ -3,27 +3,77 @@ require "spec_helper"
|
|
3
3
|
describe "Statement" do
|
4
4
|
context "basic request executing" do
|
5
5
|
before do
|
6
|
-
stub_request(:get,
|
6
|
+
stub_request(:get, @request_path.call("search")).to_return(:body => '{"server_says": "hello world"}')
|
7
7
|
|
8
8
|
@request = TheBigDB::Statement(:search, nodes: ["a", "b"])
|
9
9
|
end
|
10
10
|
|
11
11
|
it "sets the correct data_sent instance variable" do
|
12
12
|
@request.data_sent.should == {
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
13
|
+
"headers" => Hash[@request.http_request.to_hash.map{|k,v| [k, v.join] }],
|
14
|
+
"host" => TheBigDB.api_host,
|
15
|
+
"port" => TheBigDB.api_port,
|
16
|
+
"path" => "/v#{TheBigDB.api_version}/statements/search",
|
17
|
+
"method" => "GET",
|
18
|
+
"params" => {"nodes" => {"0" => "a", "1" => "b"}}
|
19
|
+
}
|
20
20
|
end
|
21
21
|
|
22
22
|
it "sets the correct data_received instance variable" do
|
23
23
|
@request.data_received.should include({
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
"headers" => Hash[@request.http_response.to_hash.map{|k,v| [k, v.join] }],
|
25
|
+
"content" => {"server_says" => "hello world"}
|
26
|
+
})
|
27
27
|
end
|
28
28
|
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "StatementRequest" do
|
32
|
+
before do
|
33
|
+
end
|
34
|
+
|
35
|
+
it "makes normal requests" do
|
36
|
+
@search = TheBigDB.search("a", "b", {match: "blue"})
|
37
|
+
@search.with(page: 2)
|
38
|
+
@search.params.should == {"nodes" => ["a", "b", {match: "blue"}], "page" => 2}
|
39
|
+
end
|
40
|
+
|
41
|
+
it "cache the response unless the params are modified, or asked to" do
|
42
|
+
stub_request(:get, @request_path.call("search")).to_return(:body => '{status: "success", statements: []}')
|
43
|
+
|
44
|
+
response = TheBigDB.search("a", "b", {match: "blue"}).with(page: 2)
|
45
|
+
response.execute
|
46
|
+
response.execute
|
47
|
+
|
48
|
+
response = TheBigDB.search("a", "b", {match: "red"}).with(page: 2)
|
49
|
+
response.execute
|
50
|
+
response.execute!
|
51
|
+
|
52
|
+
a_request(:get, @request_path.call("search"))
|
53
|
+
.with(query: hash_including({"nodes" => ["a", "b", {match: "blue"}], "page" => "2"})).should have_been_made.once
|
54
|
+
|
55
|
+
a_request(:get, @request_path.call("search"))
|
56
|
+
.with(query: hash_including({"nodes" => ["a", "b", {match: "red"}], "page" => "2"})).should have_been_made.times(2)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "has standard actions correctly binded" do
|
60
|
+
stub_request(:get, @request_path.call("search")).to_return(:body => '{status: "success", statements: []}')
|
61
|
+
stub_request(:get, @request_path.call("show")).to_return(:body => '{status: "success"}')
|
62
|
+
stub_request(:post, @request_path.call("create")).to_return(:body => '{status: "success"}')
|
63
|
+
stub_request(:post, @request_path.call("upvote")).to_return(:body => '{status: "success"}')
|
64
|
+
stub_request(:post, @request_path.call("downvote")).to_return(:body => '{status: "success"}')
|
65
|
+
|
66
|
+
TheBigDB.search("a", "b", {match: "blue"}).execute
|
67
|
+
TheBigDB.show("foobar").execute
|
68
|
+
TheBigDB.create("foobar").execute
|
69
|
+
TheBigDB.upvote("foobar").execute
|
70
|
+
TheBigDB.downvote("foobar").execute
|
71
|
+
|
72
|
+
a_request(:get, @request_path.call("search")).should have_been_made.once
|
73
|
+
a_request(:get, @request_path.call("show")).should have_been_made.once
|
74
|
+
a_request(:post, @request_path.call("create")).should have_been_made.once
|
75
|
+
a_request(:post, @request_path.call("upvote")).should have_been_made.once
|
76
|
+
a_request(:post, @request_path.call("downvote")).should have_been_made.once
|
77
|
+
end
|
78
|
+
|
29
79
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,6 +8,8 @@ RSpec.configure do |config|
|
|
8
8
|
config.before(:each) do
|
9
9
|
TheBigDB::DEFAULT_CONFIGURATION["api_host"] = "fake.test.host"
|
10
10
|
TheBigDB.reset_default_configuration
|
11
|
+
|
12
|
+
@request_path = ->(action){ /#{TheBigDB.api_host}\/v#{TheBigDB.api_version}\/statements\/#{action}/ }
|
11
13
|
end
|
12
14
|
|
13
15
|
config.after(:each) do
|
data/thebigdb.gemspec
CHANGED
@@ -4,7 +4,6 @@ require "thebigdb/version"
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "thebigdb"
|
6
6
|
s.version = TheBigDB::VERSION::STRING
|
7
|
-
s.date = "2013-03-12"
|
8
7
|
s.summary = "Ruby bindings for TheBigDB API"
|
9
8
|
s.description = "TheBigDB a simply structured open database of real life facts. See http://thebigdb.com for details."
|
10
9
|
s.authors = ["Christophe Maximin"]
|
@@ -15,6 +14,7 @@ Gem::Specification.new do |s|
|
|
15
14
|
|
16
15
|
s.add_runtime_dependency "rack", "~> 1.4"
|
17
16
|
|
17
|
+
s.add_development_dependency "rake", "~> 10.0.3"
|
18
18
|
s.add_development_dependency "rspec", "~> 2.12"
|
19
19
|
s.add_development_dependency "guard-rspec", "~> 2.3"
|
20
20
|
s.add_development_dependency "rb-inotify"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thebigdb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-04-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '1.4'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 10.0.3
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 10.0.3
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: rspec
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -132,6 +148,7 @@ extra_rdoc_files: []
|
|
132
148
|
files:
|
133
149
|
- .gitignore
|
134
150
|
- .rspec
|
151
|
+
- .travis.yml
|
135
152
|
- Gemfile
|
136
153
|
- Gemfile.lock
|
137
154
|
- Guardfile
|
@@ -140,15 +157,15 @@ files:
|
|
140
157
|
- Rakefile
|
141
158
|
- lib/thebigdb.rb
|
142
159
|
- lib/thebigdb/aliases.rb
|
160
|
+
- lib/thebigdb/helpers.rb
|
143
161
|
- lib/thebigdb/module_attribute_accessors.rb
|
144
162
|
- lib/thebigdb/request.rb
|
145
163
|
- lib/thebigdb/resources/statement.rb
|
146
|
-
- lib/thebigdb/resources/toolbox.rb
|
147
164
|
- lib/thebigdb/resources/user.rb
|
148
165
|
- lib/thebigdb/version.rb
|
166
|
+
- spec/helpers_spec.rb
|
149
167
|
- spec/request_spec.rb
|
150
168
|
- spec/resources/statement_spec.rb
|
151
|
-
- spec/resources/toolbox_spec.rb
|
152
169
|
- spec/resources/user_spec.rb
|
153
170
|
- spec/spec_helper.rb
|
154
171
|
- spec/thebigdb_spec.rb
|
@@ -178,9 +195,9 @@ signing_key:
|
|
178
195
|
specification_version: 3
|
179
196
|
summary: Ruby bindings for TheBigDB API
|
180
197
|
test_files:
|
198
|
+
- spec/helpers_spec.rb
|
181
199
|
- spec/request_spec.rb
|
182
200
|
- spec/resources/statement_spec.rb
|
183
|
-
- spec/resources/toolbox_spec.rb
|
184
201
|
- spec/resources/user_spec.rb
|
185
202
|
- spec/spec_helper.rb
|
186
203
|
- spec/thebigdb_spec.rb
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe "Toolbox Units" do
|
4
|
-
context "basic request executing" do
|
5
|
-
before do
|
6
|
-
stub_request(:get, /#{TheBigDB.api_host}\/v#{TheBigDB.api_version}\/toolbox\/units\/convert/).to_return(:body => '{"server_says": "hello world"}')
|
7
|
-
|
8
|
-
@request = TheBigDB::Toolbox::Unit(:convert, :value => "100 ly", :new_unit => "cm")
|
9
|
-
end
|
10
|
-
|
11
|
-
it "sets the correct data_sent instance variable" do
|
12
|
-
@request.data_sent.should == {
|
13
|
-
"headers" => Hash[@request.http_request.to_hash.map{|k,v| [k, v.join] }],
|
14
|
-
"host" => TheBigDB.api_host,
|
15
|
-
"port" => TheBigDB.api_port,
|
16
|
-
"path" => "/v#{TheBigDB.api_version}/toolbox/units/convert",
|
17
|
-
"method" => "GET",
|
18
|
-
"params" => {"value" => "100 ly", "new_unit" => "cm"}
|
19
|
-
}
|
20
|
-
end
|
21
|
-
|
22
|
-
it "sets the correct data_received instance variable" do
|
23
|
-
@request.data_received.should include({
|
24
|
-
"headers" => Hash[@request.http_response.to_hash.map{|k,v| [k, v.join] }],
|
25
|
-
"content" => {"server_says" => "hello world"}
|
26
|
-
})
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|