Class: Fastly

Inherits:
Object
  • Object
show all
Includes:
Fetcher
Defined in:
lib/fastly.rb,
lib/fastly/acl.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/token.rb,
lib/fastly/client.rb,
lib/fastly/domain.rb,
lib/fastly/header.rb,
lib/fastly/syslog.rb,
lib/fastly/backend.rb,
lib/fastly/fetcher.rb,
lib/fastly/invoice.rb,
lib/fastly/service.rb,
lib/fastly/snippet.rb,
lib/fastly/version.rb,
lib/fastly/customer.rb,
lib/fastly/director.rb,
lib/fastly/settings.rb,
lib/fastly/acl_entry.rb,
lib/fastly/condition.rb,
lib/fastly/dictionary.rb,
lib/fastly/s3_logging.rb,
lib/fastly/gcs_logging.rb,
lib/fastly/gem_version.rb,
lib/fastly/healthcheck.rb,
lib/fastly/cache_setting.rb,
lib/fastly/dictionary_item.rb,
lib/fastly/dynamic_snippet.rb,
lib/fastly/request_setting.rb,
lib/fastly/response_object.rb,
lib/fastly/big_query_logging.rb,
lib/fastly/sumologic_logging.rb,
lib/fastly/papertrail_logging.rb,
lib/fastly/belongs_to_service_and_version.rb

Overview

The current version of the library

Defined Under Namespace

Modules: Fetcher, Util Classes: ACL, ACLEntry, AdminRequired, AuthRequired, Backend, Base, BelongsToServiceAndVersion, BigQueryLogging, CacheSetting, Client, Condition, Customer, Dictionary, DictionaryItem, Director, Domain, DynamicSnippet, Error, FullAuthRequired, GcsLogging, Gzip, Header, Healthcheck, Invoice, KeyAuthRequired, Match, PapertrailLogging, RequestSetting, ResponseObject, S3Logging, Service, Settings, Snippet, SumologicLogging, Syslog, Token, Unauthorized, User, VCL, Version

Constant Summary collapse

VERSION =
"2.5.3"

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.


57
58
59
60
61
62
63
64
# File 'lib/fastly.rb', line 57

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

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>

717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/fastly.rb', line 717

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) ⇒ Object

Attempts to load various config options in the form

<key> = <value>

From a file.

Skips whitespace and lines starting with C<#>.


689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/fastly.rb', line 689

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

Instance Method Details

#authed?Boolean

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

Returns:

  • (Boolean)

67
68
69
# File 'lib/fastly.rb', line 67

def authed?
  client.authed?
end

#current_customerObject

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


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

def current_customer
  fail AuthRequired unless authed?
  @current_customer ||= get(Customer)
end

#current_userObject

Return a User object representing the current logged in user.


84
85
86
# File 'lib/fastly.rb', line 84

def current_user
  @current_user ||= get(User)
end

#customer_tokens(opts) ⇒ Object


32
33
34
35
# File 'lib/fastly/token.rb', line 32

def customer_tokens(opts)
  hash = client.get("/customer/#{opts[:customer_id]}/tokens")
  hash.map { |token_hash| Token.new(token_hash, Fastly::Fetcher) }
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)

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

def fully_authed?
  client.fully_authed?
end

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

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.


92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ Object

Return an Invoice object for the passed invoice ID


105
106
107
108
109
110
111
112
# 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_settings(service, number) ⇒ Object

Get the Settings object for the specified Version


61
62
63
64
65
66
67
# 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

#list_invoicesObject

Retun an array of Invoice objects.


115
116
117
118
119
# File 'lib/fastly/invoice.rb', line 115

def list_invoices
  opts = { customer_id: current_customer.id }

  list(Invoice, opts)
end

#new_token(opts) ⇒ Object


23
24
25
26
27
28
29
30
# File 'lib/fastly/token.rb', line 23

def new_token(opts)
  opts[:username] = client.user
  opts[:password] = client.password
  opts[:include_auth] = false
  
  token = create(Token, opts)
  token.nil? ? nil : token
end

#purge(url, soft = false) ⇒ Object

Purge the specified path from your cache.


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

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

#regionsObject

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


151
152
153
# File 'lib/fastly.rb', line 151

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)

103
104
105
106
# 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) ⇒ 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.


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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_settings(opts = {}) ⇒ Object

Update the Settings object for the specified Version


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

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

#usage(opts) ⇒ Object

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.


143
144
145
146
147
148
# 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