class Cloudflarer::CLI

Constants

CLOUDFLARE_API_KEY
CLOUDFLARE_EMAIL

Public Instance Methods

create(path) click to toggle source

Creates a record (using params)

# File lib/cloudflarer/cli.rb, line 113
def create(path)
  output { post(path, params) }
end
debug(msg) { || ... } click to toggle source

Print the result of the block, if $debug is on

# File lib/cloudflarer/cli.rb, line 214
def debug(msg, &block)
  result = yield
  puts "-- #{msg} #{'-' * (74 - msg.length)}" if $debug
  result
end
delete(path) click to toggle source

Destroys a resource

# File lib/cloudflarer/cli.rb, line 153
def delete(path)
  time("DELETE #{path}") { Cloudflarer.new.delete(path) }
end
destroy(path) click to toggle source

Deletes and shows a resource

# File lib/cloudflarer/cli.rb, line 128
def destroy(path)
  output { delete(path) }
end
die(msg, code = 1) click to toggle source

Print the message to STDERR and exit (non-zero)

# File lib/cloudflarer/cli.rb, line 221
def die(msg, code = 1)
  STDERR.puts msg
  exit code
end
filter() { || ... } click to toggle source

Filters results, if required. Currently doesn't do anything.

# File lib/cloudflarer/cli.rb, line 197
def filter(&block)
  result = yield
  return result.map { |o| filter { o } } if result.is_a?(Array)
  result
end
format() { || ... } click to toggle source

Gets the formatter

# File lib/cloudflarer/cli.rb, line 174
def format(&block)
  object = yield
  return object if object.is_a?(String)
  return object.to_yaml if template == 'yaml'
  return object.to_json if template == 'json'
  return render(template, object) if template.is_a?(String)
  return tablualte { yield } if template.nil?
  raise "Invalid template: #{template}"
end
get(path) click to toggle source

Gets a resource

# File lib/cloudflarer/cli.rb, line 138
def get(path)
  time("GET #{path}") { Cloudflarer.new.get(path) }
end
list(path) click to toggle source

Gets and lists multiple resources

# File lib/cloudflarer/cli.rb, line 133
def list(path)
  output { get(path) }
end
output() { || ... } click to toggle source

Outputs in the format requested

# File lib/cloudflarer/cli.rb, line 204
def output(&block)
  response = yield
  if response.fetch('success')
    puts format { filter { response.fetch('result') } }
  else
    puts format { response.fetch('error') }
  end
end
params() click to toggle source

A place to gather params for queries

# File lib/cloudflarer/cli.rb, line 103
def params
  @params ||= {}
end
patch(path, params) click to toggle source

Updates a resource (using params)

# File lib/cloudflarer/cli.rb, line 143
def patch(path, params)
  time("PATCH #{path}") { Cloudflarer.new.patch(path, params) }
end
post(path, params) click to toggle source

Creates a resource (using params)

# File lib/cloudflarer/cli.rb, line 148
def post(path, params)
  time("POST #{path}") { Cloudflarer.new.post(path, params) }
end
render(template, object) click to toggle source

Renders the given object through the Mustache template

# File lib/cloudflarer/cli.rb, line 191
def render(template, object)
  return object.map { |o| render(template, o) } if object.is_a?(Array)
  Mustache.render(template, object)
end
set(params = {}) click to toggle source

Sets a param for a query (used by options)

# File lib/cloudflarer/cli.rb, line 108
def set(params = {})
  self.params.merge!(params)
end
show(path) click to toggle source

Gets and shows a single resource

# File lib/cloudflarer/cli.rb, line 123
def show(path)
  output { get(path) }
end
template(template = nil) click to toggle source

Gets the template with which to present the object

# File lib/cloudflarer/cli.rb, line 185
def template(template = nil)
  $template ||= template
  $template || '{{id}} {{name}}'
end
time(msg) { || ... } click to toggle source

Times the block, which should return something with a status

# File lib/cloudflarer/cli.rb, line 158
def time(msg, &block)
  return(yield) unless $verbose
  print "#{msg}..."
  t = Time.now.to_f
  response = yield
  print "(%0.2f ms) " % (Time.now.to_f - t)
  if info = response['result_info']
    print "[%s/%s/%s/%s] " %
      info.values_at(*%w(page per_page count total_count)).map(&:to_s)
  end
  puts "OK" if response.fetch('success')
  puts "FAIL" unless response.fetch('success')
  response
end
update(path) click to toggle source

Updates a record (using params)

# File lib/cloudflarer/cli.rb, line 118
def update(path)
  output { patch(path, params) }
end
zone() { |zone| ... } click to toggle source

Yields the zone popped from the params, or dies

# File lib/cloudflarer/cli.rb, line 96
def zone(&block)
  @zone ||= params.delete(:zone)
  die('You need to specify the zone (-z)') unless @zone
  yield @zone
end