class Fastly
Top-level Fastly class
def self.get_options(*files)
--
of the form
Then it overrides those options with command line options
C
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?
def authed? client.authed? end
def current_customer
def current_customer fail AuthRequired unless authed? @current_customer ||= get(Customer) end
def current_user
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?
Whether or not we're fully (username and password) authed
def fully_authed? client.fully_authed? end
def get_invoice(year = nil, month = nil)
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)
def get_invoice_by_id(id) opts = { id: id, customer_id: current_customer.id } get(Invoice, opts) end
def get_settings(service, number)
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)
You only need to pass in 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
def list_invoices opts = { customer_id: current_customer.id } list(Invoice, opts) end
def purge(url, soft=false)
def purge(url, soft=false) client.purge(url, soft ? { headers: { 'Fastly-Soft-Purge' => "1"} } : {}) end
def regions
def regions client.get_stats('/stats/regions') end
def search_services(opts)
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)
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 = {})
def update_settings(opts = {}) update(Settings, opts) end
def usage(opts)
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