Class: Fastly
- Inherits:
-
Object
- Object
- Fastly
- Includes:
- Fetcher
- Defined in:
- lib/fastly.rb,
lib/fastly/vcl.rb,
lib/fastly/base.rb,
lib/fastly/user.rb,
lib/fastly/match.rb,
lib/fastly/client.rb,
lib/fastly/domain.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/healthcheck.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 Classes: AdminRequired, AuthRequired, Backend, Base, BelongsToServiceAndVersion, Client, Customer, Director, Domain, Error, FullAuthRequired, Healthcheck, Invoice, Match, Origin, Service, Settings, Syslog, Unauthorized, User, VCL, Version
Constant Summary collapse
- VERSION =
The current version of the library
"1.00"
Class Method Summary collapse
-
.get_options(*files) ⇒ Object
Tries to load options from the file passed in using, C<load_options>, stopping when it finds the first one.
-
.load_config(file) ⇒ Object
Attempts to load various config options in the form.
Instance Method Summary collapse
-
#authed? ⇒ Boolean
Whether or not we’re authed at all by either username & password or API key.
-
#commands ⇒ Object
Return a hash representing all commands available.
-
#current_customer ⇒ Object
Return a Customer object representing the customer of the current logged in user.
-
#current_user ⇒ Object
Return a User object representing the current logged in user.
-
#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.
-
#get_invoice(year = nil, month = nil) ⇒ Object
Return an array of Invoice objects representing invoices for all services.
-
#get_settings(service, number) ⇒ Object
Get the Settings object for the specified Version.
-
#initialize(opts) ⇒ Fastly
constructor
Create a new Fastly client.
-
#purge(path) ⇒ Object
Purge the specified path from your cache.
-
#search_services(opts) ⇒ Object
Search all the services that the current customer has.
-
#set_customer(id) ⇒ Object
Set the current customer to act as.
-
#update_settings(opts = {}) ⇒ Object
Update the Settings object for the specified Version.
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.
41 42 43 44 |
# File 'lib/fastly.rb', line 41 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>
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/fastly.rb', line 443 def self.(*files) = {} files.each do |file| next unless File.exist?(file) = load_config(file) break end while (ARGV.size>0 && ARGV[0] =~ /^-+(\w+)\=(\w+)$/) do [$1.to_sym] = $2; @ARGV.shift; end raise"Couldn't find options from command line arguments or #{files.join(', ')}" unless .size>0 ; 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<#>.
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/fastly.rb', line 416 def self.load_config(file) = {} return 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) [key.to_sym] = val; end end ; end |
Instance Method Details
#authed? ⇒ Boolean
Whether or not we’re authed at all by either username & password or API key
47 48 49 |
# File 'lib/fastly.rb', line 47 def authed? client.authed? end |
#commands ⇒ Object
Return a hash representing all commands available.
Useful for information.
82 83 84 |
# File 'lib/fastly.rb', line 82 def commands client.get('/commands') end |
#current_customer ⇒ Object
Return a Customer object representing the customer of the current logged in user.
58 59 60 61 |
# File 'lib/fastly.rb', line 58 def current_customer raise Fastly::AuthRequired unless self.authed? @current_customer ||= get(Customer) end |
#current_user ⇒ Object
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
65 66 67 68 |
# File 'lib/fastly.rb', line 65 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
53 54 55 |
# File 'lib/fastly.rb', line 53 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
61 62 63 |
# File 'lib/fastly/settings.rb', line 61 def get_settings(service, number) get(Fastly::Settings, service, number) end |
#purge(path) ⇒ Object
Purge the specified path from your cache.
87 88 89 90 |
# File 'lib/fastly.rb', line 87 def purge(path) res = client.post("/purge/#{path}") #res = client.post("/purge/", :path => path) 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
72 73 74 75 76 77 |
# File 'lib/fastly.rb', line 72 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 |
#update_settings(opts = {}) ⇒ Object
Update the Settings object for the specified Version
66 67 68 |
# File 'lib/fastly/settings.rb', line 66 def update_settings(opts={}) update(Fastly::Settings, opts) end |