Class: Fastly::Client

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

Overview

The UserAgent to communicate with the API

Defined Under Namespace

Classes: Curl

Instance Attribute Summary collapse

Instance Method Summary collapse

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
39
40
41
# 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"
    port   = opts[:base_port]     || 80
    uri    = URI.parse(base)
    scheme = uri.scheme
    host   = uri.host
    curb = opts.has_key?(:use_curb) ? !!opts[:use_curb] && CURB_FU : CURB_FU
    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.cookie = resp['set-cookie']
    content     = JSON.parse(resp.body)
    #return self, content['user'], content['content']
    self
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



18
19
20
# File 'lib/fastly/client.rb', line 18

def api_key
  @api_key
end

Returns the value of attribute cookie.



18
19
20
# File 'lib/fastly/client.rb', line 18

def cookie
  @cookie
end

#httpObject

Returns the value of attribute http.



18
19
20
# File 'lib/fastly/client.rb', line 18

def http
  @http
end

#passwordObject

Returns the value of attribute password.



18
19
20
# File 'lib/fastly/client.rb', line 18

def password
  @password
end

#userObject

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

Returns:

  • (Boolean)


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

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

#delete(path) ⇒ Object



69
70
71
72
# File 'lib/fastly/client.rb', line 69

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

Returns:

  • (Boolean)


49
50
51
# File 'lib/fastly/client.rb', line 49

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

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

Raises:



53
54
55
56
57
58
59
# File 'lib/fastly/client.rb', line 53

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.message unless resp.success?
    JSON.parse(resp.body)
end

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



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

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

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



65
66
67
# File 'lib/fastly/client.rb', line 65

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