class Fastly

Top-level Fastly class

The current version of the library

Invoice object

Service object

Settings Object

Token object

Constants

VERSION

Attributes

check_interval[RW]

Time between checks in milliseconds

expected_response[RW]

The HTTP status to indicate a successful healthcheck (e.g. 200)

initial[RW]

How many have to be ok for it work the first time

method[RW]

The HTTP method to use: GET, PUT, POST etc.

Public Class Methods

get_options(*files) click to toggle source

Tries to load options from the file passed in using, C<load_options>, stopping when it finds the first one.

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

--<key>=<value>
# File lib/fastly.rb, line 744
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
load_config(file) click to toggle source

Attempts to load various config options in the form

<key> = <value>

From a file.

Skips whitespace and lines starting with C<#>.

# File lib/fastly.rb, line 716
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
new(opts) click to toggle source

Create a new Fastly client. Options are

user

your Fastly login

password

your Fastly password

api_key

your Fastly api key

You only need to pass in C<api_key> OR C<user> and C<password>.

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

# File lib/fastly.rb, line 57
def initialize(opts)
  if opts[:api_key].nil?
    raise ArgumentError, "Required option missing. Please pass ':api_key'."
  end

  client(opts)
  self
end

Public Instance Methods

authed?() click to toggle source

Whether or not we're authed at all by either username & password or API key

# File lib/fastly.rb, line 67
def authed?
  client.authed?
end
create_backend(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 225
  
create_cache_setting(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 273
  
create_condition(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 269
  
create_dictionary(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 229
  
create_dictionary_item(opts)() click to toggle source

opts must contain service_id, dictionary_id, item_key and item_value params

# File lib/fastly.rb, line 233
  
create_director(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 237
  
create_domain(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 241
  
create_gzip(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 281
  
create_header(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 277
  
create_healthcheck(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 249
  
create_match(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 245
  
create_papertrail_logging(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 257
  
create_request_setting(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 285
  
create_response_object(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 289
  
create_s3_logging(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 253
  
create_syslog(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 261
  
create_vcl(opts)() click to toggle source

opts must contain service_id, version and name params

# File lib/fastly.rb, line 265
  
create_version(opts)() click to toggle source

opts must contain a service_id param

# File lib/fastly.rb, line 221
  
current_customer() click to toggle source

Return a Customer object representing the customer of the current logged in user.

# File lib/fastly.rb, line 78
def current_customer
  fail AuthRequired unless authed?
  @current_customer ||= get(Customer)
end
current_user() click to toggle source

Return a User object representing the current logged in user.

# File lib/fastly.rb, line 84
def current_user
  @current_user ||= get(User)
end
customer_tokens(opts) click to toggle source
# File lib/fastly/token.rb, line 36
def customer_tokens(opts)
  hash = client.get("/customer/#{opts[:customer_id]}/tokens")
  hash.map { |token_hash| Token.new(token_hash, Fastly::Fetcher) }
end
delete_backend(backend)() click to toggle source

You can also call

backend.delete!
# File lib/fastly.rb, line 529
  
delete_cache_setting(cache_setting)() click to toggle source

You can also call

cache_setting.delete!
# File lib/fastly.rb, line 589
  
delete_condition(condition)() click to toggle source

You can also call

condition.delete!
# File lib/fastly.rb, line 614
  
delete_customer(customer)() click to toggle source

You can also call

customer.delete!
# File lib/fastly.rb, line 514
  
delete_dictionary(dictionary)() click to toggle source

You can also call

dictionary.delete!
# File lib/fastly.rb, line 534
  
delete_dictionary_item(dictionary_item)() click to toggle source

You can also call

dictionary_item.delete!
# File lib/fastly.rb, line 539
  
delete_director(backend)() click to toggle source

You can also call

backend.delete!
# File lib/fastly.rb, line 544
  
delete_domain(domain() click to toggle source

You can also call

domain.delete!
# File lib/fastly.rb, line 549
  
delete_gzip(gzip)() click to toggle source

You can also call

gzip.delete!
# File lib/fastly.rb, line 599
  
delete_header(header)() click to toggle source

You can also call

header.delete!
# File lib/fastly.rb, line 594
  
delete_healthcheck(healthcheck)() click to toggle source

You can also call

healthcheck.delete!
# File lib/fastly.rb, line 554
  
delete_match(match)() click to toggle source

You can also call

match.delete!(match)
# File lib/fastly.rb, line 559
  
delete_papertrail_logging(papertrail_logging)() click to toggle source

You can also call

papertrail_logging.delete!
# File lib/fastly.rb, line 569
  
delete_request_setting(request_setting)() click to toggle source

You can also call

request_setting.delete!
# File lib/fastly.rb, line 604
  
delete_response_object(response_object)() click to toggle source

You can also call

response_object.delete!
# File lib/fastly.rb, line 609
  
delete_s3_logging(s3_logging)() click to toggle source

You can also call

s3_logging.delete!
# File lib/fastly.rb, line 564
  
delete_service(service)() click to toggle source

You can also call

service.delete!
# File lib/fastly.rb, line 519
  
delete_snippet(snippet)() click to toggle source

You can also call

snippet.delete!
# File lib/fastly.rb, line 584
  
delete_syslog(syslog)() click to toggle source

You can also call

syslog.delete!
# File lib/fastly.rb, line 574
  
delete_user(user)() click to toggle source

You can also call

user.delete!
# File lib/fastly.rb, line 509
  
delete_vcl(vcl)() click to toggle source

You can also call

vcl.delete!
# File lib/fastly.rb, line 579
  
delete_version(version)() click to toggle source

You can also call

version.delete!
# File lib/fastly.rb, line 524
  
fully_authed?() click to toggle source

Whether or not we're fully (username and password) authed Some methods require full username and password rather than just auth token

# File lib/fastly.rb, line 73
def fully_authed?
  client.fully_authed?
end
get_backend(service_id,() click to toggle source

Get a backend

# File lib/fastly.rb, line 309
  
get_cache_setting(service_id,() click to toggle source

Get a Cache Setting

# File lib/fastly.rb, line 369
  
get_condition(service_id,() click to toggle source

Get a Condition

# File lib/fastly.rb, line 365
  
get_customer(id)() click to toggle source

Get a customer

# File lib/fastly.rb, line 297
  
get_dictionary(service_id,() click to toggle source

Get a single dictionary

# File lib/fastly.rb, line 313
  
get_dictionary_item(service_id,() click to toggle source

Get a single dictionary item

# File lib/fastly.rb, line 317
  
get_director(service_id,() click to toggle source

Get a Director

# File lib/fastly.rb, line 321
  
get_domain(service_id,() click to toggle source

Get a Domain

# File lib/fastly.rb, line 325
  
get_gzip(service_id,() click to toggle source

Get a Gzip

# File lib/fastly.rb, line 373
  
get_header(service_id,() click to toggle source

Get a Header

# File lib/fastly.rb, line 377
  
get_healthcheck(service_id,() click to toggle source

Get a Healthcheck

# File lib/fastly.rb, line 329
  
get_invoice(year = nil, month = nil) click to toggle source

Return an Invoice object

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

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

# File lib/fastly/invoice.rb, line 92
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
get_invoice_by_id(id) click to toggle source

Return an Invoice object for the passed invoice ID

# File lib/fastly/invoice.rb, line 105
def get_invoice_by_id(id)
  opts = {
    id: id,
    customer_id: current_customer.id
  }

  get(Invoice, opts)
end
get_match(service_id,() click to toggle source

Get a Match

# File lib/fastly.rb, line 333
  
get_papertrail_logging(service_id,() click to toggle source

Get a Papertrail logging stream config

# File lib/fastly.rb, line 341
  
get_request_setting(service_id,() click to toggle source

Get a Request Setting

# File lib/fastly.rb, line 381
  
get_response_object(service_id,() click to toggle source

Get a Response Object

# File lib/fastly.rb, line 385
  
get_s3_logging(service_id,() click to toggle source

Get a S3 logging

# File lib/fastly.rb, line 337
  
get_service(id)() click to toggle source

Get a Service

# File lib/fastly.rb, line 301
  
get_settings(service, number) click to toggle source

Get the Settings object for the specified Version

# File lib/fastly/settings.rb, line 61
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
get_settings(service_id,() click to toggle source

Get a Settings

# File lib/fastly.rb, line 361
  
get_snippet(service_id,() click to toggle source

Get a VCL snippet

# File lib/fastly.rb, line 353
  
get_syslog(service_id,() click to toggle source

Get a Syslog

# File lib/fastly.rb, line 345
  
get_user(id)() click to toggle source

Get a User

# File lib/fastly.rb, line 293
  
get_vcl(service_id,() click to toggle source

Get a VCL

# File lib/fastly.rb, line 349
  
get_version(service_id,() click to toggle source

Get a Version

# File lib/fastly.rb, line 305
  
list_backends(:service_id() click to toggle source

Get a list of all backends

# File lib/fastly.rb, line 639
  
list_cache_settings(:service_id() click to toggle source

Get a list of all cache settings

# File lib/fastly.rb, line 683
  
list_conditions(:service_id() click to toggle source

Get a list of all conditions

# File lib/fastly.rb, line 679
  
list_customers(:service_id() click to toggle source

Get a list of all customers

# File lib/fastly.rb, line 627
  
list_dictionaries(:service_id() click to toggle source

Get a list of all dictionaries

# File lib/fastly.rb, line 647
  
list_dictionary_items(:service_id() click to toggle source

Get a list of all items belonging to a dictionary

# File lib/fastly.rb, line 651
  
list_directors(:service_id() click to toggle source

Get a list of all directors

# File lib/fastly.rb, line 643
  
list_domains(:service_id() click to toggle source

Get a list of all domains

# File lib/fastly.rb, line 655
  
list_gzips(:service_id() click to toggle source

Get a list of all gzips

# File lib/fastly.rb, line 691
  
list_headers(:service_id() click to toggle source

Get a list of all headers

# File lib/fastly.rb, line 687
  
list_healthchecks(:service_id() click to toggle source

Get a list of all healthchecks

# File lib/fastly.rb, line 659
  
list_invoices() click to toggle source

Retun an array of Invoice objects.

# File lib/fastly/invoice.rb, line 115
def list_invoices
  opts = { customer_id: current_customer.id }

  list(Invoice, opts)
end
list_matchs(:service_id() click to toggle source

Get a list of all matches

# File lib/fastly.rb, line 663
  
list_request_settings(:service_id() click to toggle source

Get a list of all request_settings

# File lib/fastly.rb, line 695
  
list_response_objects(:service_id() click to toggle source

Get a list of all response_objects

# File lib/fastly.rb, line 699
  
list_services(:service_id() click to toggle source

Get a list of all services

# File lib/fastly.rb, line 635
  
list_snippets(:service_id() click to toggle source

Get a list of all vcl snippets

# File lib/fastly.rb, line 675
  
list_syslogs(:service_id() click to toggle source

Get a list of all syslogs

# File lib/fastly.rb, line 667
  
list_users(:service_id() click to toggle source

Get a list of all users

# File lib/fastly.rb, line 623
  
list_vcls(:service_id() click to toggle source

Get a list of all vcls

# File lib/fastly.rb, line 671
  
list_versions(:service_id() click to toggle source

Get a list of all versions

# File lib/fastly.rb, line 631
  
new_token(opts) click to toggle source
# File lib/fastly/token.rb, line 23
def new_token(opts)
  if client.fully_authed?
    opts[:username] = client.user
    opts[:password] = client.password
    opts[:include_auth] = false
    
    token = create(Token, opts)
    token.nil? ? nil : token
  else
    raise ArgumentError, "Required options missing. Please pass :api_key, :user and :password." 
  end
end
purge(url, soft=false) click to toggle source

Purge the specified path from your cache.

# File lib/fastly.rb, line 89
def purge(url, soft=false)
  client.purge(url, soft ? { headers: { 'Fastly-Soft-Purge' => "1"} } : {})
end
regions() click to toggle source

Fetches the list of codes for regions that are covered by the Fastly CDN service.

# File lib/fastly.rb, line 151
def regions
  client.get_stats('/stats/regions')
end
search_services(opts) click to toggle source

Search all the services that the current customer has.

In general you'll want to do

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

or

service = fastly.search_services(:name => name, :version => number)
# File lib/fastly/service.rb, line 103
def search_services(opts)
  hash = client.get("#{Service.post_path}/search", opts)
  hash.nil? ? nil : Service.new(hash, self)
end
stats(opts) click to toggle source

Fetches historical stats for each of your fastly services and groups the results by service id.

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

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.

Other options available are:

from

earliest time from which to fetch historical statistics

to

latest time from which to fetch historical statistics

by

the sampling rate used to produce the result set (minute, hour, day)

region

restrict query to a particular region

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

# File lib/fastly.rb, line 109
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
update_backend(backend)() click to toggle source

You can also call

backend.save!
# File lib/fastly.rb, line 409
  
update_cache_setting(cache_setting)() click to toggle source

You can also call

cache_setting.save!
# File lib/fastly.rb, line 474
  
update_condition(condition)() click to toggle source

You can also call

condition.save!
# File lib/fastly.rb, line 499
  
update_customer(customer)() click to toggle source

You can also call

customer.save!
# File lib/fastly.rb, line 394
  
update_dictionary(dictionary)() click to toggle source

You can also call

dictionary.save!
# File lib/fastly.rb, line 414
  
update_dictionary_item(dictionary_item)() click to toggle source

You can also call

dictionary_item.save!
# File lib/fastly.rb, line 419
  
update_director(director)() click to toggle source

You can also call

director.save!
# File lib/fastly.rb, line 424
  
update_domain(domain)() click to toggle source

You can also call

domain.save!
# File lib/fastly.rb, line 429
  
update_gzip(gzip)() click to toggle source

You can also call

gzip.save!
# File lib/fastly.rb, line 484
  
update_header(header)() click to toggle source

You can also call

header.save!
# File lib/fastly.rb, line 479
  
update_healthcheck(healthcheck)() click to toggle source

You can also call

healthcheck.save!
# File lib/fastly.rb, line 434
  
update_match(match)() click to toggle source

You can also call

match.save!
# File lib/fastly.rb, line 439
  
update_papertrail_logging(papertrail_logging)() click to toggle source

You can also call

papertrail_logging.save!
# File lib/fastly.rb, line 454
  
update_request_setting(request_setting)() click to toggle source

You can also call

request_setting.save!
# File lib/fastly.rb, line 489
  
update_response_object(response_object)() click to toggle source

You can also call

response_object.save!
# File lib/fastly.rb, line 494
  
update_s3_logging(s3_logging)() click to toggle source

You can also call

s3_logging.save!
# File lib/fastly.rb, line 449
  
update_service(service)() click to toggle source

You can also call

service.save!
# File lib/fastly.rb, line 399
  
update_settings(opts = {}) click to toggle source

Update the Settings object for the specified Version

# File lib/fastly/settings.rb, line 70
def update_settings(opts = {})
  update(Settings, opts)
end
update_settings(settings)() click to toggle source

You can also call

settings.save!
# File lib/fastly.rb, line 444
  
update_snippet(snippet)() click to toggle source

You can also call

snippet.save!
# File lib/fastly.rb, line 469
  
update_syslog(syslog)() click to toggle source

You can also call

syslog.save!
# File lib/fastly.rb, line 459
  
update_user(user)() click to toggle source

You can also call

user.save!
# File lib/fastly.rb, line 389
  
update_vcl(vcl)() click to toggle source

You can also call

vcl.save!
# File lib/fastly.rb, line 464
  
update_version(version)() click to toggle source

You can also call

version.save!
# File lib/fastly.rb, line 404
  
usage(opts) click to toggle source

Returns usage information aggregated across all Fastly services and grouped by region.

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

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

Other options available are:

from

earliest time from which to fetch historical statistics

to

latest time from which to fetch historical statistics

by

the sampling rate used to produce the result set (minute, hour, day)

region

restrict query to a particular region

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

# File lib/fastly.rb, line 143
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