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.



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

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.



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

def bucket_name
  @bucket_name
end

#operator_nameObject

Returns the value of attribute operator_name.



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

def operator_name
  @operator_name
end

#operator_passwordObject

Returns the value of attribute operator_password.



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

def operator_password
  @operator_password
end

#urlsObject

Returns the value of attribute urls.



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

def urls
  @urls
end

Instance Method Details

#auth_headerObject



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

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

#make_signatureObject



38
39
40
41
42
43
44
# File 'lib/upyun/purge/client.rb', line 38

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

#md5(str) ⇒ Object



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

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

#purge(urls = nil) ⇒ Object



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

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)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data( purge: @urls.join("\n") )
  request['Authorization'] = auth_header
  request['Date'] = Time.now
  response = http.request(request)
  case response
  when Net::HTTPSuccess
    return JSON.parse(response.body)
  else
    raise Exception.new "\n\tRequest uri:#{uri.request_uri} \n\tResponse code: #{response.code} \n\tMessage: #{response.body}"
  end

end