Class: Cloudflarer::CLI

Inherits:
Ordu
  • Object
show all
Defined in:
lib/cloudflarer/cli.rb

Instance Method Summary collapse

Instance Method Details

#create(path) ⇒ Object

Creates a record (using params)



113
114
115
# File 'lib/cloudflarer/cli.rb', line 113

def create(path)
  output { post(path, params) }
end

#debug(msg, &block) ⇒ Object

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



214
215
216
217
218
# File 'lib/cloudflarer/cli.rb', line 214

def debug(msg, &block)
  result = yield
  puts "-- #{msg} #{'-' * (74 - msg.length)}" if $debug
  result
end

#delete(path) ⇒ Object

Destroys a resource



153
154
155
# File 'lib/cloudflarer/cli.rb', line 153

def delete(path)
  time("DELETE #{path}") { Cloudflarer.new.delete(path) }
end

#destroy(path) ⇒ Object

Deletes and shows a resource



128
129
130
# File 'lib/cloudflarer/cli.rb', line 128

def destroy(path)
  output { delete(path) }
end

#die(msg, code = 1) ⇒ Object

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



221
222
223
224
# File 'lib/cloudflarer/cli.rb', line 221

def die(msg, code = 1)
  STDERR.puts msg
  exit code
end

#filter(&block) ⇒ Object

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



197
198
199
200
201
# 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(&block) ⇒ Object

Gets the formatter



174
175
176
177
178
179
180
181
182
# 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) ⇒ Object

Gets a resource



138
139
140
# File 'lib/cloudflarer/cli.rb', line 138

def get(path)
  time("GET #{path}") { Cloudflarer.new.get(path) }
end

#list(path) ⇒ Object

Gets and lists multiple resources



133
134
135
# File 'lib/cloudflarer/cli.rb', line 133

def list(path)
  output { get(path) }
end

#output(&block) ⇒ Object

Outputs in the format requested



204
205
206
207
208
209
210
211
# 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

#paramsObject

A place to gather params for queries



103
104
105
# File 'lib/cloudflarer/cli.rb', line 103

def params
  @params ||= {}
end

#patch(path, params) ⇒ Object

Updates a resource (using params)



143
144
145
# File 'lib/cloudflarer/cli.rb', line 143

def patch(path, params)
  time("PATCH #{path}") { Cloudflarer.new.patch(path, params) }
end

#post(path, params) ⇒ Object

Creates a resource (using params)



148
149
150
# File 'lib/cloudflarer/cli.rb', line 148

def post(path, params)
  time("POST #{path}") { Cloudflarer.new.post(path, params) }
end

#render(template, object) ⇒ Object

Renders the given object through the Mustache template



191
192
193
194
# 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 = {}) ⇒ Object

Sets a param for a query (used by options)



108
109
110
# File 'lib/cloudflarer/cli.rb', line 108

def set(params = {})
  self.params.merge!(params)
end

#show(path) ⇒ Object

Gets and shows a single resource



123
124
125
# File 'lib/cloudflarer/cli.rb', line 123

def show(path)
  output { get(path) }
end

#template(template = nil) ⇒ Object

Gets the template with which to present the object



185
186
187
188
# File 'lib/cloudflarer/cli.rb', line 185

def template(template = nil)
  $template ||= template
  $template || '{{id}} {{name}}'
end

#time(msg, &block) ⇒ Object

Times the block, which should return something with a status



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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) ⇒ Object

Updates a record (using params)



118
119
120
# File 'lib/cloudflarer/cli.rb', line 118

def update(path)
  output { patch(path, params) }
end

#zone {|@zone| ... } ⇒ Object

Yields the zone popped from the params, or dies

Yields:



96
97
98
99
100
# 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