cloudflare 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +1 -1
  2. data/README.md +21 -9
  3. data/lib/cloudflare.rb +505 -316
  4. data/test/test_cloudflare.rb +1 -1
  5. metadata +11 -7
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Marcin "B4k3r" Prokop
1
+ Copyright (c) 2012-2013 Marcin "B4k3r" Prokop
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
data/README.md CHANGED
@@ -1,13 +1,25 @@
1
+ NOTICE
2
+ ======
3
+
4
+ In version v1.1.0 I renamed some functions in Client API.
5
+
6
+ - threat_score -> ip_lkup
7
+ - set_cache_lvl -> cache_lvl
8
+ - set_security_lvl -> sec_lvl
9
+ - purge_cache -> fpurge_ts
10
+ - update_image -> zone_grab
11
+
12
+
1
13
  CloudFlare
2
14
  ==========
3
15
 
4
16
  It is a Ruby wrapper for the CloudFlare API.
5
17
 
6
- Official home page is [here](http://b4k3r.github.com/cloudflare). The complete [RDoc](http://rdoc.info/github/B4k3r/cloudflare/) is online.
18
+ Official home page is [here](https://github.com/B4k3r/cloudflare). The complete [RDoc](http://rdoc.info/github/B4k3r/cloudflare/) is online.
7
19
 
8
20
  Visit also a CloudFlare API documentation:
9
21
 
10
- - [Client](http://www.cloudflare.com/wiki/Client_Interface_API)
22
+ - [Client](http://www.cloudflare.com/docs/client-api.html)
11
23
  - [Host](http://www.cloudflare.com/docs/host-api.html)
12
24
 
13
25
  Installation
@@ -35,9 +47,9 @@ cf = CloudFlare.new('user_api_key', 'user_email')
35
47
  output = cf.add_rec('domain.com', 'A', '212.11.6.211', 'subdomain.domain.com', true)
36
48
 
37
49
  if output['result'] == 'success'
38
- puts 'Successfuly added DNS record'
50
+ puts 'Successfuly added DNS record'
39
51
  else
40
- puts output['msg'] // error message
52
+ puts output['msg'] # error message
41
53
  end
42
54
  ```
43
55
 
@@ -47,20 +59,20 @@ end
47
59
  require 'cloudflare'
48
60
 
49
61
  cf = CloudFlare.new('host_api_key')
50
- output = cf.create_user('new_user_email', 'new_password', 'new_username (optional)', 'unique id (optional)')
62
+ output = cf.create_user('john@example.com', 'secret', 'john')
51
63
 
52
64
  if output['result'] == 'success'
53
- puts output['msg']
54
- puts "Your login is #{output['response']['cloudflare_username']}"
65
+ puts output['msg']
66
+ puts "Your login is #{output['response']['cloudflare_username']}" # => john
55
67
  else
56
- puts output['msg'] // error message
68
+ puts output['msg'] # error message
57
69
  end
58
70
  ```
59
71
 
60
72
  License
61
73
  -------
62
74
 
63
- Copyright © 2012. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
75
+ Copyright © 2012 - 2013. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
64
76
 
65
77
 
66
78
 
@@ -4,323 +4,512 @@ require 'json'
4
4
  # All public functions return (Hash) result: success or error with message and error code
5
5
  #
6
6
  # For more information please visit:
7
- # - http://www.cloudflare.com/wiki/Client_Interface_API
7
+ # - http://www.cloudflare.com/docs/client-api.html
8
8
  # - http://www.cloudflare.com/docs/host-api.html
9
+ #
9
10
 
10
11
  class CloudFlare
11
12
 
12
- # URL for Client and Host API
13
-
14
- URL_API = {
15
- client: 'https://www.cloudflare.com/api_json.html',
16
- host: 'https://api.cloudflare.com/host-gw.html'
17
- }
18
-
19
- TIMEOUT = 5 # Default is 5 seconds
20
-
21
- # @param api_key User or Host API key.
22
- # @param email It is for a Client API.
23
- def initialize(api_key, email = nil)
24
-
25
- @params = Hash.new
26
-
27
- if email.nil?
28
- @params[:api_key] = api_key
29
- else
30
- @params[:api_key] = api_key
31
- @params[:email] = email
32
- end
33
-
34
- end
35
-
36
- # CLIENT
37
-
38
- # This function can be used to get currently settings of values such as the security level.
39
- #
40
- # More: https://www.cloudflare.com/wiki/Client_Interface_API#FUNCTION:_Current_Stats_and_Settings
41
- #
42
- # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
43
- # @param interval The interval parameter defines what period you want to look at. Default is 30 days, but 1 day delayed. Pro only intervals are 100, 110, and 120.
44
- #
45
- # @return (Hash) the current stats and settings for a particular website
46
-
47
- def stats(zone, interval = 20)
48
- send_req({a: :stats, z: zone, interval: interval})
49
- end
50
-
51
- # This function sets the Basic Security Level to HELP I'M UNDER ATTACK / HIGH / MEDIUM / LOW / ESSENTIALLY OFF.
52
- #
53
- # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
54
- # @param value Must be one of low|med|high|help|eoff.
55
-
56
- def set_security_lvl(zone, value)
57
- send_req({a: :sec_lvl, z: zone, v: value})
58
- end
59
-
60
- # This function sets the Caching Level to Aggressive or Basic.
61
- #
62
- # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
63
- # @param value Must be one of agg|basic.
64
-
65
- def set_cache_lvl(zone, value)
66
- send_req({a: :cache_lvl, z: zone, v: value})
67
- end
68
-
69
- # This function allows you to toggle Development Mode on or off for a particular domain. When Development Mode is on the cache is bypassed. Development mode remains on for 3 hours or until when it is toggled back off.
70
- #
71
- # @note Development mode will expire on "expires_on" (3 hours from when it is toggled on). Development mode can be toggled off immediately by setting +value+ to 0.
72
- #
73
- # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+
74
- # @param value May be set to true (on) or false (off).
75
- # @return (Hash) expires_on
76
-
77
- def devmode(zone, value)
78
- send_req({a: :devmode, z: zone, v: value == true ? 1 : 0})
79
- end
80
-
81
- # This function will purge CloudFlare of any cached files. It may take up to 48 hours for the cache to rebuild and optimum performance to be achieved so this function should be used sparingly.
82
- #
83
- # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
84
- # @return (Hash) fpurge_ts, cooldawn
85
- # @return *fpurge_ts* - Time at which cache was purged.
86
- # @return *cooldown* - Number of seconds before the next time this call is allowed again.
87
-
88
- def purge_cache(zone)
89
- send_req({a: :fpurge_ts, z: zone, v: 1})
90
- end
91
-
92
- # This function checks whether one or more websites/domains are active under an account and return the zone ids (zids) for these.
93
- #
94
- # @param zones Zones, eg. +example.com+, +exampletwo.com+
95
- # @return (Hash) Map of passed in zones. If a zone if hosted on CloudFlare and the email + tkn combination is correct for the given zone, the value for the zone will be its zone id (use this for other API calls). Otherwise 0.
96
-
97
- def zone_check(*zones)
98
- send_req({a: :zone_check, zones: zones.kind_of?(Array) ? zones.join(',') : zones})
99
- end
100
-
101
- # This function pulls recent IPs hitting your site.
102
- #
103
- # @param zoneid Id of the zone you would like to check.
104
- # @param hours Number of hours to go back. Default is 24, max is 48.
105
- # @param class Restrict the result set to a given class. Currently r|s|t, for regular, crawler, threat resp.
106
- # @param geo Add to add longitude and latitude information to the response. 0,0 means no data.
107
- # @return (Hash) A list of IP addresses which hit your site classified by type.
108
-
109
- def zone_ips(zoneid, classification, hours = 24, geo = '0,0')
110
- send_req({a: :zone_ips, zid: zoneid, hours: hours, class: classification, geo: geo})
111
- end
112
-
113
- # This functions updates the snapshot of your site for CloudFlare's challenge page.
114
- #
115
- # @note Yhis call is rate limited to once per zone per day. Also the new image may take up to 1 hour to appear.
116
- #
117
- # @param zoneid Id of the zone you would like to check.
118
-
119
- def update_image(zoneid)
120
- send_req({a: :zone_grab, zid: zoneid})
121
- end
122
-
123
- # This function adds an IP address to your white lists.
124
- #
125
- # @param address The address you wish to set a rule for.
126
-
127
- def whitelist(address)
128
- send_req({a: :wl, key: address})
129
- end
130
-
131
- # This function adds an IP address to your black lists.
132
- #
133
- # @param address The address you wish to set a rule for.
134
-
135
- def blacklist(address)
136
- send_req({a: :ban, key: address})
137
- end
138
-
139
- # This function creates a new DNS record for your site. This can be either a CNAME or A record.
140
- #
141
- # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
142
- # @param type Type of record - CNAME or A.
143
- # @param content The value of the cname or IP address (the destination).
144
- # @param name The name of the record you wish to create.
145
- # @param mode False or true. false means CloudFlare is off (grey cloud) for the new zone, while true means a happy orange cloud.
146
-
147
- def add_rec(zone, type, content, name, mode)
148
- send_req({a: :rec_set, zone: zone, type: type, content: content, name: name, mode: mode == true ? 1 : 0})
149
- end
150
-
151
- # This function deletes a DNS record.
152
- #
153
- # @note All records of the given name will be deleted. For this reason, you must pass in the full DNS name of the record you wish to remove. For +example+, +sub.foo.com+, as opposed to just sub.
154
- #
155
- # @param zone
156
- # @param name The name of the record you wish to remove.
157
-
158
- def del_rec(zone, name)
159
- send_req({a: :rec_del, zone: zone, name: name})
160
- end
161
-
162
- # This function purges the preloader's cache.
163
- #
164
- # @note Can take up to an hour for this to take effect.
165
- #
166
- # @param ip The value of the IP address.
167
-
168
- def pre_purge(ip)
169
- send_req({a: :pre_purge, zone_name: ip})
170
- end
171
-
172
- # This function updates a DNS record for your site. This needs to be an A record.
173
- #
174
- # @param ip The value of the IP address (the destination).
175
- # @param hosts The name of the record you wish to adjust.
176
-
177
- def update_rec(ip, hosts)
178
- send_req({a: :DIUP, ip: ip, hosts: hosts})
179
- end
180
-
181
- # This function checks the threat score for a given IP.
182
- #
183
- # @note scores are logarithmically increasing, like the Richter scale.
184
- #
185
- # @param ip IP address to check.
186
- # @return The current threat score for a given IP.
187
-
188
- def threat_score(ip)
189
- send_req({a: :ip_lkup, ip: ip})
190
- end
191
-
192
- # This function toggles ipv6 support for a site.
193
- #
194
- # @param zone
195
- # @param value False disables, true enables support.
196
-
197
- def toggle_ipv6(zone, value)
198
- send_req({a: :ipv46, z: zone, v: value == true ? 1 : 0})
199
- end
200
-
201
- # HOST
202
-
203
- # This function creates a CloudFlare account mapped to your user.
204
- #
205
- # @param email The user's e-mail address for the new CloudFlare account.
206
- # @param pass The user's password for the new CloudFlare account. CloudFlare will never store this password in clear text.
207
- # @param login (optional) The user's username for the new CloudFlare account. CloudFlare will auto-generate one if it is not specified.
208
- # @param id Set a unique string identifying the User. This identifier will serve as an alias to the user's CloudFlare account. Typically you would set this value to the unique ID in your system (e.g., the internal customer number or username stored in your own system). This parameter can be used to retrieve a user_key when it is required. The unique_id must be an ASCII string with a maximum length of 100 characters.
209
- # @return (String) cloudflare_email
210
- # @return (String) user_key
211
- # @return (String) unique_id
212
- # @return (String) cloudflare_username
213
-
214
- def create_user(email, pass, login = nil, id = nil)
215
- send_req({act: :user_create, cloudflare_email: email, cloudflare_pass: pass, cloudflare_username: login, unique_id: id})
216
- end
217
-
218
- # This function setups a User's zone for CNAME hosting.
219
- #
220
- # @note This function replaces any previous setup for the particular zone_name. If are adding an additional subdomain to an account that already has some subdomains setup, you should specify all the subdomains not only the new subdomains.
221
- #
222
- # @param user_key The unique 32 hex character auth string, identifying the user's CloudFlare Account. Generated from a +create_user+ or +user_auth+.
223
- # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
224
- # @param resolve_to The CNAME that CloudFlare should ultimately resolve web connections to after they have been filtered, e.g. +resolve-to-cloudflare.example.com+. This record should ultimately resolve to the one or more IP addresses of the hosts for the particular website for all the specified subdomains.
225
- # @param subdomains A comma-separated string of subdomain(s) that CloudFlare should host, e.g. +www,blog,forums+ or +www.example.com,blog.example.com,forums.example.com+.
226
- # @return (String) zone_name
227
- # @return (String) resolving_to
228
- # @return (Hash) hosted_cnames
229
- # @return (Hash) forward_tos
230
-
231
- def add_zone(user_key, zone, resolve_to, subdomains)
232
- send_req({act: :zone_set, user_key: user_key, zone_name: zone, resolve_to: resolve_to, subdomains: subdomains.kind_of?(Array) ? zones.join(',') : subdomains})
233
- end
234
-
235
- # This function lookups a user's CloudFlare account information.
236
- #
237
- # @note If you use +unique_id+, +id+ must be +true+.
238
- #
239
- # *Example:*
240
- #
241
- # cf = CloudFlare('your_host_key')
242
- # cf.user_lookup('unique_id', true)
243
- #
244
- # @param email Lookup a user's account information or status by either +email+ or +unique_id+.
245
- # @return (String) user_key
246
- # @return (Boolean) user_exists
247
- # @return (Boolean) user_authed
248
- # @return (String) cloudflare_email
249
- # @return (String) unique_id
250
- # @return (Array) hosted_zones
251
-
252
- def user_lookup(email, id = false)
253
- if id
254
- send_req({act: :user_lookup, unique_id: email})
255
- else
256
- send_req({act: :user_lookup, cloudflare_email: email})
257
- end
258
- end
259
-
260
- # This function authorizes access to a user's existing CloudFlare account.
261
- #
262
- # @param email the user's e-mail address for the new CloudFlare account.
263
- # @param pass the user's password for the new CloudFlare account. CloudFlare will never store this password in clear text.
264
- # @param unique_id (optional) set a unique string identifying the user. This identifier will serve as an alias to the user's CloudFlare account. Typically you would set this value to the unique ID in your system. This parameter can be used as an alias for other actions (e.g., it can substitute for the +email+ and +pass+ if you choose not to store those fields in your system).
265
- # @return (Hash) User's e-mail, key and unique id.
266
-
267
- def user_auth(email, pass, id = nil)
268
- send_req({act: :user_auth, cloudflare_email: email, cloudflare_pass: pass, unique_id: id})
269
- end
270
-
271
- # This function lookups a specific user's zone.
272
- #
273
- # @param user_key API user key
274
- # @param zone the zone you'd like to lookup, e.g. "example.com"
275
- # @return (String) zone_name
276
- # @return (Boolean) zones_exists
277
- # @return (Boolean) zone_hosted
278
- # @return (Hash) hosted_cnames
279
- # @return (Hash) hosted_cnames
280
- # @return (Hash) forward_tos
281
-
282
- def zone_lookup(user_key, zone)
283
- send_req({act: :zone_lookup, user_key: user_key, zone_name: zone})
284
- end
285
-
286
- # This function deletes a specific zone on behalf of a user.
287
- #
288
- # @param user_key API user key
289
- # @param zone The zone you'd like to lookup, e.g. +example.com+
290
- # @return (String) zone_name,
291
- # @return (Boolean) zone_deleted
292
-
293
- def del_zone(user_key, zone)
294
- send_req({act: :zone_delee, user_key: user_key, zone_name: zone})
295
- end
296
-
297
- private
298
-
299
- def send_req(params)
300
-
301
- if @params[:email]
302
- params[:tkn] = @params[:api_key]
303
- params[:u] = @params[:email]
304
- uri = URI(URL_API[:client])
305
- else
306
- params[:host_key] = @params[:api_key]
307
- uri = URI(URL_API[:host])
308
- end
309
-
310
- req = Net::HTTP::Post.new(uri.path)
311
- req.set_form_data(params)
312
-
313
- http = Net::HTTP.new(uri.host, uri.port)
314
- http.use_ssl = true
315
- http.read_timeout = TIMEOUT
316
-
317
- begin
318
- res = http.request(req)
319
- JSON.parse(res.body)
320
- rescue => e
321
- puts "#{e.class} #{e.message}"
322
- end
323
-
324
- end
325
-
326
- end
13
+ # URL for Client and Host API
14
+
15
+ URL_API = {
16
+ client: 'https://www.cloudflare.com/api_json.html',
17
+ host: 'https://api.cloudflare.com/host-gw.html'
18
+ }
19
+
20
+ TIMEOUT = 5 # Default is 5 seconds
21
+
22
+ # @param api_key [String] user or Host API key.
23
+ # @param email [String] it is for a Client API.
24
+ def initialize(api_key, email = nil)
25
+ @params = Hash.new
26
+
27
+ if email.nil?
28
+ @params[:api_key] = api_key
29
+ else
30
+ @params[:api_key] = api_key
31
+ @params[:email] = email
32
+ end
33
+
34
+ end
35
+
36
+ # CLIENT
37
+
38
+ # This function can be used to get currently settings of values such as the security level.
39
+ #
40
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.1
41
+ #
42
+ # @param zone [String]
43
+ # @param interval [Integer]
44
+
45
+ def stats(zone, interval = 20)
46
+ send_req({a: :stats, z: zone, interval: interval})
47
+ end
48
+
49
+ # This function lists all domains in a CloudFlare account along with other data.
50
+ #
51
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.2
52
+
53
+ def zone_load_multi
54
+ send_req(a: :zone_load_multi)
55
+ end
56
+
57
+ # This function lists all of the DNS records from a particular domain in a CloudFlare account.
58
+ #
59
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.3
60
+ #
61
+ # @param zone [String]
62
+
63
+ def rec_load_all(zone)
64
+ send_req({a: :rec_load_all, z: zone})
65
+ end
66
+
67
+ # This function checks whether one or more websites/domains are active under an account and return the zone ids (zids) for these.
68
+ #
69
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.4
70
+ #
71
+ # @param zones [String or Array]
72
+
73
+ def zone_check(*zones)
74
+ send_req({a: :zone_check, zones: zones.kind_of?(Array) ? zones.join(',') : zones})
75
+ end
76
+
77
+ # This function pulls recent IPs hitting your site.
78
+ #
79
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.5
80
+ #
81
+ # @param zone [String]
82
+ # @param hours [Integer] max 48
83
+ # @param classification [String] (optional) values: r|c|t
84
+ # @param geo [Fixnum] (optional)
85
+
86
+ def zone_ips(zone, classification = nil, hours = 24, geo = 1)
87
+ send_req({a: :zone_ips, z: zone, hours: hours, "class" => classification, geo: geo})
88
+ end
89
+
90
+ # This function checks the threat score for a given IP.
91
+ #
92
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.6
93
+ #
94
+ # @param ip [String]
95
+
96
+ def ip_lkup(ip)
97
+ send_req({a: :ip_lkup, ip: ip})
98
+ end
99
+
100
+ # This function retrieves all current settings for a given domain.
101
+ #
102
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.7
103
+ #
104
+ # @param zone [String]
105
+
106
+ def zone_settings(zone)
107
+ send_req({a: :zone_settings, z: zone})
108
+ end
109
+
110
+ # This function sets the Basic Security Level to HELP I'M UNDER ATTACK / HIGH / MEDIUM / LOW / ESSENTIALLY OFF.
111
+ #
112
+ # @see http://www.cloudflare.com/docs/client-api.html#4.1
113
+ #
114
+ # @param zone [String]
115
+ # @param value [String] values: low|med|high|help|eoff
116
+
117
+ def sec_lvl(zone, value)
118
+ send_req({a: :sec_lvl, z: zone, v: value})
119
+ end
120
+
121
+ # This function sets the Caching Level to Aggressive or Basic.
122
+ #
123
+ # @see http://www.cloudflare.com/docs/client-api.html#4.2
124
+ #
125
+ # @param zone [String]
126
+ # @param value [String] values: agg|basic
127
+
128
+ def cache_lvl(zone, value)
129
+ send_req({a: :cache_lvl, z: zone, v: value})
130
+ end
131
+
132
+ # This function allows you to toggle Development Mode on or off for a particular domain.
133
+ #
134
+ # @see http://www.cloudflare.com/docs/client-api.html#4.3
135
+ #
136
+ # @param zone [String]
137
+ # @param value [Boolean]
138
+
139
+ def devmode(zone, value)
140
+ send_req({a: :devmode, z: zone, v: value ? 1 : 0})
141
+ end
142
+
143
+ # This function will purge CloudFlare of any cached files.
144
+ #
145
+ # @see http://www.cloudflare.com/docs/client-api.html#4.4
146
+ #
147
+ # @param zone [String]
148
+
149
+ def fpurge_ts(zone)
150
+ send_req({a: :fpurge_ts, z: zone, v: 1})
151
+ end
152
+
153
+ # This function will purge a single file from CloudFlare's cache.
154
+ #
155
+ # @see http://www.cloudflare.com/docs/client-api.html#4.5
156
+ #
157
+ # @param zone [String]
158
+ # @param url [String]
159
+
160
+ def zone_file_purge(zone, url)
161
+ send_req({a: :zone_file_purge, z: zone, url: url})
162
+ end
163
+
164
+ # This function updates the snapshot of your site for CloudFlare's challenge page.
165
+ #
166
+ # @see http://www.cloudflare.com/docs/client-api.html#4.6
167
+ #
168
+ # @param zoneid [Integer]
169
+
170
+ def zone_grab(zoneid)
171
+ send_req({a: :zone_grab, zid: zoneid})
172
+ end
173
+
174
+ # This function adds an IP address to your white lists.
175
+ #
176
+ # @see http://www.cloudflare.com/docs/client-api.html#4.7
177
+ #
178
+ # @param ip [String]
179
+
180
+ def whitelist(ip)
181
+ send_req({a: :wl, key: ip})
182
+ end
183
+
184
+
185
+ # This function adds an IP address to your black lists.
186
+ #
187
+ # @see http://www.cloudflare.com/docs/client-api.html#4.7
188
+ #
189
+ # @param ip [String]
190
+
191
+ def blacklist(ip)
192
+ send_req({a: :ban, key: ip})
193
+ end
194
+
195
+ # This function removes the IP from whitelist or blacklist.
196
+ #
197
+ # @see http://www.cloudflare.com/docs/client-api.html#4.7
198
+ #
199
+ # @param ip [String]
200
+
201
+ def remove_ip(ip)
202
+ send_req({a: :nul, key: ip})
203
+ end
204
+
205
+ # This function toggles IPv6 support.
206
+ #
207
+ # @see http://www.cloudflare.com/docs/client-api.html#4.8
208
+ #
209
+ # @param zone [String]
210
+ # @param value [Boolean]
211
+
212
+ def ipv46(zone, value)
213
+ send_req({a: :ipv46, z: zone, v: value ? 1 : 0})
214
+ end
215
+
216
+ # This function changes Rocket Loader setting.
217
+ #
218
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.9
219
+ #
220
+ # @param zone [String]
221
+ # @param value [Integer or String] values: 0|a|m
222
+
223
+ def async(zone, value)
224
+ send_req({a: :async, z: zone, v: value})
225
+ end
226
+
227
+ # This function changes minification settings.
228
+ #
229
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.9
230
+ #
231
+ # @param zone [String]
232
+ # @param value [Integer] values: 0|2|3|4|5|6|7
233
+
234
+ def minify(zone, value)
235
+ send_req({a: :minify, z: zone, v: value})
236
+ end
237
+
238
+ # This function creates a new DNS record for your site. This can be either a CNAME or A record.
239
+ #
240
+ # @see http://www.cloudflare.com/docs/client-api.html#s5.1
241
+ #
242
+ # @param zone [String]
243
+ # @param type [String] values: A|CNAME|MX|TXT|SPF|AAAA|NS|SRV|LOC
244
+ # @param name [String]
245
+ # @param content [String]
246
+ # @param ttl [Integer] values: 1|120...4294967295
247
+ # @param prio [Integer] (applies to MX/SRV)
248
+ # @param service [String] (applies to SRV)
249
+ # @param srvname [String] (applies to SRV)
250
+ # @param protocol [Integer] (applies to SRV) values: _tcp|_udp|_tls
251
+ # @param weight [Intger] (applies to SRV)
252
+ # @param port [Integer] (applies to SRV)
253
+ # @param target [String] (applies to SRV)
254
+
255
+ def rec_new(zone, type, name, content, ttl, prio = nil, service = nil, srvname = nil, protocol = nil, weight = nil, port = nil, target = nil)
256
+ send_req({
257
+ a: :rec_new,
258
+ zone: zone,
259
+ type: type,
260
+ name: name,
261
+ content: content,
262
+ ttl: ttl,
263
+ prio: prio,
264
+ service: service,
265
+ srvname: srvname,
266
+ protocol: protocol,
267
+ weight: weight,
268
+ port: port,
269
+ target: target
270
+ })
271
+ end
272
+
273
+ # This function edits a DNS record for a zone.
274
+ #
275
+ # @see http://www.cloudflare.com/docs/client-api.html#s5.2
276
+ #
277
+ # @param zone [String]
278
+ # @param type [String] values: A|CNAME|MX|TXT|SPF|AAAA|NS|SRV|LOC
279
+ # @param zoneid [Integer]
280
+ # @param name [String]
281
+ # @param content [String]
282
+ # @param ttl [Integer] values: 1|120...4294967295
283
+ # @param service_mode [Boolean] (applies to A/AAAA/CNAME)
284
+ # @param prio [Integer] (applies to MX/SRV)
285
+ # @param service [String] (applies to SRV)
286
+ # @param srvname [String] (applies to SRV)
287
+ # @param protocol [Integer] (applies to SRV) values: _tcp/_udp/_tls
288
+ # @param weight [Intger] (applies to SRV)
289
+ # @param port [Integer] (applies to SRV)
290
+ # @param target [String] (applies to SRV)
291
+
292
+ def rec_edit(zone, type, zoneid, name, content, ttl, service_mode = nil, prio = nil, service = nil, srvname = nil, protocol = nil, weight = nil, port = nil, target = nil)
293
+ send_req({
294
+ a: :rec_new,
295
+ z: zone,
296
+ type: type,
297
+ id: zoneid,
298
+ name: name,
299
+ content: content,
300
+ ttl: ttl,
301
+ service_mode: service_mode ? 1 : 0,
302
+ prio: prio,
303
+ service: service,
304
+ srvname: srvname,
305
+ protocol: protocol,
306
+ weight: weight,
307
+ port: port,
308
+ target: target
309
+ })
310
+ end
311
+
312
+ # This functon deletes a record for a domain.
313
+ #
314
+ # @see http://www.cloudflare.com/docs/client-api.html#s5.3
315
+ #
316
+ # @param zone [String]
317
+ # @param zoneid [Integer]
318
+
319
+ def rec_delete(zone, zoneid)
320
+ send_req({a: :rec_delete, z: zone, id: zoneid})
321
+ end
322
+
323
+ # HOST
324
+
325
+ # This function creates a CloudFlare account mapped to your user.
326
+ #
327
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.1
328
+ #
329
+ # @param email [String]
330
+ # @param pass [String]
331
+ # @param login [String] (optional) cloudflare_username
332
+ # @param id [Integer] (optional) unique_id
333
+ # @param cui [Integer] (optional) clobber_unique_id
334
+
335
+ def create_user(email, pass, login = nil, id = nil, cui = nil)
336
+ send_req({
337
+ act: :user_create,
338
+ cloudflare_email: email,
339
+ cloudflare_pass: pass,
340
+ cloudflare_username: login,
341
+ unique_id: id,
342
+ clobber_unique_id: cui
343
+ })
344
+ end
345
+
346
+ # This function setups a User's zone for CNAME hosting.
347
+ #
348
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.2
349
+ #
350
+ # @param user_key [String]
351
+ # @param zone [String]
352
+ # @param resolve_to [String]
353
+ # @param subdomains [String or Array]
354
+
355
+ def add_zone(user_key, zone, resolve_to, subdomains)
356
+ send_req({
357
+ act: :zone_set,
358
+ user_key: user_key,
359
+ zone_name: zone,
360
+ resolve_to: resolve_to,
361
+ subdomains: subdomains.kind_of?(Array) ? zones.join(',') : subdomains
362
+ })
363
+ end
364
+
365
+ # This function lookups a user's CloudFlare account information.
366
+ #
367
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.3
368
+ #
369
+ # *Example:*
370
+ #
371
+ # cf = CloudFlare('your_host_key')
372
+ # cf.user_lookup('unique_id', true)
373
+ #
374
+ # If +id+ is set to true, email is a unique_id.
375
+ #
376
+ # @param email [String or Integer]
377
+ # @param id [Boolean]
378
+
379
+ def user_lookup(email, id = false)
380
+ if id
381
+ send_req({act: :user_lookup, unique_id: email})
382
+ else
383
+ send_req({act: :user_lookup, cloudflare_email: email})
384
+ end
385
+ end
386
+
387
+ # This function authorizes access to a user's existing CloudFlare account.
388
+ #
389
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.4
390
+ #
391
+ # @param email [String]
392
+ # @param pass [String]
393
+ # @param unique_id [Integer] (optional)
394
+ # @param cui [Integer] (optional) clobber_unique_id
395
+
396
+ def user_auth(email, pass, id = nil, cui = nil)
397
+ send_req({
398
+ act: :user_auth,
399
+ cloudflare_email: email,
400
+ cloudflare_pass: pass,
401
+ unique_id: id,
402
+ clobber_unique_id: cui
403
+ })
404
+ end
405
+
406
+ # This function lookups a specific user's zone.
407
+ #
408
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.5
409
+ #
410
+ # @param user_key [String]
411
+ # @param zone [String]
412
+
413
+ def zone_lookup(user_key, zone)
414
+ send_req({act: :zone_lookup, user_key: user_key, zone_name: zone})
415
+ end
416
+
417
+ # This function deletes a specific zone on behalf of a user.
418
+ #
419
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.6
420
+ #
421
+ # @param user_key [String]
422
+ # @param zone [String]
423
+
424
+ def del_zone(user_key, zone)
425
+ send_req({act: :zone_delete, user_key: user_key, zone_name: zone})
426
+ end
427
+
428
+ # This function creates a new child host provider.
429
+ #
430
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.7
431
+ #
432
+ # @param host_name [String]
433
+ # @param pub_name [String]
434
+ # @param prefix [String]
435
+ # @param website [String]
436
+ # @param email [String]
437
+
438
+ def host_child_new(host_name, pub_name, prefix, website, email)
439
+ send_req({
440
+ act: :host_child_new,
441
+ pub_name: pub_name,
442
+ prefix: prefix,
443
+ website: website,
444
+ email: email
445
+ })
446
+ end
447
+
448
+ # This function regenerates your host key.
449
+ #
450
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.8
451
+
452
+ def host_key_regen
453
+ send_req(act: :host_key_regen)
454
+ end
455
+
456
+ # This function stops a child host provider account.
457
+ #
458
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.9
459
+ #
460
+ # @param id [Integer] child_id
461
+
462
+ def host_child_stop(id)
463
+ send_req({act: host_child_stop, child_id: id})
464
+ end
465
+
466
+ # This function lists the domains currently active on CloudFlare for the given host.
467
+ #
468
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.10
469
+ #
470
+ # @param limit [Integer] (optional)
471
+ # @param offset [Integer] (optional)
472
+ # @param name [String] (optional) zone_name
473
+ # @param sub_id [Integer] (optional) sub_id
474
+ # @param status [String] (optional) values: V|D|ALL
475
+
476
+ def zone_list(limit = 100, offset = 0, name = nil, sub_id = nil, status = nil)
477
+ send_req({
478
+ act: :zone_list,
479
+ offset: offset,
480
+ zone_name: name,
481
+ sub_id: sub_id,
482
+ zone_status: status
483
+ })
484
+ end
485
+
486
+ private
487
+
488
+ def send_req(params)
489
+
490
+ if @params[:email]
491
+ params[:tkn] = @params[:api_key]
492
+ params[:u] = @params[:email]
493
+ uri = URI(URL_API[:client])
494
+ else
495
+ params[:host_key] = @params[:api_key]
496
+ uri = URI(URL_API[:host])
497
+ end
498
+
499
+ req = Net::HTTP::Post.new(uri.path)
500
+ req.set_form_data(params)
501
+
502
+ http = Net::HTTP.new(uri.host, uri.port)
503
+ http.use_ssl = true
504
+ http.read_timeout = TIMEOUT
505
+
506
+ begin
507
+ res = http.request(req)
508
+ JSON.parse(res.body)
509
+ rescue => e
510
+ puts "#{e.class} #{e.message}"
511
+ end
512
+
513
+ end
514
+
515
+ end
@@ -5,7 +5,7 @@ class HostTest < Test::Unit::TestCase
5
5
 
6
6
  def test_client_connection
7
7
  cf = CloudFlare.new('example_api', 'example@example.com')
8
- assert_equal('E_UNAUTH', cf.toggle_ipv6('example.com', true)['err_code'])
8
+ assert_equal('E_UNAUTH', cf.ipv46('example.com', true)['err_code'])
9
9
  end
10
10
 
11
11
  def test_host_connection
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudflare
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-30 00:00:00.000000000 Z
12
+ date: 2013-02-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &8532060 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *8532060
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  description: A Ruby wrapper for the CloudFlare API.
26
31
  email: marcin@prokop.co
27
32
  executables: []
@@ -35,7 +40,7 @@ files:
35
40
  - test/test_cloudflare.rb
36
41
  - README.md
37
42
  - LICENSE
38
- homepage: http://b4k3r.github.com/cloudflare/
43
+ homepage: https://github.com/B4k3r/cloudflare
39
44
  licenses: []
40
45
  post_install_message:
41
46
  rdoc_options:
@@ -58,10 +63,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
63
  version: '0'
59
64
  requirements: []
60
65
  rubyforge_project:
61
- rubygems_version: 1.8.11
66
+ rubygems_version: 1.8.25
62
67
  signing_key:
63
68
  specification_version: 3
64
69
  summary: A Ruby wrapper for the CloudFlare API.
65
70
  test_files:
66
71
  - test/test_cloudflare.rb
67
- has_rdoc: