Class: Fastly

Inherits:
Object
  • Object
show all
Includes:
Fetcher
Defined in:
lib/fastly.rb,
lib/fastly/vcl.rb,
lib/fastly/base.rb,
lib/fastly/gzip.rb,
lib/fastly/user.rb,
lib/fastly/util.rb,
lib/fastly/match.rb,
lib/fastly/client.rb,
lib/fastly/domain.rb,
lib/fastly/header.rb,
lib/fastly/origin.rb,
lib/fastly/syslog.rb,
lib/fastly/backend.rb,
lib/fastly/fetcher.rb,
lib/fastly/invoice.rb,
lib/fastly/service.rb,
lib/fastly/version.rb,
lib/fastly/customer.rb,
lib/fastly/director.rb,
lib/fastly/settings.rb,
lib/fastly/condition.rb,
lib/fastly/s3_logging.rb,
lib/fastly/gem_version.rb,
lib/fastly/healthcheck.rb,
lib/fastly/cache_setting.rb,
lib/fastly/request_setting.rb,
lib/fastly/response_object.rb,
lib/fastly/belongs_to_service_and_version.rb

Overview

A client library for interacting with the Fastly web acceleration service

Defined Under Namespace

Modules: Fetcher, Util Classes: AdminRequired, AuthRequired, Backend, Base, BelongsToServiceAndVersion, CacheSetting, Client, Condition, Customer, Director, Domain, Error, FullAuthRequired, Gzip, Header, Healthcheck, Invoice, Match, Origin, RequestSetting, ResponseObject, S3Logging, Service, Settings, Syslog, Unauthorized, User, VCL, Version

Constant Summary collapse

VERSION =

The current version of the library

"1.1.2"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fetcher

#client, #create, #delete, #get, #list, #update

Constructor Details

#initialize(opts) ⇒ Fastly

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.



47
48
49
50
# File 'lib/fastly.rb', line 47

def initialize(opts)
  self.client(opts)
  self
end

Class Method Details

.get_options(*files) ⇒ Object

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>


657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/fastly.rb', line 657

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+)$/) do
    options[$1.to_sym] = $2;
    @ARGV.shift;
  end
  raise"Couldn't find options from command line arguments or #{files.join(', ')}" unless options.size>0
  options;
end

.load_config(file) ⇒ Object

Attempts to load various config options in the form

<key> = <value>

From a file.

Skips whitespace and lines starting with C<#>.



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/fastly.rb', line 630

def self.load_config(file)
  options = {}
  return options unless File.exist?(file)

  File.open(file, "r") do |infile|
    while (line = infile.gets) do
      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

Instance Method Details

#authed?Boolean

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

Returns:

  • (Boolean)


53
54
55
# File 'lib/fastly.rb', line 53

def authed?
  client.authed?
end

#commandsObject

Return a hash representing all commands available.

Useful for information.



88
89
90
# File 'lib/fastly.rb', line 88

def commands 
  client.get('/commands')
end

#current_customerObject

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



64
65
66
67
# File 'lib/fastly.rb', line 64

def current_customer
  raise Fastly::AuthRequired unless self.authed?
  @current_customer ||= get(Customer)
end

#current_userObject

Return a User object representing the current logged in user. NOTE: requires you to be fully authed - will not work with only an API key



71
72
73
74
# File 'lib/fastly.rb', line 71

def current_user
  raise Fastly::FullAuthRequired unless self.fully_authed?
  @current_user ||= get(User)
end

#fully_authed?Boolean

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

Returns:

  • (Boolean)


59
60
61
# File 'lib/fastly.rb', line 59

def fully_authed?
  client.fully_authed?
end

#get_invoice(year = nil, month = nil) ⇒ Object

Return an array of Invoice objects representing invoices for all services.

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.



97
98
99
100
101
102
103
104
# File 'lib/fastly/invoice.rb', line 97

def get_invoice(year=nil, month=nil)
  opts = {}
  unless year.nil? || month.nil?
    opts[:year]  = year
    opts[:month] = month
  end
  get(Fastly::Invoice, opts)
end

#get_settings(service, number) ⇒ Object

Get the Settings object for the specified Version



60
61
62
63
64
65
66
67
68
# File 'lib/fastly/settings.rb', line 60

def get_settings(service, number)
  klass            = Fastly::Settings
  hash             = client.get(Fastly::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)] }]
  
  return klass.new(hash, self)
end

#purge(path) ⇒ Object

Purge the specified path from your cache.



93
94
95
96
# File 'lib/fastly.rb', line 93

def purge(path)
  res = client.post("/purge/#{path}")
  #res = client.post("/purge/", :path => path)
end

#regionsObject

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



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

def regions
  client.get_stats("/stats/regions")
end

#search_services(opts) ⇒ Object

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)


106
107
108
109
110
111
# File 'lib/fastly/service.rb', line 106

def search_services(opts)
  klass = Fastly::Service
  hash  = client.get(klass.post_path+"/search", opts)
  return nil if hash.nil?
  klass.new(hash, self)
end

#set_customer(id) ⇒ Object

Set the current customer to act as. NOTE: this will only work if you’re an admin



78
79
80
81
82
83
# File 'lib/fastly.rb', line 78

def set_customer(id)
    raise Fastly::FullAuthRequired "You must be fully authed to set the customer" unless self.fully_authed?;
    raise Fastly::AdminRequired "You must be an admin to set the customer" unless self.current_user.can_do?(:admin);
    @current_customer = nil 
    client.set_customer(id);
end

#stats(opts) ⇒ Object

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.

Raises:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fastly.rb', line 114

def stats(opts)
  raise Fastly::Error.new("You can't specify a field or a service for an aggregate request") if opts[:aggregate] && (opts[:field] || opts[:service])
  
  url  = "/stats"

  if opts.delete(:aggregate)
    url += "/aggregate"
  end
  
  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_settings(opts = {}) ⇒ Object

Update the Settings object for the specified Version



71
72
73
# File 'lib/fastly/settings.rb', line 71

def update_settings(opts={})
  update(Fastly::Settings, opts)
end

#usage(opts) ⇒ Object

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

If the :by_service flag is passed then teturns 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.



146
147
148
149
150
# File 'lib/fastly.rb', line 146

def usage(opts)
  url  = "/stats/usage";
  url += "_by_service" if opts.delete(:by_service)
  client.get_stats(url, opts)
end