dotblock-api 0.0.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.
@@ -0,0 +1,18 @@
1
+ # DotBlockAPI
2
+
3
+ This is a Ruby client for <http://api.dotblock.com/>.
4
+
5
+
6
+ ## Installation
7
+
8
+ gem install dotblock-api
9
+
10
+
11
+ ## Usage
12
+
13
+ See `examples/example.rb`
14
+
15
+
16
+ ## License
17
+
18
+ MIT License, see LICENSE in this repo.
@@ -0,0 +1,31 @@
1
+ $:.unshift 'lib'
2
+
3
+ task :default => :test
4
+
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test' << '.'
8
+ test.pattern = 'test/**/*_test.rb'
9
+ test.verbose = true
10
+ end
11
+
12
+ desc "Open an irb session preloaded with this library"
13
+ task :console do
14
+ sh "irb -rubygems -r ./lib/dotblock_api.rb -I ./lib"
15
+ end
16
+
17
+ require 'sdoc_helpers'
18
+ desc "Push a new version to Gemcutter"
19
+ task :publish do
20
+ require 'dotblock_api/version'
21
+
22
+ ver = DotBlockAPI::Version
23
+
24
+ sh "gem build dotblock-api.gemspec"
25
+ sh "gem push dotblock-api-#{ver}.gem"
26
+ sh "git tag -a -m 'DotBlockAPI v#{ver}' v#{ver}"
27
+ sh "git push origin v#{ver}"
28
+ sh "git push origin master"
29
+ sh "git clean -fd"
30
+ #sh "rake pages"
31
+ end
@@ -0,0 +1,4 @@
1
+ module DotBlockAPI
2
+ autoload :Version, 'dotblock_api/version'
3
+ autoload :Client, 'dotblock_api/client'
4
+ end
@@ -0,0 +1,79 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ module DotBlockAPI
5
+ class Client
6
+
7
+ def initialize(username, password)
8
+ @username = username
9
+ @password = password
10
+ end
11
+
12
+ def ping
13
+ send_request("/v1/ping")
14
+ end
15
+
16
+ def client_info
17
+ send_request("/v1/account.json")
18
+ end
19
+
20
+ def update_client_info(params = {})
21
+ info = Hash[params.map { |key, val| ["info[#{key}]", val] }]
22
+ send_request("/v1/account.json", 'PUT', info)
23
+ end
24
+
25
+ def server_list
26
+ send_request("/v1/servers.json")
27
+ end
28
+
29
+ def server_info(id)
30
+ send_request("/v1/servers/#{id}.json")
31
+ end
32
+
33
+ def reboot_server(id)
34
+ send_request("/v1/servers/#{id}.json", 'PUT', { "action" => "reboot" })
35
+ end
36
+
37
+ def boot_server(id)
38
+ send_request("/v1/servers/#{id}.json", 'PUT', { "action" => "boot" })
39
+ end
40
+
41
+ def suspend_server(id)
42
+ send_request("/v1/servers/#{id}.json", 'PUT', { "action" => "suspend" })
43
+ end
44
+
45
+ def resume_server(id)
46
+ send_request("/v1/servers/#{id}.json", 'PUT', { "action" => "resume" })
47
+ end
48
+
49
+ def shutdown_server(id)
50
+ send_request("/v1/servers/#{id}.json", 'PUT', { "action" => "shutdown" })
51
+ end
52
+
53
+ private
54
+
55
+ def send_request(url, method = 'GET', params = {})
56
+ url = URI.parse("http://api.dotblock.com#{url}")
57
+ req = case method.to_s.upcase
58
+ when 'GET'
59
+ Net::HTTP::Get.new(url.path)
60
+ when 'PUT'
61
+ Net::HTTP::Put.new(url.path)
62
+ else
63
+ raise "Invalid method"
64
+ end
65
+
66
+ req.basic_auth(@username, @password)
67
+
68
+ unless method == 'GET' || params.empty?
69
+ req.set_form_data(params)
70
+ end
71
+
72
+ res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
73
+
74
+ JSON.parse(res.body)
75
+ rescue
76
+ nil
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module DotBlockAPI
2
+ VERSION = Version = '0.0.1'
3
+ end
@@ -0,0 +1,60 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class DotBlockAPIClientTest < Test::Unit::TestCase
4
+ context "DotBlockAPI Client instance" do
5
+ setup do
6
+ @serverid = ENV['DOTBLOCK_SERVERID']
7
+ @api = DotBlockAPI::Client.new(ENV['DOTBLOCK_USERNAME'], ENV['DOTBLOCK_PASSWORD'])
8
+ end
9
+
10
+ context "client info actions" do
11
+
12
+ should "return client info" do
13
+ assert_not_nil @api.client_info
14
+ end
15
+
16
+ should "update client info" do
17
+ old_info = @api.client_info
18
+ new_info = @api.update_client_info({'firstname' => 'Bender', 'lastname' => 'Rodriguez'})
19
+ assert old_info['firstname'] != new_info['firstname']
20
+ assert old_info['lastname'] != new_info['lastname']
21
+ assert new_info['firstname'] == 'Bender'
22
+ assert new_info['lastname'] == 'Rodriguez'
23
+ end
24
+
25
+ end
26
+
27
+ context "server actions" do
28
+
29
+ should "return list of all servers" do
30
+ assert_not_nil @api.server_list
31
+ end
32
+
33
+ should "return info for a server" do
34
+ assert_not_nil @api.server_info(@serverid)
35
+ end
36
+
37
+ should "reboot a server" do
38
+ assert @api.reboot_server(@serverid)['response'] == 'success'
39
+ end
40
+
41
+ should "boot a server" do
42
+ assert @api.reboot_server(@serverid)['response'] == 'success'
43
+ end
44
+
45
+ should "suspend a server" do
46
+ assert @api.suspend_server(@serverid)['response'] == 'success'
47
+ end
48
+
49
+ should "resume a server" do
50
+ assert @api.resume_server(@serverid)['response'] == 'success'
51
+ end
52
+
53
+ should "shutdown a server" do
54
+ assert @api.shutdown_server(@serverid)['response'] == 'success'
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ begin
6
+ require 'turn'
7
+ rescue LoadError
8
+ end
9
+
10
+ require 'dotblock_api'
11
+
12
+ if ENV['DOTBLOCK_USERNAME'].nil? || ENV['DOTBLOCK_PASSWORD'].nil? || ENV['DOTBLOCK_SERVERID'].nil?
13
+ abort <<-MSG
14
+
15
+ Run tests with DOTBLOCK_USERNAME, DOTBLOCK_PASSWORD, and DOTBLOCK_SERVERID eg:
16
+
17
+ DOTBLOCK_USERNAME=josh@dotblock.com DOTBLOCK_PASSWORD=pwn13s DOTBLOCK_SERVERID=123456 rake test
18
+
19
+ MSG
20
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dotblock-api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Joshua Priddle
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-23 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 11
30
+ segments:
31
+ - 1
32
+ - 4
33
+ - 6
34
+ version: 1.4.6
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: shoulda
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: " DotBlock API: A Ruby client for api.dotblock.com\n"
52
+ email: jpriddle@nevercraft.net
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - README.markdown
59
+ files:
60
+ - Rakefile
61
+ - README.markdown
62
+ - lib/dotblock_api/client.rb
63
+ - lib/dotblock_api/version.rb
64
+ - lib/dotblock_api.rb
65
+ - test/client_test.rb
66
+ - test/test_helper.rb
67
+ has_rdoc: true
68
+ homepage: http://github.com/dotblock/dotblock-api-ruby
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options:
73
+ - --charset=UTF-8
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.5.0
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: "DotBlock API: A Ruby client for api.dotblock.com"
101
+ test_files: []
102
+