dozens 0.0.1 → 0.0.2
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.
- data/lib/dozens.rb +92 -2
- data/lib/dozens/api.rb +96 -0
- data/lib/dozens/version.rb +1 -1
- metadata +2 -1
data/lib/dozens.rb
CHANGED
@@ -1,6 +1,96 @@
|
|
1
1
|
require "dozens/version"
|
2
|
-
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'uri'
|
5
|
+
require 'json'
|
3
6
|
|
4
7
|
module Dozens
|
5
|
-
|
8
|
+
class API
|
9
|
+
API_BASE = 'http://dozens.jp/api/'
|
10
|
+
ZONE_BASE = API_BASE + 'zone'
|
11
|
+
RECORD_BASE = API_BASE + 'record'
|
12
|
+
|
13
|
+
def initialize(dozens_id, api_key)
|
14
|
+
@dozens_id = dozens_id
|
15
|
+
@api_key = api_key
|
16
|
+
end
|
17
|
+
def authenticate
|
18
|
+
auth_token
|
19
|
+
end
|
20
|
+
def zones
|
21
|
+
get_with_auth ZONE_BASE + '.json'
|
22
|
+
end
|
23
|
+
def create_zone(params)
|
24
|
+
post_with_auth ZONE_BASE + '/create.json', params
|
25
|
+
end
|
26
|
+
def delete_zone(zone_id)
|
27
|
+
delete_with_auth ZONE_BASE + "/delete/#{zone_id}.json"
|
28
|
+
end
|
29
|
+
def records(zone_name)
|
30
|
+
get_with_auth RECORD_BASE + "/#{zone_name}.json"
|
31
|
+
end
|
32
|
+
def create_record(params)
|
33
|
+
post_with_auth RECORD_BASE + "/create.json", params
|
34
|
+
end
|
35
|
+
def update_record(record_id, params)
|
36
|
+
post_with_auth RECORD_BASE + "/update/#{record_id}.json", params
|
37
|
+
end
|
38
|
+
def delete_record(record_id)
|
39
|
+
delete_with_auth RECORD_BASE + "/delete/#{record_id}.json"
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
def get_with_headers(url, headers = {})
|
44
|
+
request :get, url, headers
|
45
|
+
end
|
46
|
+
def get_with_auth(url)
|
47
|
+
with_auth do |hdrs|
|
48
|
+
request :get, url, hdrs
|
49
|
+
end
|
50
|
+
end
|
51
|
+
def post_with_auth(url, params)
|
52
|
+
with_auth do |hdrs|
|
53
|
+
request :post, url, hdrs, params
|
54
|
+
end
|
55
|
+
end
|
56
|
+
def put_with_auth(url)
|
57
|
+
with_auth do |hdrs|
|
58
|
+
request :put, url, hdrs
|
59
|
+
end
|
60
|
+
end
|
61
|
+
def delete_with_auth(url)
|
62
|
+
with_auth do |hdrs|
|
63
|
+
request :delete, url, hdrs
|
64
|
+
end
|
65
|
+
end
|
66
|
+
def request(verb, url, headers = {}, params = {})
|
67
|
+
uri = URI.parse(url)
|
68
|
+
klass = eval "Net::HTTP::#{verb.capitalize}"
|
69
|
+
req = klass.new(uri.path)
|
70
|
+
headers.each do |key, value|
|
71
|
+
req[key] = value
|
72
|
+
end
|
73
|
+
if req.request_body_permitted? && !params.empty?
|
74
|
+
req.body = params.to_json.to_s
|
75
|
+
req['Content-Type'] = 'application/json'
|
76
|
+
end
|
77
|
+
resp = nil
|
78
|
+
Net::HTTP.start(uri.host, uri.port) do |http|
|
79
|
+
resp = http.request(req)
|
80
|
+
end
|
81
|
+
JSON.parse(resp.body)
|
82
|
+
end
|
83
|
+
def with_auth(org_headers = {}, &block)
|
84
|
+
block.call(org_headers.merge({'X-Auth-Token' => auth_token}))
|
85
|
+
end
|
86
|
+
def authorize_get(dozens_id, api_key)
|
87
|
+
resp = get_with_headers('http://dozens.jp/api/authorize.json',
|
88
|
+
'X-Auth-User' => @dozens_id,
|
89
|
+
'X-Auth-Key' => @api_key)
|
90
|
+
resp['auth_token']
|
91
|
+
end
|
92
|
+
def auth_token
|
93
|
+
@auth_token ||= authorize_get(@dozens_id, @api_key)
|
94
|
+
end
|
95
|
+
end
|
6
96
|
end
|
data/lib/dozens/api.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'uri'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module Dozens
|
8
|
+
class API
|
9
|
+
API_BASE = 'http://dozens.jp/api/'
|
10
|
+
ZONE_BASE = API_BASE + 'zone'
|
11
|
+
RECORD_BASE = API_BASE + 'record'
|
12
|
+
|
13
|
+
def initialize(dozens_id, api_key)
|
14
|
+
@dozens_id = dozens_id
|
15
|
+
@api_key = api_key
|
16
|
+
end
|
17
|
+
def authenticate
|
18
|
+
auth_token
|
19
|
+
end
|
20
|
+
def zones
|
21
|
+
get_with_auth ZONE_BASE + '.json'
|
22
|
+
end
|
23
|
+
def create_zone(params)
|
24
|
+
post_with_auth ZONE_BASE + '/create.json', params
|
25
|
+
end
|
26
|
+
def delete_zone(zone_id)
|
27
|
+
delete_with_auth ZONE_BASE + "/delete/#{zone_id}.json"
|
28
|
+
end
|
29
|
+
def records(zone_name)
|
30
|
+
get_with_auth RECORD_BASE + "/#{zone_name}.json"
|
31
|
+
end
|
32
|
+
def create_record(params)
|
33
|
+
post_with_auth RECORD_BASE + "/create.json", params
|
34
|
+
end
|
35
|
+
def update_record(record_id, params)
|
36
|
+
post_with_auth RECORD_BASE + "/update/#{record_id}.json", params
|
37
|
+
end
|
38
|
+
def delete_record(record_id)
|
39
|
+
delete_with_auth RECORD_BASE + "/delete/#{record_id}.json"
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
def get_with_headers(url, headers = {})
|
44
|
+
request :get, url, headers
|
45
|
+
end
|
46
|
+
def get_with_auth(url)
|
47
|
+
with_auth do |hdrs|
|
48
|
+
request :get, url, hdrs
|
49
|
+
end
|
50
|
+
end
|
51
|
+
def post_with_auth(url, params)
|
52
|
+
with_auth do |hdrs|
|
53
|
+
request :post, url, hdrs, params
|
54
|
+
end
|
55
|
+
end
|
56
|
+
def put_with_auth(url)
|
57
|
+
with_auth do |hdrs|
|
58
|
+
request :put, url, hdrs
|
59
|
+
end
|
60
|
+
end
|
61
|
+
def delete_with_auth(url)
|
62
|
+
with_auth do |hdrs|
|
63
|
+
request :delete, url, hdrs
|
64
|
+
end
|
65
|
+
end
|
66
|
+
def request(verb, url, headers = {}, params = {})
|
67
|
+
uri = URI.parse(url)
|
68
|
+
klass = eval "Net::HTTP::#{verb.capitalize}"
|
69
|
+
req = klass.new(uri.path)
|
70
|
+
headers.each do |key, value|
|
71
|
+
req[key] = value
|
72
|
+
end
|
73
|
+
if req.request_body_permitted? && !params.empty?
|
74
|
+
req.body = params.to_json.to_s
|
75
|
+
req['Content-Type'] = 'application/json'
|
76
|
+
end
|
77
|
+
resp = nil
|
78
|
+
Net::HTTP.start(uri.host, uri.port) do |http|
|
79
|
+
resp = http.request(req)
|
80
|
+
end
|
81
|
+
JSON.parse(resp.body)
|
82
|
+
end
|
83
|
+
def with_auth(org_headers = {}, &block)
|
84
|
+
block.call(org_headers.merge({'X-Auth-Token' => auth_token}))
|
85
|
+
end
|
86
|
+
def authorize_get(dozens_id, api_key)
|
87
|
+
resp = get_with_headers('http://dozens.jp/api/authorize.json',
|
88
|
+
'X-Auth-User' => @dozens_id,
|
89
|
+
'X-Auth-Key' => @api_key)
|
90
|
+
resp['auth_token']
|
91
|
+
end
|
92
|
+
def auth_token
|
93
|
+
@auth_token ||= authorize_get(@dozens_id, @api_key)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/dozens/version.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: dozens
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- CHIKURA Shinsaku
|
@@ -29,6 +29,7 @@ files:
|
|
29
29
|
- Rakefile
|
30
30
|
- dozens.gemspec
|
31
31
|
- lib/dozens.rb
|
32
|
+
- lib/dozens/api.rb
|
32
33
|
- lib/dozens/version.rb
|
33
34
|
has_rdoc: true
|
34
35
|
homepage: ""
|