class CloudflareClient

General class for the client

Constants

API_BASE
POSSIBLE_API_SETTINGS
VALID_BUNDLE_METHODS
VALID_DIRECTIONS
VALID_MATCHES
VERSION

Public Class Methods

new(auth_key: nil, email: nil, auth_token: nil, &block) click to toggle source
# File lib/cloudflare_client.rb, line 54
def initialize(auth_key: nil, email: nil, auth_token: nil, &block)
  raise('Missing auth_key or auth_token') if auth_key.nil? && auth_token.nil?
  raise('missing email') if email.nil? && !auth_key.nil?
  @cf_client ||= build_client(auth_key: auth_key, email: email, auth_token: auth_token, &block)
end

Private Instance Methods

basic_type_check(name, value, type) click to toggle source
# File lib/cloudflare_client.rb, line 115
def basic_type_check(name, value, type)
  raise "#{name} must be a #{type}" unless value.is_a?(type)
end
build_client(params) { |client| ... } click to toggle source
# File lib/cloudflare_client.rb, line 133
def build_client(params)
  # we need multipart form encoding for some of these operations
  client                                      = Faraday.new(url: API_BASE) do |conn|
    conn.request :multipart
    conn.request :url_encoded
    conn.use Middleware::Response::RaiseError
  end

  client.headers['Content-Type']              = 'application/json'
  if params[:auth_token]
    client.headers['Authorization'] = "Bearer #{params[:auth_token]}"
  else
    client.headers['X-Auth-Key']                = params[:auth_key]
    client.headers['X-Auth-User-Service-Key   ']  = params[:auth_key] #FIXME, is this always the same?
    client.headers['X-Auth-Email']              = params[:email]
  end

  yield client if block_given?
  client.adapter :net_http
  client
end
bundle_method_check(bundle_method) click to toggle source

TODO: cloudflare IPs TODO: AML TODO: load balancer monitors TODO: load balancer pools TODO: org load balancer monitors TODO: org load balancer pools TODO: load balancers

# File lib/cloudflare_client.rb, line 72
def bundle_method_check(bundle_method)
  unless VALID_BUNDLE_METHODS.include?(bundle_method)
    raise("valid bundle methods are #{VALID_BUNDLE_METHODS.flatten}")
  end
end
cf_delete(path: nil, data: {}) click to toggle source
# File lib/cloudflare_client.rb, line 199
def cf_delete(path: nil, data: {})
  result = @cf_client.delete do |request|
    request.url(API_BASE + path) unless path.nil?
    request.body = data.to_json unless data.empty?
  end
  JSON.parse(result.body, symbolize_names: true)
end
cf_get(path: nil, params: {}, raw: nil, extra_headers: {}) click to toggle source
# File lib/cloudflare_client.rb, line 164
def cf_get(path: nil, params: {}, raw: nil, extra_headers: {})
  result = @cf_client.get do |request|
    request.headers.merge!(extra_headers) unless extra_headers.empty?
    request.url(API_BASE + path) unless path.nil?
    unless params.nil?
      request.params = params if params.values.any? { |i| !i.nil? }
    end
  end
  unless raw.nil?
    return result.body
  end
  # we ask for compressed logs.  uncompress if we get them
  # as the user can always ask for raw stuff
  if result.headers["content-encoding"] == 'gzip'
    return Zlib::GzipReader.new(StringIO.new(result.body.to_s)).read
  end
  JSON.parse(result.body, symbolize_names: true)
end
cf_patch(path: nil, data: {}) click to toggle source
# File lib/cloudflare_client.rb, line 191
def cf_patch(path: nil, data: {})
  result               = @cf_client.patch do |request|
    request.url(API_BASE + path) unless path.nil?
    request.body = data.to_json unless data.empty?
  end
  JSON.parse(result.body, symbolize_names: true)
end
cf_post(path: nil, data: {}) click to toggle source
# File lib/cloudflare_client.rb, line 155
def cf_post(path: nil, data: {})
  raise('No data to post') if data.empty?
  result = @cf_client.post do |request|
    request.url(API_BASE + path) unless path.nil?
    request.body = data.to_json
  end
  JSON.parse(result.body, symbolize_names: true)
end
cf_put(path: nil, data: nil) click to toggle source
# File lib/cloudflare_client.rb, line 183
def cf_put(path: nil, data: nil)
  result = @cf_client.put do |request|
    request.url(API_BASE + path) unless path.nil?
    request.body = data.to_json unless data.nil?
  end
  JSON.parse(result.body, symbolize_names: true)
end
date_rfc3339?(ts) click to toggle source
# File lib/cloudflare_client.rb, line 78
def date_rfc3339?(ts)
  begin
    DateTime.rfc3339(ts)
  rescue ArgumentError
    return false
  end
  true
end
id_check(name, id) click to toggle source
# File lib/cloudflare_client.rb, line 99
def id_check(name, id)
  raise "#{name} required" if id.nil?
end
iso8601_check(name, ts) click to toggle source
# File lib/cloudflare_client.rb, line 87
def iso8601_check(name, ts)
  DateTime.iso8601(ts)
rescue ArgumentError
  raise "#{name} must be a valid iso8601 timestamp"
end
max_length_check(name, value, max_length=32) click to toggle source
# File lib/cloudflare_client.rb, line 119
def max_length_check(name, value, max_length=32)
  raise "the length of #{name} must not exceed #{max_length}" unless value.length <= max_length
end
non_empty_array_check(name, array) click to toggle source
# File lib/cloudflare_client.rb, line 107
def non_empty_array_check(name, array)
  raise "#{name} must be an array of #{name}" unless array.is_a?(Array) && !array.empty?
end
non_empty_hash_check(name, hash) click to toggle source
# File lib/cloudflare_client.rb, line 111
def non_empty_hash_check(name, hash)
  raise "#{name} must be an hash of #{name}" unless hash.is_a?(Hash) && !hash.empty?
end
range_check(name, value, min=nil, max=nil) click to toggle source
# File lib/cloudflare_client.rb, line 123
def range_check(name, value, min=nil, max=nil)
  if min && max
    raise "#{name} must be between #{min} and #{max}" unless value >= min && value <= max
  elsif min
    raise "#{name} must be equal or larger than #{min}" unless value >= min
  elsif max
    raise "#{name} must be equal or less than #{max}" unless value <= max
  end
end
timestamp_check(name, ts) click to toggle source
# File lib/cloudflare_client.rb, line 93
def timestamp_check(name, ts)
  Time.at(ts).to_datetime
rescue TypeError
  raise "#{name} must be a valid unix timestamp"
end
valid_setting?(name = nil) click to toggle source
# File lib/cloudflare_client.rb, line 207
def valid_setting?(name = nil)
  return false if name.nil?
  return false unless POSSIBLE_API_SETTINGS.include?(name)
  true
end
valid_value_check(name, value, valid_values) click to toggle source
# File lib/cloudflare_client.rb, line 103
def valid_value_check(name, value, valid_values)
  raise "#{name} must be one of #{valid_values}" unless valid_values.include?(value)
end