Class: Akamai::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/akamairb/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
13
14
# File 'lib/akamairb/connection.rb', line 8

def initialize(config)
  self.conn = Faraday.new(:url => 'https://api.ccu.akamai.com') do |faraday|
    faraday.headers['Content-Type'] = 'application/json'
    faraday.adapter Faraday.default_adapter  # make requests with Net::HTTP
    faraday.basic_auth(config["user"],config["pass"])
  end
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



6
7
8
# File 'lib/akamairb/connection.rb', line 6

def conn
  @conn
end

Instance Method Details

#progress(url) ⇒ Object



25
26
27
# File 'lib/akamairb/connection.rb', line 25

def progress url
  parse conn.get(url)
end

#progress_until_done(url) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/akamairb/connection.rb', line 29

def progress_until_done url
  loop do
    response = progress url
    yield response if block_given?
    break if response["purgeStatus"] == "Done"
    sleep response["pingAfterSeconds"]
  end
end

#purge(*urls) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/akamairb/connection.rb', line 16

def purge(*urls)
  response = conn.post do |req|
    req.url '/ccu/v2/queues/default'
    req.body = JSON.dump({"objects" => urls.flatten})
  end
  
  parse response
end

#purge_and_progress(*urls, &block) ⇒ Object



38
39
40
41
# File 'lib/akamairb/connection.rb', line 38

def purge_and_progress *urls, &block
  response = purge *urls
  progress_until_done response["progressUri"], &block
end