sputnik 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rbenv-vars
6
+ .DS_Store
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.2@sputnik
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sputnik.gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,12 @@
1
+ # Простейший Спутник-1
2
+
3
+ ![Спутник](http://www.uwgb.edu/dutchs/Graphics-Geol/Spacecraft/sputnik1.gif)
4
+
5
+ ## Using
6
+
7
+ Sputnik.authenticate(:apikey => 'derp', :base_url => 'http://localhost:9000')
8
+
9
+ databases = Sputnik::Database.all
10
+ database = Sputnik::Database.find('joemo')
11
+ Sputnik::Database.create({'my new derp'})
12
+ Sputnik::Database.delete('joemo')
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/sputnik.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'sputnik/version'
2
+ require 'ostruct'
3
+ require 'json'
4
+ require 'faraday'
5
+ require 'sputnik/connection'
6
+
7
+ module Sputnik
8
+ class << self
9
+ def authenticate(options={})
10
+ Sputnik.client = Sputnik::Connection.new(options)
11
+ end
12
+
13
+ def client
14
+ Thread.current[:sputnik_client]
15
+ end
16
+
17
+ def client=(new_client)
18
+ Thread.current[:sputnik_client] = new_client
19
+ end
20
+ end
21
+ end
22
+
23
+ require 'sputnik/base'
24
+ require 'sputnik/plan'
25
+ require 'sputnik/database'
26
+ require 'sputnik/database_stats'
27
+ require 'sputnik/collection'
28
+ require 'sputnik/document'
29
+ require 'sputnik/index'
@@ -0,0 +1,7 @@
1
+ module Sputnik
2
+ class Base < OpenStruct
3
+ def self.client
4
+ Sputnik.client
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ module Sputnik
2
+ class Collection < Base
3
+ class << self
4
+ def all(database_name)
5
+ response = client.get("/databases/#{database_name}/collections")
6
+ values = []
7
+ response.each do |item|
8
+ values << Collection.new(item)
9
+ end
10
+ end
11
+
12
+ def find(database_name, collection_name)
13
+ Collection.new(client.get("/databases/#{database_name}/collections/#{collection_name}"))
14
+ end
15
+
16
+ def create(database_name, params)
17
+ Collection.new(client.post("/databases/#{database_name}/collections", params))
18
+ end
19
+
20
+ def update(database_name, collection_name, params)
21
+ Collection.new(client.put("/databases/#{database_name}/collections/#{collection_name}", params))
22
+ end
23
+
24
+ # TODO: how about a "save" option? I could add it here, but it would be two server hits
25
+
26
+ def delete(database_name, collection_name)
27
+ client.delete("/databases/#{database_name}/collections/#{collection_name}", params)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,66 @@
1
+ module Sputnik
2
+ DEFAULT_HOST = 'https://api.mongohq.com'
3
+
4
+ %w{MissingApikeyError InvalidApikeyError InternalServerError NotImplementedError NotFoundError}.each{|const|
5
+ Kernel.const_set const, Class.new(RuntimeError)
6
+ }
7
+
8
+ class Connection
9
+ attr_reader :apikey, :base_url
10
+
11
+ def initialize(options={})
12
+ @apikey = options[:apikey]
13
+ @base_url = options[:base_url] || DEFAULT_HOST
14
+ end
15
+
16
+ def connect
17
+ conn = Faraday.new(:url => base_url) do |builder|
18
+ builder.request :json
19
+ builder.adapter :net_http
20
+ end
21
+ conn
22
+ end
23
+
24
+ def get(path, params={})
25
+ params = params.merge({:_apikey => apikey})
26
+ response = connect.get do |req|
27
+ req.url(path)
28
+ req.params = params
29
+ end
30
+
31
+ verify_status!(response)
32
+
33
+ return JSON.parse(response.body) || {}
34
+ end
35
+
36
+ def post(path, params={})
37
+ params = params.merge({:_apikey => apikey})
38
+ response = connect.post do |req|
39
+ req.url(path)
40
+ req.params = params
41
+ req.header = {'Content-Type' => 'application/json'}
42
+ end
43
+
44
+ verify_status!(response)
45
+
46
+ return JSON.parse(response.body) || {}
47
+ end
48
+
49
+ private
50
+
51
+ def verify_status!(response)
52
+ case response.status
53
+ when 500
54
+ raise InternalServerError
55
+ when 501
56
+ raise NotImplementedError
57
+ when 403
58
+ raise NotFoundError
59
+ when 404
60
+ raise NotFoundError
61
+ else
62
+ end
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,32 @@
1
+ module Sputnik
2
+ 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
+ class << self
11
+ def all
12
+ response = client.get('/databases')
13
+ values = []
14
+ response.each do |item|
15
+ values << Database.new(item)
16
+ end
17
+ end
18
+
19
+ def find(database_name)
20
+ Database.new(client.get("/databases/#{database_name}"))
21
+ end
22
+
23
+ def create(params)
24
+ Database.new(client.post('/databases', params))
25
+ end
26
+
27
+ def delete(database_name)
28
+ client.delete("/databases/#{database_name}", params)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ module Sputnik
2
+ class DatabaseStats < Base
3
+ class << self
4
+ def find(database_name)
5
+ DatabaseStats.new(client.get("/databases/#{database_name}/stats"))
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,32 @@
1
+ # TODO: this has room to be considerably cooler...
2
+ module Sputnik
3
+ class Document < Base
4
+ class << self
5
+ def all(database_name, collection_name)
6
+ response = client.get("/databases/#{database_name}/collections/#{collection_name}/documents")
7
+ values = []
8
+ response.each do |item|
9
+ values << Document.new(item)
10
+ end
11
+ end
12
+
13
+ def find(database_name, collection_name, document_id)
14
+ Document.new(client.get("/databases/#{database_name}/collections/#{collection_name}/documents/#{document_id}"))
15
+ end
16
+
17
+ def create(database_name, collection_name, params)
18
+ Document.new(client.post("/databases/#{database_name}/collections/#{collection_name}/documents", params))
19
+ end
20
+
21
+ def update(database_name, collection_name, document_id, params)
22
+ Document.new(client.put("/databases/#{database_name}/collections#{collection_name}/documents/#{document_id}", params))
23
+ end
24
+
25
+ # TODO: how about a "save" option? I could add it here, but it would be two server hits
26
+
27
+ def delete(database_name, collection_name, document_id)
28
+ client.delete("/databases/#{database_name}/collections/#{collection_name}/documents/#{document_id}", params)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ module Sputnik
2
+ class Index < Base
3
+ class << self
4
+ def all(database_name, collection_name)
5
+ response = client.get("/databases/#{database_name}/collections/#{collection_name}/indexes")
6
+ values = []
7
+ response.each do |item|
8
+ values << Index.new(item)
9
+ end
10
+ end
11
+
12
+ def find(database_name, collection_name, index_name)
13
+ Index.new(client.get("/databases/#{database_name}/collections/#{collection_name}/indexes/#{index_name}"))
14
+ end
15
+
16
+ def create(database_name, collection_name, params)
17
+ Index.new(client.post("/databases/#{database_name}/collections/#{collection_name}/indexes", params))
18
+ end
19
+
20
+ def delete(database_name, collection_name, index_name)
21
+ client.delete("/databases/#{database_name}/collections/#{collection_name}/indexes/#{index_name}", params)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module Sputnik
2
+ class Plan < Base
3
+ class << self
4
+ def all
5
+ response = client.get('/plans')
6
+ values = []
7
+ response.each do |item|
8
+ values << Plan.new(item)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Sputnik
2
+ VERSION = "0.1"
3
+ end
File without changes
File without changes
data/sputnik.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "sputnik/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "sputnik"
7
+ s.version = Sputnik::VERSION
8
+ s.authors = ["coderoshi"]
9
+ s.email = ["eric.redmond@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Connects to MongoHQ API}
12
+ s.description = %q{Connects to MongoHQ API}
13
+
14
+ s.rubyforge_project = "sputnik"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ s.add_runtime_dependency "faraday"
23
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sputnik
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - coderoshi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70259773429420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70259773429420
25
+ - !ruby/object:Gem::Dependency
26
+ name: faraday
27
+ requirement: &70259773429000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70259773429000
36
+ description: Connects to MongoHQ API
37
+ email:
38
+ - eric.redmond@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rvmrc
45
+ - Gemfile
46
+ - README.markdown
47
+ - Rakefile
48
+ - lib/sputnik.rb
49
+ - lib/sputnik/base.rb
50
+ - lib/sputnik/collection.rb
51
+ - lib/sputnik/connection.rb
52
+ - lib/sputnik/database.rb
53
+ - lib/sputnik/database_stats.rb
54
+ - lib/sputnik/document.rb
55
+ - lib/sputnik/index.rb
56
+ - lib/sputnik/plan.rb
57
+ - lib/sputnik/version.rb
58
+ - spec/integration/authentication_spec.rb
59
+ - spec/spec_helper.rb
60
+ - sputnik.gemspec
61
+ homepage: ''
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project: sputnik
81
+ rubygems_version: 1.8.10
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Connects to MongoHQ API
85
+ test_files:
86
+ - spec/integration/authentication_spec.rb
87
+ - spec/spec_helper.rb