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
42
# 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"
    customer  = opts[:customer]
    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.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

#customerObject

Returns the value of attribute customer.



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

def customer
  @customer
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)


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

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

#delete(path) ⇒ Object



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

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)


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

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

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

Raises:



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

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

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

Raises:



66
67
68
69
70
# File 'lib/fastly/client.rb', line 66

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



72
73
74
# File 'lib/fastly/client.rb', line 72

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

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



76
77
78
# File 'lib/fastly/client.rb', line 76

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

#set_customer(id) ⇒ Object



54
55
56
# File 'lib/fastly/client.rb', line 54

def set_customer(id)
  
end