dozens_jp_cli 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dozens_jp_cli/version.rb +1 -1
- data/lib/dozens_jp_cli.rb +82 -18
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69873f95bad988bdda884bffd178c28441e724b5
|
4
|
+
data.tar.gz: ad6d2f2de81244e51598d62f00107ab82564dceb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17e0f353f306e65a22e5356975591d1d876929b4d4c5612e0e1f4318f2d07cfa86b90e0771a9cb32da83be38a07f2d0cd976d5c80b6982872dbf004c08cdded3
|
7
|
+
data.tar.gz: e69c0f020ce2abd299501570ef8534e3f2301c4e440cb95a4b96c2618d2d25e1f8ea45772c9487f39f728a51910cd564199ffd2c09feb2af4b2623d18b96a011
|
data/lib/dozens_jp_cli.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
+
require 'mechanize'
|
2
|
+
|
1
3
|
require "dozens_jp_cli/version"
|
2
4
|
|
3
5
|
|
4
6
|
class Dozens
|
5
7
|
attr_accessor :ua
|
8
|
+
API_BASE_URI = "http://dozens.jp/api"
|
6
9
|
|
7
10
|
def initialize(user,auth_key)
|
8
11
|
@user = user
|
@@ -15,11 +18,24 @@ class Dozens
|
|
15
18
|
def post(uri,header=nil,params=[])
|
16
19
|
self.request(:post,uri,header,params)
|
17
20
|
end
|
21
|
+
def delete(uri,header=nil,params={})
|
22
|
+
self.request(:delete,uri,header,params)
|
23
|
+
end
|
18
24
|
def request(method,uri,header=nil,params=[])
|
19
|
-
header = {"X-Auth-Token"=>
|
20
|
-
begin
|
21
|
-
|
22
|
-
|
25
|
+
header = {"X-Auth-Token"=> self.auth_token } if header.nil?
|
26
|
+
begin
|
27
|
+
if method == :get then
|
28
|
+
ua.get(uri, params,nil,header)
|
29
|
+
end
|
30
|
+
|
31
|
+
if method == :post then
|
32
|
+
header["Content-type"] = "application/json"
|
33
|
+
ua.post(uri,params,header)
|
34
|
+
end
|
35
|
+
if method == :delete then
|
36
|
+
ua.delete(uri,params,header)
|
37
|
+
end
|
38
|
+
|
23
39
|
rescue =>e
|
24
40
|
raise e
|
25
41
|
end
|
@@ -29,52 +45,100 @@ class Dozens
|
|
29
45
|
if @auth_token && @expires && @expires > Time.now then
|
30
46
|
return @auth_token
|
31
47
|
end
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
48
|
+
|
49
|
+
uri = API_BASE_URI+"/authorize.json"
|
50
|
+
self.get(uri ,{"X-Auth-User"=>@user,"X-Auth-Key"=>@auth_key})
|
51
|
+
|
37
52
|
@auth_token = JSON.parse(ua.page.body)["auth_token"]
|
38
53
|
@expires = Time.now+60*60*24
|
39
54
|
@auth_token
|
40
55
|
end
|
41
56
|
|
57
|
+
def zone_delete(name_or_id)
|
58
|
+
zone_id = ( name_or_id =~ /\d+/ ) ? name_or_id : self.find_zone_id(name_or_id)
|
59
|
+
url = API_BASE_URI+"/zone/delete/#{zone_id}.json"
|
60
|
+
self.delete(url)
|
61
|
+
end
|
62
|
+
def zone_create_new(name,email)
|
63
|
+
params_str = {name:name, mailaddress:email, add_google_apps:false}.to_json
|
64
|
+
url = API_BASE_URI+"/zone/create.json"
|
65
|
+
self.post(url,nil,params_str)
|
66
|
+
end
|
67
|
+
|
42
68
|
def zone_list
|
43
|
-
uri = "
|
69
|
+
uri = API_BASE_URI+"/zone.json"
|
44
70
|
ret = self.get(uri)
|
45
71
|
@domains = JSON.parse(ret)["domain"]
|
46
72
|
end
|
47
73
|
def record_list(zone_name)
|
48
|
-
uri = "
|
74
|
+
uri = API_BASE_URI+"/record/#{zone_name}.json"
|
49
75
|
ret = self.get(uri)
|
50
76
|
@records = JSON.parse(ret)["record"]
|
51
77
|
end
|
78
|
+
def find_zone_id(name)
|
79
|
+
zone = self.zone_list.find{|e|
|
80
|
+
name =~/#{e["name"]}/
|
81
|
+
}
|
82
|
+
return nil unless zone
|
83
|
+
return zone["id"]
|
84
|
+
end
|
85
|
+
def zone_exists?(name)
|
86
|
+
self.find_zone_id(name)!=nil
|
87
|
+
end
|
88
|
+
|
52
89
|
def find_record_id(name)
|
53
90
|
zone = self.zone_list.find{|e|
|
54
91
|
name =~/#{e["name"]}/
|
55
|
-
|
92
|
+
}
|
56
93
|
return nil unless zone
|
57
94
|
record = self.record_list(zone["name"]).find{|e|
|
58
95
|
name =~/#{e["name"]}/
|
59
96
|
}
|
60
97
|
return nil unless record
|
61
98
|
record["id"]
|
99
|
+
end
|
100
|
+
def record_exists?(record_name)
|
101
|
+
self.find_record_id(record_name)!=nil
|
102
|
+
end
|
103
|
+
def record_delete(name_or_id)
|
104
|
+
record_id = ( name_or_id =~ /\d+/ ) ? name_or_id : self.find_record_id(name_or_id)
|
105
|
+
url = API_BASE_URI+"/record/delete/#{record_id}.json"
|
106
|
+
self.delete(url)
|
107
|
+
end
|
108
|
+
def record_create(name,zone,content=nil,type="A",prio=10,ttl=7200)
|
109
|
+
if name =~/#{zone}/ then
|
110
|
+
name = name.gsub(/\.#{zone}/,"" )
|
111
|
+
end
|
112
|
+
content ||= self.current_global_ip
|
113
|
+
raise unless self.zone_exists? zone
|
114
|
+
|
115
|
+
param = {
|
116
|
+
domain: zone,
|
117
|
+
name: name,
|
118
|
+
type: type,
|
119
|
+
prio: prio,
|
120
|
+
content: content,
|
121
|
+
ttl: ttl,
|
122
|
+
}.to_json
|
123
|
+
|
124
|
+
url = API_BASE_URI+"/record/create.json"
|
125
|
+
self.post(url,nil,param)
|
126
|
+
|
127
|
+
|
128
|
+
|
62
129
|
end
|
63
130
|
def record_update(record_id,prio=10,content=nil,ttl=7200)
|
64
131
|
content = self.current_global_ip unless content
|
65
132
|
params = {"prio"=>prio,"content"=>content,"ttl"=>ttl}.to_json
|
66
|
-
url = "
|
133
|
+
url = API_BASE_URI+"/record/update/#{record_id}.json"
|
67
134
|
ret = self.post(url,nil,params)
|
68
|
-
|
135
|
+
JSON.parse(ret)["record"]
|
69
136
|
end
|
70
137
|
|
71
138
|
def current_global_ip
|
72
139
|
url = "http://myexternalip.com/json"
|
73
|
-
ret = self.get(url)
|
140
|
+
ret = self.get(url,{},[])
|
74
141
|
ret = JSON.parse(ret)["ip"]
|
75
142
|
end
|
76
143
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
144
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dozens_jp_cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- takuya
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|