Class: Fastly::Client
- Inherits:
-
Object
- Object
- Fastly::Client
- Defined in:
- lib/fastly/client.rb
Overview
The UserAgent to communicate with the API
Defined Under Namespace
Classes: Curl
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#cookie ⇒ Object
Returns the value of attribute cookie.
-
#customer ⇒ Object
Returns the value of attribute customer.
-
#http ⇒ Object
Returns the value of attribute http.
-
#password ⇒ Object
Returns the value of attribute password.
-
#user ⇒ Object
Returns the value of attribute user.
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
- #set_customer(id) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fastly/client.rb', line 20 def initialize(opts) [:api_key, :user, :password].each do |key| self.send("#{key}=", opts[key]) if opts.has_key?(key) end base = opts[:base_url] || "https://api.fastly.com" uri = URI.parse(base) scheme = uri.scheme host = uri.host curb = opts.has_key?(:use_curb) ? !!opts[:use_curb] && CURB_FU : CURB_FU port = opts.has_key?(:base_port) ? opts[:base_port] : (scheme == "https") ? 443 : 80 self.http = curb ? Fastly::Client::Curl.new(host, port) : Net::HTTP.new(host, port) self.http.use_ssl = (scheme == "https") return self unless fully_authed? # If we're fully authed (i.e username and password ) then we need to log in resp = self.http.post('/login', make_params(:user => user, :password => password)) raise Fastly::Unauthorized unless resp.success? self. = resp['set-cookie'] end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
18 19 20 |
# File 'lib/fastly/client.rb', line 18 def api_key @api_key end |
#cookie ⇒ Object
Returns the value of attribute cookie.
18 19 20 |
# File 'lib/fastly/client.rb', line 18 def @cookie end |
#customer ⇒ Object
Returns the value of attribute customer.
18 19 20 |
# File 'lib/fastly/client.rb', line 18 def customer @customer end |
#http ⇒ Object
Returns the value of attribute http.
18 19 20 |
# File 'lib/fastly/client.rb', line 18 def http @http end |
#password ⇒ Object
Returns the value of attribute password.
18 19 20 |
# File 'lib/fastly/client.rb', line 18 def password @password end |
#user ⇒ Object
Returns the value of attribute user.
18 19 20 |
# File 'lib/fastly/client.rb', line 18 def user @user end |
Instance Method Details
#authed? ⇒ Boolean
50 51 52 |
# File 'lib/fastly/client.rb', line 50 def authed? !api_key.nil? || fully_authed? end |
#delete(path) ⇒ Object
85 86 87 88 |
# File 'lib/fastly/client.rb', line 85 def delete(path) resp = self.http.delete(path, headers) return resp.success? end |
#fully_authed? ⇒ Boolean
Some methods require full username and password rather than just auth token
55 56 57 |
# File 'lib/fastly/client.rb', line 55 def fully_authed? !(user.nil? || password.nil?) end |
#get(path, params = {}) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/fastly/client.rb', line 63 def get(path, params={}) path += "?"+make_params(params) unless params.empty? resp = self.http.get(path, headers) return nil if 404 == resp.status raise Fastly::Error, resp. unless resp.success? JSON.parse(resp.body) end |
#get_stats(path, params = {}) ⇒ Object
71 72 73 74 75 |
# File 'lib/fastly/client.rb', line 71 def get_stats(path, params={}) content = get(path, params) raise Fastly::Error, content["message"] unless content["status"] == 'success' content["data"] end |
#post(path, params = {}) ⇒ Object
77 78 79 |
# File 'lib/fastly/client.rb', line 77 def post(path, params={}) post_and_put(:post, path, params) end |
#put(path, params = {}) ⇒ Object
81 82 83 |
# File 'lib/fastly/client.rb', line 81 def put(path, params={}) post_and_put(:put, path, params) end |
#require_key! ⇒ Object
40 41 42 43 44 |
# File 'lib/fastly/client.rb', line 40 def require_key! raise Fastly::AuthRequired.new("This request requires an API key") if api_key.nil? @require_key = true end |
#require_key? ⇒ Boolean
46 47 48 |
# File 'lib/fastly/client.rb', line 46 def require_key? !!@require_key end |
#set_customer(id) ⇒ Object
59 60 61 |
# File 'lib/fastly/client.rb', line 59 def set_customer(id) end |