Class: Fastly::Client
- Inherits:
-
Object
- Object
- Fastly::Client
- Defined in:
- lib/fastly/client.rb,
lib/fastly/client/curl.rb
Overview
The UserAgent to communicate with the API
Defined Under Namespace
Classes: Curl
Instance Attribute Summary collapse
-
#api_key ⇒ Object
:nodoc: all.
-
#cookie ⇒ Object
:nodoc: all.
-
#customer ⇒ Object
:nodoc: all.
-
#http ⇒ Object
:nodoc: all.
-
#password ⇒ Object
:nodoc: all.
-
#user ⇒ Object
:nodoc: all.
Instance Method Summary collapse
- #authed? ⇒ Boolean
- #delete(path) ⇒ Object
-
#fully_authed? ⇒ Boolean
Some methods require full username and password rather than just auth token.
- #get(path, params = {}) ⇒ Object
- #get_stats(path, params = {}) ⇒ Object
-
#initialize(opts) ⇒ Client
constructor
A new instance of Client.
- #post(path, params = {}) ⇒ Object
- #put(path, params = {}) ⇒ Object
- #require_key! ⇒ Object
- #require_key? ⇒ Boolean
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 |
# 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) base = opts.fetch(:base_url, 'https://api.fastly.com') uri = URI.parse(base) @http = Curl.new(uri) return self unless fully_authed? # If we're fully authed (i.e username and password ) then we need to log in resp = http.post('/login', make_params(user: user, password: password)) if resp.success? @cookie = resp['Set-Cookie'] else fail Unauthorized end self end |
Instance Attribute Details
#api_key ⇒ Object
:nodoc: all
9 10 11 |
# File 'lib/fastly/client.rb', line 9 def api_key @api_key end |
#cookie ⇒ Object
:nodoc: all
9 10 11 |
# File 'lib/fastly/client.rb', line 9 def @cookie end |
#customer ⇒ Object
:nodoc: all
9 10 11 |
# File 'lib/fastly/client.rb', line 9 def customer @customer end |
#http ⇒ Object
:nodoc: all
9 10 11 |
# File 'lib/fastly/client.rb', line 9 def http @http end |
#password ⇒ Object
:nodoc: all
9 10 11 |
# File 'lib/fastly/client.rb', line 9 def password @password end |
#user ⇒ Object
:nodoc: all
9 10 11 |
# File 'lib/fastly/client.rb', line 9 def user @user end |
Instance Method Details
#authed? ⇒ Boolean
46 47 48 |
# File 'lib/fastly/client.rb', line 46 def authed? !api_key.nil? || fully_authed? end |
#delete(path) ⇒ Object
81 82 83 84 |
# File 'lib/fastly/client.rb', line 81 def delete(path) resp = http.delete(path, headers) resp.success? end |
#fully_authed? ⇒ Boolean
Some methods require full username and password rather than just auth token
51 52 53 |
# File 'lib/fastly/client.rb', line 51 def fully_authed? !(user.nil? || password.nil?) end |
#get(path, params = {}) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/fastly/client.rb', line 55 def get(path, params = {}) path += "?#{make_params(params)}" unless params.empty? resp = http.get(path, headers) return nil if 404 == resp.status fail Error, resp. unless resp.success? JSON.parse(resp.body) end |
#get_stats(path, params = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/fastly/client.rb', line 63 def get_stats(path, params = {}) content = get(path, params) case content['status'] when 'success' then content['data'] else fail Error, content['message'] end end |
#post(path, params = {}) ⇒ Object
73 74 75 |
# File 'lib/fastly/client.rb', line 73 def post(path, params = {}) post_and_put(:post, path, params) end |
#put(path, params = {}) ⇒ Object
77 78 79 |
# File 'lib/fastly/client.rb', line 77 def put(path, params = {}) post_and_put(:put, path, params) end |
#require_key! ⇒ Object
36 37 38 39 40 |
# File 'lib/fastly/client.rb', line 36 def require_key! raise Fastly::AuthRequired.new("This request requires an API key") if api_key.nil? @require_key = true end |
#require_key? ⇒ Boolean
42 43 44 |
# File 'lib/fastly/client.rb', line 42 def require_key? !!@require_key end |