dh_api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/bin/dh_api +4 -0
- data/dh_api.gemspec +22 -0
- data/lib/dh_api.rb +96 -0
- data/lib/dh_api/version.rb +3 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/bin/dh_api
ADDED
data/dh_api.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "dh_api/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "dh_api"
|
7
|
+
s.version = DhApi::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Rui Andrada"]
|
10
|
+
s.email = ["shingonoide@gmail.com"]
|
11
|
+
s.homepage = "http://shingonoide.barradev.com"
|
12
|
+
s.summary = %q{Ruby gem and command line to access Dreamhost's API through json}
|
13
|
+
s.description = %q{I need a library to workout with Dreamhost's API, and found Dreamy but seems to be outdated.
|
14
|
+
So I trying to make my own gem and trying to get better and using json
|
15
|
+
}
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.add_dependency('hashie', '= 1.0.0')
|
22
|
+
end
|
data/lib/dh_api.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/https'
|
3
|
+
require 'json'
|
4
|
+
require 'hashie'
|
5
|
+
|
6
|
+
module DhApi
|
7
|
+
class Account
|
8
|
+
@@host = 'api.dreamhost.com'
|
9
|
+
|
10
|
+
def initialize(api_key)
|
11
|
+
@api_key = api_key
|
12
|
+
end
|
13
|
+
|
14
|
+
# Return Hash of domains objects
|
15
|
+
def domains
|
16
|
+
request('domain-list_domains').inject([]) { |domains, domain|
|
17
|
+
domains << Domain.new(domain)
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
#Return Hash of users objects
|
22
|
+
def users(passwords=false)
|
23
|
+
response = passwords ? request('user-list_users') : request('user-list_users_no_pw')
|
24
|
+
response.inject([]) { |users, user|
|
25
|
+
users << User.new(user)
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def dns(cmd='list', entry={})
|
30
|
+
case cmd
|
31
|
+
when 'list'
|
32
|
+
request('dns-list_records').inject([]){ |dnses, dns|
|
33
|
+
dnses << DNS.new(dns)
|
34
|
+
}
|
35
|
+
when 'add'
|
36
|
+
request('dns-add_record', {'record' => entry['record'], 'type' => entry['type'], 'value' => entry['value']})
|
37
|
+
when 'remove'
|
38
|
+
request('dns-remove_record', {'record' => entry['record'], 'type' => entry['type'], 'value' => entry['value']})
|
39
|
+
else
|
40
|
+
raise APIParameterError, 'Unknown DNS command'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def request(cmd, values={}, use_post=false)
|
46
|
+
values = {
|
47
|
+
'key' => @api_key,
|
48
|
+
'format' => 'json',
|
49
|
+
'cmd' => cmd,
|
50
|
+
}.merge(values)
|
51
|
+
|
52
|
+
connection = Net::HTTP.new(@@host, 443)
|
53
|
+
connection.use_ssl=true
|
54
|
+
connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
55
|
+
|
56
|
+
begin
|
57
|
+
response =
|
58
|
+
if use_post
|
59
|
+
post = Net::HTTP::Post.new('/')
|
60
|
+
post.form_data=values
|
61
|
+
connection.request(post)
|
62
|
+
else
|
63
|
+
prepare_query_string=''
|
64
|
+
values.each_pair { |key, value|
|
65
|
+
prepare_query_string << "#{key}=#{value}&"
|
66
|
+
}
|
67
|
+
querystring = "/?#{prepare_query_string}"
|
68
|
+
connection.get(querystring)
|
69
|
+
end
|
70
|
+
rescue => error
|
71
|
+
raise APIRequestError, error.message
|
72
|
+
end
|
73
|
+
|
74
|
+
if %w[200 304].include?(response.code)
|
75
|
+
data = JSON.parse(response.body)
|
76
|
+
elsif response.code == '503'
|
77
|
+
raise APIRequestError, response.message
|
78
|
+
elsif response.code == '401'
|
79
|
+
raise APIRequestError, 'Authentication error. please check if your key are correct'
|
80
|
+
else
|
81
|
+
raise APIRequestError, "Dreamhost API response: ##{response.code}, message: #{response.message}"
|
82
|
+
end
|
83
|
+
|
84
|
+
raise APIRequestError, "#{data['data']}: #{data['reason']}" unless data['result'] == 'success'
|
85
|
+
data['data']
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class Domain < Hashie::Mash; end
|
90
|
+
class User < Hashie::Mash; end
|
91
|
+
class DNS < Hashie::Mash; end
|
92
|
+
class APIRequestError < StandardError;
|
93
|
+
end
|
94
|
+
class APIParameterError < StandardError;
|
95
|
+
end
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dh_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
|
+
- Rui Andrada
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-18 00:00:00 -03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: hashie
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 1.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: "I need a library to workout with Dreamhost's API, and found Dreamy but seems to be outdated.\n So I trying to make my own gem and trying to get better and using json\n "
|
38
|
+
email:
|
39
|
+
- shingonoide@gmail.com
|
40
|
+
executables:
|
41
|
+
- dh_api
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- Rakefile
|
50
|
+
- bin/dh_api
|
51
|
+
- dh_api.gemspec
|
52
|
+
- lib/dh_api.rb
|
53
|
+
- lib/dh_api/version.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://shingonoide.barradev.com
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.6.2
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Ruby gem and command line to access Dreamhost's API through json
|
88
|
+
test_files: []
|
89
|
+
|