Class: Fastly::Client
- Inherits:
-
Object
- Object
- Fastly::Client
- Defined in:
- lib/fastly/client.rb
Overview
The UserAgent to communicate with the API
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.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/fastly/client.rb', line 10 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 = Net::HTTP.start(uri.host, uri.port, use_ssl: true) 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_key ⇒ Object
:nodoc: all
8 9 10 |
# File 'lib/fastly/client.rb', line 8 def api_key @api_key end |
#cookie ⇒ Object
:nodoc: all
8 9 10 |
# File 'lib/fastly/client.rb', line 8 def @cookie end |
#customer ⇒ Object
:nodoc: all
8 9 10 |
# File 'lib/fastly/client.rb', line 8 def customer @customer end |
#http ⇒ Object
:nodoc: all
8 9 10 |
# File 'lib/fastly/client.rb', line 8 def http @http end |
#password ⇒ Object
:nodoc: all
8 9 10 |
# File 'lib/fastly/client.rb', line 8 def password @password end |
#user ⇒ Object
:nodoc: all
8 9 10 |
# File 'lib/fastly/client.rb', line 8 def user @user end |
Instance Method Details
#authed? ⇒ Boolean
43 44 45 |
# File 'lib/fastly/client.rb', line 43 def authed? !api_key.nil? || fully_authed? end |
#delete(path) ⇒ Object
78 79 80 81 |
# File 'lib/fastly/client.rb', line 78 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
48 49 50 |
# File 'lib/fastly/client.rb', line 48 def fully_authed? !(user.nil? || password.nil?) end |
#get(path, params = {}) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/fastly/client.rb', line 52 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
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/fastly/client.rb', line 59 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
70 71 72 |
# File 'lib/fastly/client.rb', line 70 def post(path, params = {}) post_and_put(:post, path, params) end |
#put(path, params = {}) ⇒ Object
74 75 76 |
# File 'lib/fastly/client.rb', line 74 def put(path, params = {}) post_and_put(:put, path, params) end |
#require_key! ⇒ Object
34 35 36 37 |
# File 'lib/fastly/client.rb', line 34 def require_key! raise Fastly::KeyAuthRequired.new("This request requires an API key") if api_key.nil? @require_key = true end |
#require_key? ⇒ Boolean
39 40 41 |
# File 'lib/fastly/client.rb', line 39 def require_key? !!@require_key end |