Class: Upyun::Purge::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
# File 'lib/upyun/purge/client.rb', line 8

def initialize(option = {})
  @bucket_name = option[:bucket_name] || Upyun::Purge.bucket_name
  @operator_name = option[:operator_name] || Upyun::Purge.operator_name
  @operator_password = option[:operator_password] || Upyun::Purge.operator_password
end

Instance Attribute Details

#bucket_nameObject

Returns the value of attribute bucket_name.



6
7
8
# File 'lib/upyun/purge/client.rb', line 6

def bucket_name
  @bucket_name
end

#operator_nameObject

Returns the value of attribute operator_name.



6
7
8
# File 'lib/upyun/purge/client.rb', line 6

def operator_name
  @operator_name
end

#operator_passwordObject

Returns the value of attribute operator_password.



6
7
8
# File 'lib/upyun/purge/client.rb', line 6

def operator_password
  @operator_password
end

#urlsObject

Returns the value of attribute urls.



4
5
6
# File 'lib/upyun/purge/client.rb', line 4

def urls
  @urls
end

Instance Method Details

#auth_header(datetime) ⇒ Object



41
42
43
# File 'lib/upyun/purge/client.rb', line 41

def auth_header(datetime)
  "UpYun #{@bucket_name}:#{@operator_name}:#{make_signature(datetime)}"
end

#make_signature(datetime) ⇒ Object



34
35
36
37
38
39
# File 'lib/upyun/purge/client.rb', line 34

def make_signature(datetime)
  _urls = @urls.join("\n")
  second_str = [_urls, @bucket_name, datetime, md5(@operator_password)].join('&')
  sign = md5(second_str)
  sign
end

#md5(str) ⇒ Object



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

def md5(str)
  Digest::MD5.hexdigest str
end

#purge(urls = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/upyun/purge/client.rb', line 14

def purge(urls = nil)
  @urls = urls.is_a?(Array) ? urls : [urls]
  @urls = @urls.compact

  uri = URI(Upyun::Purge.api_server)
  http = Net::HTTP.new(uri.host, uri.port)
  datetime = Time.now.gmtime.httpdate
  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data(purge: @urls.join("\n"))
  request['Authorization'] = auth_header(datetime)
  request['Date'] = datetime
  response = http.request(request)
  case response
  when Net::HTTPSuccess
    return JSON.parse(response.body)
  else
    raise Exception, "\n\tRequest uri:#{uri.request_uri} \n\tResponse code: #{response.code} \n\tMessage: #{response.body}"
  end
end