Class: Fastly::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fastly/client.rb

Overview

The UserAgent to communicate with the API

Constant Summary collapse

ROOT_CA =

:nodoc: all

File.join("../../cacert.pem", __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fastly/client.rb', line 11

def initialize(opts)
  @api_key  = opts.fetch(:api_key, nil)
  @user     = opts.fetch(:user, nil)
  @password = opts.fetch(:password, nil)
  @customer = opts.fetch(:customer, nil)

  certificate_store = opts.fetch(:certificate_store, ROOT_CA)

  base      = opts.fetch(:base_url, 'https://api.fastly.com')
  uri       = URI.parse(base)
  ssl       = uri.is_a? URI::HTTPS  # detect if we should pass `use_ssl`
  @http     = Net::HTTP.start(uri.host, uri.port, use_ssl: ssl)

  http.ca_path      = certificate_store
  http.verify_mode  = OpenSSL::SSL::VERIFY_PEER
  http.verify_depth = 5

  return self unless fully_authed?

  # If full auth creds (user/pass) then log in and set a cookie
  resp = http.post('/login', make_params(user: user, password: password))

  if resp.kind_of?(Net::HTTPSuccess)
    @cookie = resp['Set-Cookie']
  else
    fail Unauthorized, "Invalid auth credentials. Check username/password."
  end

  self
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/fastly/client.rb', line 9

def api_key
  @api_key
end

Returns the value of attribute cookie.



9
10
11
# File 'lib/fastly/client.rb', line 9

def cookie
  @cookie
end

#customerObject

Returns the value of attribute customer.



9
10
11
# File 'lib/fastly/client.rb', line 9

def customer
  @customer
end

#httpObject

Returns the value of attribute http.



9
10
11
# File 'lib/fastly/client.rb', line 9

def http
  @http
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/fastly/client.rb', line 9

def password
  @password
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/fastly/client.rb', line 9

def user
  @user
end

Instance Method Details

#authed?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/fastly/client.rb', line 51

def authed?
  !api_key.nil? || fully_authed?
end

#delete(path) ⇒ Object



86
87
88
89
# File 'lib/fastly/client.rb', line 86

def delete(path)
  resp  = http.delete(path, headers)
  resp.kind_of?(Net::HTTPSuccess)
end

#fully_authed?Boolean

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

Returns:

  • (Boolean)


56
57
58
# File 'lib/fastly/client.rb', line 56

def fully_authed?
  !(user.nil? || password.nil?)
end

#get(path, params = {}) ⇒ Object



60
61
62
63
64
65
# File 'lib/fastly/client.rb', line 60

def get(path, params = {})
  path += "?#{make_params(params)}" unless params.empty?
  resp  = http.get(path, headers)
  fail Error, resp.body unless resp.kind_of?(Net::HTTPSuccess)
  JSON.parse(resp.body)
end

#get_stats(path, params = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/fastly/client.rb', line 67

def get_stats(path, params = {})
  resp = get(path, params)

  # return meta data, not just the actual stats data
  if resp['status'] == 'success'
    resp
  else
    fail Error, resp['msg']
  end
end

#post(path, params = {}) ⇒ Object



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

def post(path, params = {})
  post_and_put(:post, path, params)
end

#put(path, params = {}) ⇒ Object



82
83
84
# File 'lib/fastly/client.rb', line 82

def put(path, params = {})
  post_and_put(:put, path, params)
end

#require_key!Object



42
43
44
45
# File 'lib/fastly/client.rb', line 42

def require_key!
  raise Fastly::KeyAuthRequired.new("This request requires an API key") if api_key.nil?
  @require_key = true
end

#require_key?Boolean

Returns:

  • (Boolean)


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

def require_key?
  !!@require_key
end