sputnik 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sputnik.rb CHANGED
@@ -5,13 +5,15 @@ require 'faraday'
5
5
  require 'sputnik/connection'
6
6
 
7
7
  module Sputnik
8
+ ConnectionNotAuthenticatedError = Class.new(RuntimeError)
9
+
8
10
  class << self
9
11
  def authenticate(options={})
10
12
  Sputnik.client = Sputnik::Connection.new(options)
11
13
  end
12
14
 
13
15
  def client
14
- Thread.current[:sputnik_client]
16
+ Thread.current[:sputnik_client] || (raise ConnectionNotAuthenticatedError, 'You must first call the authenticate method to connect to MongoHQ.')
15
17
  end
16
18
 
17
19
  def client=(new_client)
@@ -5,8 +5,9 @@ module Sputnik
5
5
  response = client.get("/databases/#{database_name}/collections")
6
6
  values = []
7
7
  response.each do |item|
8
- values << Collection.new(item)
8
+ values << Collection.new({col: item})
9
9
  end
10
+ values
10
11
  end
11
12
 
12
13
  def find(database_name, collection_name)
@@ -24,8 +25,37 @@ module Sputnik
24
25
  # TODO: how about a "save" option? I could add it here, but it would be two server hits
25
26
 
26
27
  def delete(database_name, collection_name)
27
- client.delete("/databases/#{database_name}/collections/#{collection_name}", params)
28
+ client.delete("/databases/#{database_name}/collections/#{collection_name}")
28
29
  end
29
30
  end
31
+
32
+ def all
33
+ Collection.all(database.db)
34
+ end
35
+
36
+ def find(collection_name=nil)
37
+ Collection.find(database.db, collection_name || self.name)
38
+ end
39
+
40
+ def create(params=nil)
41
+ Collection.create(database.db, params || self.to_hash)
42
+ end
43
+
44
+ def update(collection_name=nil, params=nil)
45
+ Collection.update(database.db, collection_name || self.name, params || self.to_hash)
46
+ end
47
+
48
+ def delete(collection_name=nil)
49
+ Collection.delete(database.db, collection_name || self.name)
50
+ end
51
+
52
+
53
+ def documents
54
+ Document.new(:database => self.database, :collection => self)
55
+ end
56
+
57
+ def indexes
58
+ Index.new(:database => self.database, :collection => self)
59
+ end
30
60
  end
31
61
  end
@@ -25,6 +25,7 @@ module Sputnik
25
25
  params = params.merge({:_apikey => apikey})
26
26
  response = connect.get do |req|
27
27
  req.url(path)
28
+ req.headers['User-Agent'] = default_header
28
29
  req.params = params
29
30
  end
30
31
 
@@ -38,7 +39,8 @@ module Sputnik
38
39
  response = connect.post do |req|
39
40
  req.url(path)
40
41
  req.params = params
41
- req.header = {'Content-Type' => 'application/json'}
42
+ req.headers['Content-Type'] = 'application/json'
43
+ req.headers['User-Agent'] = default_header
42
44
  end
43
45
 
44
46
  verify_status!(response)
@@ -46,12 +48,16 @@ module Sputnik
46
48
  return JSON.parse(response.body) || {}
47
49
  end
48
50
 
51
+ def default_header
52
+ "Sputnik/#{VERSION}/ruby"
53
+ end
54
+
49
55
  private
50
56
 
51
57
  def verify_status!(response)
52
58
  case response.status
53
59
  when 500
54
- raise InternalServerError
60
+ raise InternalServerError, (JSON.parse(response.body) || {})['error']
55
61
  when 501
56
62
  raise NotImplementedError
57
63
  when 403
@@ -1,12 +1,7 @@
1
+ # TODO: use command pattern to build an enumerable
2
+ # command that's not called until it's used
1
3
  module Sputnik
2
4
  class Database < Base
3
- def collection
4
- raise "Not Yet Implemented"
5
- # TODO: call this database's collection
6
- # for example, to get a database's collections:
7
- # Sputnik::Database.new(:name => 'derp').collection.all
8
- end
9
-
10
5
  class << self
11
6
  def all
12
7
  response = client.get('/databases')
@@ -14,6 +9,7 @@ module Sputnik
14
9
  response.each do |item|
15
10
  values << Database.new(item)
16
11
  end
12
+ values
17
13
  end
18
14
 
19
15
  def find(database_name)
@@ -28,5 +24,14 @@ module Sputnik
28
24
  client.delete("/databases/#{database_name}", params)
29
25
  end
30
26
  end
27
+
28
+ # Sputnik::Database.new(:name => 'derp').collection.all
29
+ def collection
30
+ Collection.new(:database => self)
31
+ end
32
+
33
+ def stats
34
+ DatabaseStats.find(db)
35
+ end
31
36
  end
32
37
  end
@@ -1,13 +1,13 @@
1
- # TODO: this has room to be considerably cooler...
2
1
  module Sputnik
3
2
  class Document < Base
4
3
  class << self
5
- def all(database_name, collection_name)
6
- response = client.get("/databases/#{database_name}/collections/#{collection_name}/documents")
4
+ def all(database_name, collection_name, params={})
5
+ response = client.get("/databases/#{database_name}/collections/#{collection_name}/documents", params)
7
6
  values = []
8
7
  response.each do |item|
9
8
  values << Document.new(item)
10
9
  end
10
+ values
11
11
  end
12
12
 
13
13
  def find(database_name, collection_name, document_id)
@@ -25,8 +25,28 @@ module Sputnik
25
25
  # TODO: how about a "save" option? I could add it here, but it would be two server hits
26
26
 
27
27
  def delete(database_name, collection_name, document_id)
28
- client.delete("/databases/#{database_name}/collections/#{collection_name}/documents/#{document_id}", params)
28
+ client.delete("/databases/#{database_name}/collections/#{collection_name}/documents/#{document_id}")
29
29
  end
30
30
  end
31
+
32
+ def all(params=nil)
33
+ Document.all(collection.database.db, collection.name, params || self.to_hash)
34
+ end
35
+
36
+ def find(id=nil, params=nil)
37
+ Document.find(collection.database.db, collection.name, id || self._id, params || self.to_hash)
38
+ end
39
+
40
+ def create(params=nil)
41
+ Document.create(collection.database.db, collection.name, params || self.to_hash)
42
+ end
43
+
44
+ def update(id=nil, params=nil)
45
+ Document.update(collection.database.db, collection.name, id || self._id, params || self.to_hash)
46
+ end
47
+
48
+ def delete(id=nil)
49
+ Document.delete(collection.database.db, collection.name, id || self._id)
50
+ end
31
51
  end
32
52
  end
data/lib/sputnik/index.rb CHANGED
@@ -7,6 +7,7 @@ module Sputnik
7
7
  response.each do |item|
8
8
  values << Index.new(item)
9
9
  end
10
+ values
10
11
  end
11
12
 
12
13
  def find(database_name, collection_name, index_name)
@@ -18,8 +19,24 @@ module Sputnik
18
19
  end
19
20
 
20
21
  def delete(database_name, collection_name, index_name)
21
- client.delete("/databases/#{database_name}/collections/#{collection_name}/indexes/#{index_name}", params)
22
+ client.delete("/databases/#{database_name}/collections/#{collection_name}/indexes/#{index_name}")
22
23
  end
23
24
  end
25
+
26
+ def all(params=nil)
27
+ Index.all(collection.database.db, collection.name, params || self.to_hash)
28
+ end
29
+
30
+ def find(name=nil, params=nil)
31
+ Index.find(collection.database.db, collection.name, name || self.name, params || self.to_hash)
32
+ end
33
+
34
+ def create(params=nil)
35
+ Index.create(collection.database.db, collection.name, params || self.to_hash)
36
+ end
37
+
38
+ def delete(name=nil)
39
+ Index.delete(collection.database.db, collection.name, name || self.name)
40
+ end
24
41
  end
25
42
  end
@@ -1,3 +1,3 @@
1
1
  module Sputnik
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sputnik
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-03 00:00:00.000000000Z
12
+ date: 2011-11-13 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70259773429420 !ruby/object:Gem::Requirement
16
+ requirement: &70124238869940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70259773429420
24
+ version_requirements: *70124238869940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: faraday
27
- requirement: &70259773429000 !ruby/object:Gem::Requirement
27
+ requirement: &70124238869520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70259773429000
35
+ version_requirements: *70124238869520
36
36
  description: Connects to MongoHQ API
37
37
  email:
38
38
  - eric.redmond@gmail.com