class Fastly

Top-level Fastly class

def self.get_options(*files)


--=

of the form
Then it overrides those options with command line options

C, stopping when it finds the first one.
Tries to load options from the file[s] passed in using,
#
def self.get_options(*files)
  options = {}
  files.each do |file|
    next unless File.exist?(file)
    options = load_config(file)
    break
  end
  while ARGV.size > 0 && ARGV[0] =~ /^-+(\w+)\=(\w+)$/
    options[$1.to_sym] = $2
    ARGV.shift
  end
  fail "Couldn't find options from command line arguments or #{files.join(', ')}" unless options.size > 0
  options
end

def self.load_config(file)


Skips whitespace and lines starting with C<#>.

From a file.

=

Attempts to load various config options in the form
#
def self.load_config(file)
  options = {}
  return options unless File.exist?(file)
  File.open(file, 'r') do |infile|
    while line = infile.gets
      line.chomp!
      next if line =~ /^#/
      next if line =~ /^\s*$/
      next unless line =~ /=/
      line.strip!
      key, val = line.split(/\s*=\s*/, 2)
      options[key.to_sym] = val
    end
  end
  options
end

def authed?

Whether or not we're authed at all by either username & password or API key
def authed?
  client.authed?
end

def current_customer

Return a Customer object representing the customer of the current logged in user.
def current_customer
  fail AuthRequired unless authed?
  @current_customer ||= get(Customer)
end

def current_user

NOTE: requires you to be fully authed - will not work with only an API key
Return a User object representing the current logged in user.
def current_user
  fail FullAuthRequired unless fully_authed?
  @current_user ||= get(User)
end

def fully_authed?

Some methods require full username and password rather than just auth token
Whether or not we're fully (username and password) authed
def fully_authed?
  client.fully_authed?
end

def get_invoice(year = nil, month = nil)

Otherwise it returns the invoices for the current month so far.

If a year and month are passed in returns the invoices for that whole month.

Return an Invoice object
def get_invoice(year = nil, month = nil)
  opts = { customer_id: current_customer.id }
  if year.nil? || month.nil?
    opts[:mtd] = true
  else
    opts[:year]  = year
    opts[:month] = month
  end
  get(Invoice, opts)
end

def get_invoice_by_id(id)

Return an Invoice object for the passed invoice ID
def get_invoice_by_id(id)
  opts = {
    id: id,
    customer_id: current_customer.id
  }
  get(Invoice, opts)
end

def get_settings(service, number)

Get the Settings object for the specified Version
def get_settings(service, number)
  hash = client.get(Settings.get_path(service, number))
  return nil if hash.nil?
  hash['settings'] = Hash[['general.default_host', 'general.default_ttl'].collect { |var| [var, hash.delete(var)] }]
  Settings.new(hash, self)
end

def initialize(opts)

Some methods require full username and password rather than just auth token.

You only need to pass in C OR C and C.

api_key:: your Fastly api key
password:: your Fastly password
user:: your Fastly login

Create a new Fastly client. Options are
def initialize(opts)
  if opts[:api_key].nil? && (opts[:password].nil? || opts[:user].nil?)
    raise ArgumentError, "Required options missing. Please pass either ':api_key' or both ':user' and ':password'."
  end
  client(opts)
  self
end

def list_invoices

Retun an array of Invoice objects.
def list_invoices
  opts = { customer_id: current_customer.id }
  list(Invoice, opts)
end

def purge(url, soft=false)

Purge the specified path from your cache.
def purge(url, soft=false)
  client.purge(url, soft ? { headers: { 'Fastly-Soft-Purge' => "1"} } : {})
end

def regions

Fetches the list of codes for regions that are covered by the Fastly CDN service.
def regions
  client.get_stats('/stats/regions')
end

def search_services(opts)

service = fastly.search_services(:name => name, :version => number)

or

services = fastly.search_services(:name => name)

In general you'll want to do

Search all the services that the current customer has.
def search_services(opts)
  hash = client.get("#{Service.post_path}/search", opts)
  hash.nil? ? nil : Service.new(hash, self)
end

def stats(opts)

See http://docs.fastly.com/docs/stats for details.

region:: restrict query to a particular region
by:: the sampling rate used to produce the result set (minute, hour, day)
to:: latest time from which to fetch historical statistics
from:: earliest time from which to fetch historical statistics

Other options available are:

If you pass in an :aggregate flag then fetches historical stats information aggregated across all of your Fastly services. This cannot be combined with :field and :service.

The :field and :service opts can be combined.
If you pass in a :service opt then fetches only the specified service.
If you pass in a :field opt then fetches only the specified field.

Fetches historical stats for each of your fastly services and groups the results by service id.
def stats(opts)
  if opts[:aggregate] && (opts[:field] || opts[:service])
    fail Error, "You can't specify a field or a service for an aggregate request"
  end
  url  = '/stats'
  url += '/aggregate' if opts.delete(:aggregate)
  if service = opts.delete(:service)
    url += "/service/#{service}"
  end
  if field = opts.delete(:field)
    url += "/field/#{field}"
  end
  client.get_stats(url, opts)
end

def update_settings(opts = {})

Update the Settings object for the specified Version
def update_settings(opts = {})
  update(Settings, opts)
end

def usage(opts)

See http://docs.fastly.com/docs/stats for details.

region:: restrict query to a particular region
by:: the sampling rate used to produce the result set (minute, hour, day)
to:: latest time from which to fetch historical statistics
from:: earliest time from which to fetch historical statistics

Other options available are:

If the :by_service flag is passed then returns usage information aggregated by service and grouped by service & region.

If the :by_month flag is passed then returns total usage information aggregated by month as well as grouped by service & region.

Returns usage information aggregated across all Fastly services and grouped by region.
def usage(opts)
  url  = '/stats/usage'
  url += '_by_month' if opts.delete(:by_month)
  url += '_by_service' if opts.delete(:by_service)
  client.get_stats(url, opts)
end