thebigdb 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- thebigdb (1.2.0)
4
+ thebigdb (1.2.1)
5
5
  rack (~> 1.4)
6
6
 
7
7
  GEM
@@ -22,6 +22,10 @@ module TheBigDB
22
22
  def prepare(method, request_uri, params = {})
23
23
  method = method.downcase.to_s
24
24
 
25
+ if TheBigDB.api_key.is_a?(String) and !TheBigDB.api_key.empty?
26
+ params.merge!("api_key" => TheBigDB.api_key)
27
+ end
28
+
25
29
  # we add the API version to the URL, with a trailing slash and the rest of the request
26
30
  request_uri = "/v#{TheBigDB.api_version}" + (request_uri.start_with?("/") ? request_uri : "/#{request_uri}")
27
31
 
@@ -38,10 +42,10 @@ module TheBigDB
38
42
  @http_request["user-agent"] = "TheBigDB RubyWrapper/#{TheBigDB::VERSION::STRING}"
39
43
 
40
44
  client_user_agent = {
41
- :publisher => "thebigdb",
42
- :version => TheBigDB::VERSION::STRING,
43
- :language => "ruby",
44
- :language_version => "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
45
+ "publisher" => "thebigdb",
46
+ "version" => TheBigDB::VERSION::STRING,
47
+ "language" => "ruby",
48
+ "language_version" => "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
45
49
  }
46
50
 
47
51
  @http_request["X-TheBigDB-Client-User-Agent"] = JSON(client_user_agent)
@@ -43,22 +43,22 @@ module TheBigDB
43
43
 
44
44
  # Shortcuts to actions
45
45
  def self.search(*nodes)
46
- StatementRequest.new("search").with(nodes: nodes)
46
+ StatementRequest.new("search").with("nodes" => nodes)
47
47
  end
48
48
 
49
49
  def self.create(*nodes)
50
- StatementRequest.new("create").with(nodes: nodes)
50
+ StatementRequest.new("create").with("nodes" => nodes)
51
51
  end
52
52
 
53
53
  def self.show(id = "")
54
- StatementRequest.new("show").with(id: id)
54
+ StatementRequest.new("show").with("id" => id)
55
55
  end
56
56
 
57
57
  def self.upvote(id = "")
58
- StatementRequest.new("upvote").with(id: id)
58
+ StatementRequest.new("upvote").with("id" => id)
59
59
  end
60
60
 
61
61
  def self.downvote(id = "")
62
- StatementRequest.new("downvote").with(id: id)
62
+ StatementRequest.new("downvote").with("id" => id)
63
63
  end
64
64
  end
@@ -2,7 +2,7 @@ module TheBigDB
2
2
  module VERSION
3
3
  MAJOR = 1
4
4
  MINOR = 2
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].compact.join('.')
8
8
  end
data/lib/thebigdb.rb CHANGED
@@ -17,6 +17,7 @@ end
17
17
  module TheBigDB
18
18
 
19
19
  DEFAULT_CONFIGURATION = {
20
+ "api_key" => nil,
20
21
  "api_host" => "api.thebigdb.com",
21
22
  "api_port" => 80,
22
23
  "api_version" => "1",
data/spec/request_spec.rb CHANGED
@@ -45,7 +45,6 @@ describe "preparing" do
45
45
  end
46
46
 
47
47
  describe "executing" do
48
-
49
48
  context "requests" do
50
49
  before do
51
50
  stub_request(:get, /#{TheBigDB.api_host}/).to_return(:body => '{"server_says": "hello world"}')
@@ -120,6 +119,35 @@ describe "executing" do
120
119
  end
121
120
  end
122
121
 
122
+ context "with basic params and API key" do
123
+ before do
124
+ TheBigDB.api_key = "user-api-key"
125
+ stub_request(:get, /#{TheBigDB.api_host}/).to_return(:body => '{"server_says": "hello world"}')
126
+
127
+ @request = TheBigDB::Request.new
128
+ @request.prepare(:get, "/", :foo => "bar")
129
+ @request.execute
130
+ end
131
+
132
+ it "sets the correct data_sent instance variable" do
133
+ @request.data_sent.should == {
134
+ "headers" => Hash[@request.http_request.to_hash.map{|k,v| [k, v.join] }],
135
+ "host" => TheBigDB.api_host,
136
+ "port" => TheBigDB.api_port,
137
+ "path" => "/v#{TheBigDB.api_version}/",
138
+ "method" => "GET",
139
+ "params" => {"foo" => "bar", "api_key" => "user-api-key"}
140
+ }
141
+ end
142
+
143
+ it "sets the correct data_received instance variable" do
144
+ @request.data_received.should include({
145
+ "headers" => Hash[@request.http_response.to_hash.map{|k,v| [k, v.join] }],
146
+ "content" => {"server_says" => "hello world"}
147
+ })
148
+ end
149
+ end
150
+
123
151
  context "with arrays in params" do
124
152
  before do
125
153
  stub_request(:get, /#{TheBigDB.api_host}/).to_return(:body => '{"server_says": "hello world"}')
@@ -206,6 +234,35 @@ describe "executing" do
206
234
  end
207
235
  end
208
236
 
237
+ context "with basic params and API key" do
238
+ before do
239
+ TheBigDB.api_key = "user-api-key"
240
+ stub_request(:post, /#{TheBigDB.api_host}/).to_return(:body => '{"server_says": "hello world"}')
241
+
242
+ @request = TheBigDB::Request.new
243
+ @request.prepare(:post, "/", :foo => "bar")
244
+ @request.execute
245
+ end
246
+
247
+ it "sets the correct data_sent instance variable" do
248
+ @request.data_sent.should == {
249
+ "headers" => Hash[@request.http_request.to_hash.map{|k,v| [k, v.join] }],
250
+ "host" => TheBigDB.api_host,
251
+ "port" => TheBigDB.api_port,
252
+ "path" => "/v#{TheBigDB.api_version}/",
253
+ "method" => "POST",
254
+ "params" => {"foo" => "bar", "api_key" => "user-api-key"}
255
+ }
256
+ end
257
+
258
+ it "sets the correct data_received instance variable" do
259
+ @request.data_received.should include({
260
+ "headers" => Hash[@request.http_response.to_hash.map{|k,v| [k, v.join] }],
261
+ "content" => {"server_says" => "hello world"}
262
+ })
263
+ end
264
+ end
265
+
209
266
  context "with array arguments" do
210
267
  before do
211
268
  stub_request(:post, /#{TheBigDB.api_host}/).to_return(:body => '{"server_says": "hello world"}')
@@ -8,10 +8,22 @@ describe "TheBigDB module" do
8
8
  TheBigDB.api_host = "foobar"
9
9
  TheBigDB.api_port = 1337
10
10
  TheBigDB.api_version = "1337"
11
+ TheBigDB.api_key = "your-private-api-key"
12
+ TheBigDB.raise_on_api_status_error = true
13
+ TheBigDB.before_request_execution = (before_proc = ->(request){ nil })
14
+ TheBigDB.after_request_execution = (after_proc = ->(request){ nil })
11
15
 
12
16
  TheBigDB.api_host.should == "foobar"
13
17
  TheBigDB.api_port.should == 1337
14
18
  TheBigDB.api_version.should == "1337"
19
+ TheBigDB.api_key.should == "your-private-api-key"
20
+ TheBigDB.raise_on_api_status_error.should == true
21
+ TheBigDB.before_request_execution.should == before_proc
22
+ TheBigDB.after_request_execution.should == after_proc
23
+
24
+ # use_ssl modifies the port
25
+ TheBigDB.use_ssl = true
26
+ TheBigDB.use_ssl.should == true
15
27
  end
16
28
 
17
29
  it "has resettable default values" do
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.2.0
4
+ version: 1.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: