akamai_api 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/Guardfile +5 -15
  3. data/README.md +106 -36
  4. data/Thorfile +5 -0
  5. data/akamai_api.gemspec +6 -3
  6. data/bin/akamai_api +1 -2
  7. data/cassettes/akamai_api_ccu_arl_invalidate/invalid_credentials.yml +37 -0
  8. data/cassettes/akamai_api_ccu_arl_invalidate/invalid_item.yml +36 -0
  9. data/cassettes/akamai_api_ccu_arl_invalidate/multiple_items.yml +38 -0
  10. data/cassettes/akamai_api_ccu_arl_invalidate/single_item.yml +38 -0
  11. data/cassettes/akamai_api_ccu_arl_remove/invalid_credentials.yml +37 -0
  12. data/cassettes/akamai_api_ccu_arl_remove/invalid_item.yml +36 -0
  13. data/cassettes/akamai_api_ccu_arl_remove/multiple_items.yml +38 -0
  14. data/cassettes/akamai_api_ccu_arl_remove/single_item.yml +38 -0
  15. data/cassettes/akamai_api_ccu_cpcode_invalidate/invalid_credentials.yml +37 -0
  16. data/cassettes/akamai_api_ccu_cpcode_invalidate/invalid_item.yml +36 -0
  17. data/cassettes/akamai_api_ccu_cpcode_invalidate/multiple_items.yml +38 -0
  18. data/cassettes/akamai_api_ccu_cpcode_invalidate/single_item.yml +38 -0
  19. data/cassettes/akamai_api_ccu_cpcode_remove/invalid_credentials.yml +37 -0
  20. data/cassettes/akamai_api_ccu_cpcode_remove/invalid_item.yml +36 -0
  21. data/cassettes/akamai_api_ccu_cpcode_remove/multiple_items.yml +38 -0
  22. data/cassettes/akamai_api_ccu_cpcode_remove/single_item.yml +38 -0
  23. data/cassettes/akamai_api_ccu_purge_status/completed_request.yml +35 -0
  24. data/cassettes/akamai_api_ccu_purge_status/enqueued_request.yml +35 -0
  25. data/cassettes/akamai_api_ccu_purge_status/invalid_credentials.yml +35 -0
  26. data/cassettes/akamai_api_ccu_purge_status/not_found_request.yml +37 -0
  27. data/cassettes/akamai_api_ccu_status/invalid_credentials.yml +35 -0
  28. data/cassettes/akamai_api_ccu_status/valid_credentials.yml +34 -0
  29. data/cassettes/akamai_api_eccu_last_request/invalid_credentials.yml +68 -0
  30. data/cassettes/akamai_api_eccu_last_request/valid_credentials.yml +140 -0
  31. data/cassettes/akamai_api_eccu_publish/invalid_credentials.yml +73 -0
  32. data/cassettes/akamai_api_eccu_publish/invalid_domain.yml +60 -0
  33. data/cassettes/akamai_api_eccu_publish/successful.yml +126 -0
  34. data/cassettes/akamai_api_eccu_requests/invalid_credentials.yml +68 -0
  35. data/cassettes/akamai_api_eccu_requests/valid_credentials.yml +325 -0
  36. data/features/ccu_arl_invalidate.feature +83 -0
  37. data/features/ccu_arl_remove.feature +83 -0
  38. data/features/ccu_cpcode_invalidate.feature +83 -0
  39. data/features/ccu_cpcode_remove.feature +83 -0
  40. data/features/ccu_purge_status.feature +83 -0
  41. data/features/ccu_status.feature +31 -0
  42. data/features/eccu_last_request.feature +47 -0
  43. data/features/eccu_publish.feature +98 -0
  44. data/features/eccu_requests.feature +43 -0
  45. data/features/step_definitions/contain_multiple_times_steps.rb +3 -0
  46. data/features/support/env.rb +58 -0
  47. data/lib/akamai_api/ccu/purge/request.rb +76 -0
  48. data/lib/akamai_api/ccu/purge/response.rb +31 -0
  49. data/lib/akamai_api/ccu/purge.rb +5 -0
  50. data/lib/akamai_api/ccu/purge_status/not_found_response.rb +9 -0
  51. data/lib/akamai_api/ccu/purge_status/request.rb +32 -0
  52. data/lib/akamai_api/ccu/purge_status/response.rb +19 -0
  53. data/lib/akamai_api/ccu/purge_status/successful_response.rb +25 -0
  54. data/lib/akamai_api/ccu/purge_status.rb +10 -0
  55. data/lib/akamai_api/ccu/response.rb +18 -0
  56. data/lib/akamai_api/ccu/status/request.rb +23 -0
  57. data/lib/akamai_api/ccu/status/response.rb +11 -0
  58. data/lib/akamai_api/ccu/status.rb +2 -0
  59. data/lib/akamai_api/ccu.rb +14 -50
  60. data/lib/akamai_api/cli/app.rb +2 -2
  61. data/lib/akamai_api/cli/ccu/arl.rb +36 -0
  62. data/lib/akamai_api/cli/ccu/base.rb +18 -0
  63. data/lib/akamai_api/cli/ccu/cp_code.rb +39 -0
  64. data/lib/akamai_api/cli/ccu/purge_renderer.rb +49 -0
  65. data/lib/akamai_api/cli/ccu/status_renderer.rb +69 -0
  66. data/lib/akamai_api/cli/ccu.rb +5 -10
  67. data/lib/akamai_api/cli/eccu/entry_renderer.rb +63 -0
  68. data/lib/akamai_api/cli/eccu.rb +16 -14
  69. data/lib/akamai_api/cli.rb +7 -2
  70. data/lib/akamai_api/eccu_request.rb +19 -1
  71. data/lib/akamai_api/version.rb +1 -1
  72. data/lib/akamai_api.rb +3 -1
  73. data/spec/features/ccu/purge_request_spec.rb +94 -0
  74. data/spec/features/ccu/purge_status_request_spec.rb +31 -0
  75. data/spec/features/ccu/status_request_spec.rb +17 -0
  76. data/spec/fixtures/ccu/successful_purge.json +1 -0
  77. data/spec/lib/akamai_api/ccu/purge/request_spec.rb +124 -0
  78. data/spec/lib/akamai_api/ccu/purge/response_spec.rb +54 -0
  79. data/spec/lib/akamai_api/ccu/purge_status/not_found_response_spec.rb +41 -0
  80. data/spec/lib/akamai_api/ccu/purge_status/request_spec.rb +64 -0
  81. data/spec/lib/akamai_api/ccu/purge_status/successful_response_spec.rb +86 -0
  82. data/spec/lib/akamai_api/ccu/response_spec.rb +15 -0
  83. data/spec/lib/akamai_api/ccu/status/request_spec.rb +22 -0
  84. data/spec/lib/akamai_api/ccu/status/response_spec.rb +29 -0
  85. data/spec/lib/akamai_api/ccu_spec.rb +49 -61
  86. data/spec/lib/akamai_api/cli/ccu/status_renderer_spec.rb +36 -0
  87. data/spec/lib/akamai_api/eccu_request_spec.rb +5 -5
  88. data/spec/spec_helper.rb +21 -1
  89. metadata +163 -26
  90. data/lib/akamai_api/ccu_response.rb +0 -51
  91. data/lib/akamai_api/cli/ccu_arl.rb +0 -40
  92. data/lib/akamai_api/cli/ccu_cp_code.rb +0 -48
  93. data/lib/akamai_api/cli/template.rb +0 -36
  94. data/lib/akamai_api/cp_code.rb +0 -33
  95. data/spec/fixtures/ccu/successful_purge.xml +0 -15
  96. data/spec/fixtures/cp_code/collection.xml +0 -21
  97. data/spec/fixtures/cp_code/single.xml +0 -16
  98. data/spec/lib/akamai_api/cp_code_spec.rb +0 -47
  99. data/wsdls/cpcode.wsdl +0 -2765
@@ -1,4 +1,9 @@
1
+ require 'httparty'
1
2
  require 'active_support/core_ext'
3
+ require 'akamai_api/ccu/response'
4
+ require 'akamai_api/ccu/purge'
5
+ require 'akamai_api/ccu/status'
6
+ require 'akamai_api/ccu/purge_status'
2
7
 
3
8
  module AkamaiApi
4
9
  module Ccu
@@ -23,61 +28,20 @@ module AkamaiApi
23
28
  end
24
29
 
25
30
  def purge action, type, items, args = {}
26
- validate_action action
27
- validate_type type
28
- options = ["action=#{action}", "type=#{type}"]
29
- add_domain args[:domain], options
30
- add_email args[:email], options
31
- body = SoapBody.new do
32
- string :name, AkamaiApi.config[:auth].first
33
- string :pwd, AkamaiApi.config[:auth].last
34
- string :network, ''
35
- array :opt, options
36
- array :uri, Array.wrap(items)
37
- end
38
- response = client.call :purge_request, :message => body.to_s
39
- CcuResponse.new response, items
40
- end
41
-
42
- private
43
-
44
- def validate_action action
45
- unless %w[invalidate remove].include? action.to_s
46
- raise UnrecognizedOption, "Unknown type '#{action}' (only 'remove' and 'invalidate' are allowed)"
47
- end
48
- end
49
-
50
- def validate_type type
51
- unless %w[cpcode arl].include? type.to_s
52
- raise UnrecognizedOption, "Unknown type '#{type}' (only 'cpcode' and 'arl' are allowed)"
53
- end
54
- end
55
-
56
- def add_domain domain, options
57
- if domain.present?
58
- unless %w[production staging].include? domain.to_s
59
- raise "Unknown domain type '#{domain}' (only :production and :staging are allowed)"
60
- end
61
- options << "domain=#{domain}"
62
- end
31
+ request = Purge::Request.new action, type, domain: args[:domain]
32
+ request.execute items
63
33
  end
64
34
 
65
- def add_email email, options
66
- if email.present?
67
- emails = Array.wrap(email).join ','
68
- options << "email-notification=#{emails}"
35
+ def status progress_uri = nil
36
+ if progress_uri.present?
37
+ PurgeStatus::Request.new.execute progress_uri
38
+ else
39
+ Status::Request.new.execute
69
40
  end
70
41
  end
71
42
 
72
- def client
73
- savon_args = {
74
- :wsdl => File.expand_path('../../../wsdls/ccuapi.wsdl', __FILE__),
75
- :namespaces => {
76
- 'xmlns:soapenc' => 'http://schemas.xmlsoap.org/soap/encoding/'
77
- },
78
- :log => AkamaiApi.config[:log]
79
- }
80
- Savon.client savon_args
43
+ def self.auth
44
+ { username: AkamaiApi.config[:auth].first, password: AkamaiApi.config[:auth].last }
81
45
  end
82
46
  end
83
47
  end
@@ -2,7 +2,7 @@ module AkamaiApi
2
2
  module Cli
3
3
  class App < Thor
4
4
  desc 'ccu', 'CCU Interface'
5
- subcommand 'ccu', AkamaiApi::Cli::Ccu
5
+ subcommand 'ccu', AkamaiApi::Cli::Ccu::Base
6
6
 
7
7
  desc 'eccu', 'ECCU Interface'
8
8
  subcommand 'eccu', AkamaiApi::Cli::Eccu
@@ -15,4 +15,4 @@ module AkamaiApi
15
15
  end
16
16
  end
17
17
  end
18
- end
18
+ end
@@ -0,0 +1,36 @@
1
+ module AkamaiApi::Cli::Ccu
2
+ class Arl < AkamaiApi::Cli::Command
3
+ namespace 'ccu arl'
4
+
5
+ desc 'remove http://john.com/a.txt http://www.smith.com/b.txt ...', 'Purge ARL(s) removing them from the cache'
6
+ method_option :domain, :type => :string, :aliases => '-d',
7
+ :banner => 'production|staging',
8
+ :desc => 'Optional argument used to specify the environment. Usually you will not need this option'
9
+ method_option :banner => "foo@foo.com bar@bar.com",
10
+ :desc => 'Email(s) used to send notification when the purge has been completed'
11
+ def remove(*arls)
12
+ purge_action :remove, arls
13
+ end
14
+
15
+ desc 'invalidate http://john.com/a.txt http://www.smith.com/b.txt ...', 'Purge ARL(s) marking their cache as expired'
16
+ method_option :domain, :type => :string, :aliases => '-d',
17
+ :banner => 'production|staging',
18
+ :desc => 'Optional argument used to specify the environment. Usually you will not need this option'
19
+ method_option :banner => "foo@foo.com bar@bar.com",
20
+ :desc => 'Email(s) used to send notification when the purge has been completed'
21
+ def invalidate(*arls)
22
+ purge_action :invalidate, arls
23
+ end
24
+
25
+ no_tasks do
26
+ def purge_action type, arls
27
+ raise 'You should provide at least one valid URL' if arls.blank?
28
+ load_config
29
+ res = AkamaiApi::Ccu.purge type, :arl, arls, :domain => options[:domain]
30
+ puts ::AkamaiApi::Cli::Ccu::PurgeRenderer.new(res).render
31
+ rescue AkamaiApi::Unauthorized
32
+ puts 'Your login credentials are invalid.'
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,18 @@
1
+ module AkamaiApi::Cli::Ccu
2
+ class Base < AkamaiApi::Cli::Command
3
+ desc 'cpcode', 'CP Code CCU actions'
4
+ subcommand 'cpcode', CpCode
5
+
6
+ desc 'arl', 'ARL CCU actions'
7
+ subcommand 'arl', Arl
8
+
9
+ desc 'status [progress_uri]', 'Show the CCU queue status if no progress_uri is given, or show a CCU Purge request status if a progress uri is given'
10
+ def status progress_uri = nil
11
+ load_config
12
+ response = AkamaiApi::Ccu.status progress_uri
13
+ puts StatusRenderer.new(response).render
14
+ rescue AkamaiApi::Unauthorized
15
+ puts 'Your login credentials are invalid.'
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,39 @@
1
+ module AkamaiApi::Cli::Ccu
2
+ class CpCode < AkamaiApi::Cli::Command
3
+ namespace 'ccu cpcode'
4
+
5
+ desc 'remove CPCODE1 CPCODE2 ...', 'Purge CP Code(s) removing them from the cache'
6
+ method_option :domain, :type => :string, :aliases => '-d',
7
+ :banner => 'production|staging',
8
+ :desc => 'Optional argument used to specify the environment. Usually you will not need this option'
9
+ method_option :banner => "foo@foo.com bar@bar.com",
10
+ :desc => 'Email(s) used to send notification when the purge has been completed'
11
+ def remove(*cpcodes)
12
+ purge_action :remove, cpcodes
13
+ end
14
+
15
+ desc 'invalidate CPCODE1 CPCODE2 ...', 'Purge CP Code(s) marking their cache as expired'
16
+ method_option :domain, :type => :string, :aliases => '-d',
17
+ :banner => 'production|staging',
18
+ :desc => 'Optional argument used to specify the environment. Usually you will not need this option'
19
+ method_option :banner => "foo@foo.com bar@bar.com",
20
+ :desc => 'Email(s) used to send notification when the purge has been completed'
21
+ def invalidate(*cpcodes)
22
+ purge_action :invalidate, cpcodes
23
+ end
24
+
25
+ no_tasks do
26
+ def purge_action type, cpcodes
27
+ if cpcodes.blank?
28
+ puts 'You should provide at least one valid CP Code'
29
+ return
30
+ end
31
+ load_config
32
+ res = AkamaiApi::Ccu.purge type, :cpcode, cpcodes, :domain => options[:domain]
33
+ puts ::AkamaiApi::Cli::Ccu::PurgeRenderer.new(res).render
34
+ rescue AkamaiApi::Unauthorized
35
+ puts "Your login credentials are invalid."
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,49 @@
1
+ module AkamaiApi::Cli::Ccu
2
+ class PurgeRenderer
3
+ attr_reader :response
4
+
5
+ def initialize response
6
+ @response = response
7
+ end
8
+
9
+ def render
10
+ [
11
+ "----------",
12
+ render_response,
13
+ "----------"
14
+ ].join "\n"
15
+ end
16
+
17
+ def render_response
18
+ if response.code == 201
19
+ render_successful_response
20
+ else
21
+ render_error_response
22
+ end
23
+ end
24
+
25
+ def render_error_response
26
+ [
27
+ "There was an error processing your request:",
28
+ "\t* Result: #{response.code} - #{response.title} (#{response.message})",
29
+ "\t* Described by: #{response.described_by}"
30
+ ]
31
+ end
32
+
33
+ def render_successful_response
34
+ result = [
35
+ "Purge request successfully submitted:",
36
+ "\t* Result: #{response.code} - #{response.message}",
37
+ "\t* Purge ID: #{response.purge_id} | Support ID: #{response.support_id}"
38
+ ]
39
+ if response.time_to_wait
40
+ result.concat [
41
+ "\t* Estimated time: #{response.estimated_time} secs.",
42
+ "\t* Progress URI: #{response.uri}",
43
+ "\t* Time to wait before check: #{response.time_to_wait} secs.",
44
+ ]
45
+ end
46
+ result
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,69 @@
1
+ module AkamaiApi::Cli::Ccu
2
+ class StatusRenderer
3
+ attr_reader :response
4
+
5
+ def initialize(response)
6
+ @response = response
7
+ end
8
+
9
+ def render
10
+ res = response.is_a?(AkamaiApi::Ccu::Status::Response) ? render_status : render_purge_status
11
+ [
12
+ '----------',
13
+ res,
14
+ '----------'
15
+ ].join "\n"
16
+ end
17
+
18
+ def render_status
19
+ [
20
+ "Akamai CCU Queue Status",
21
+ "\t* Result: #{response.code} - #{response.message}",
22
+ "\t* Support ID: #{response.support_id}",
23
+ "\t* Queue Length: #{response.queue_length}"
24
+ ].join "\n"
25
+ end
26
+
27
+ def render_purge_status
28
+ if response.is_a? AkamaiApi::Ccu::PurgeStatus::SuccessfulResponse
29
+ render_successful_purge_status
30
+ else
31
+ render_not_found_purge_status
32
+ end
33
+ end
34
+
35
+ def render_successful_purge_status
36
+ output = [
37
+ successful_purge_status_description,
38
+ "\t* Result: #{response.code} - #{response.status}",
39
+ "\t* Purge ID: #{response.purge_id} | Support ID: #{response.support_id}",
40
+ "\t* Submitted by: #{response.submitted_by} on #{response.submitted_at}"
41
+ ]
42
+ if response.completed_at
43
+ output << "\t* Completed on: #{response.completed_at}"
44
+ else
45
+ output << "\t* Estimated time: #{response.estimated_time} secs."
46
+ output << "\t* Queue length: #{response.queue_length}"
47
+ output << "\t* Time to wait before next check: #{response.time_to_wait} secs."
48
+ end
49
+ output.join "\n"
50
+ end
51
+
52
+ def successful_purge_status_description
53
+ if response.completed_at
54
+ "Purge request has been successfully completed:"
55
+ else
56
+ "Purge request is currently enqueued:"
57
+ end
58
+ end
59
+
60
+ def render_not_found_purge_status
61
+ [
62
+ "No purge request found using #{response.progress_uri}:",
63
+ "\t* Result: #{response.code} - #{response.message}",
64
+ "\t* Purge ID: #{response.purge_id} | Support ID: #{response.support_id}",
65
+ "\t* Time to wait before next check: #{response.time_to_wait} secs."
66
+ ].join "\n"
67
+ end
68
+ end
69
+ end
@@ -1,11 +1,6 @@
1
- module AkamaiApi
2
- module Cli
3
- class Ccu < Command
4
- desc 'cpcode', 'CP Code CCU actions'
5
- subcommand 'cpcode', CcuCpCode
1
+ %w(cp_code arl status_renderer purge_renderer base).each do |file|
2
+ require File.expand_path "../ccu/#{file}", __FILE__
3
+ end
6
4
 
7
- desc 'arl', 'ARL CCU actions'
8
- subcommand 'arl', CcuArl
9
- end
10
- end
11
- end
5
+ module AkamaiApi::Cli::Ccu
6
+ end
@@ -0,0 +1,63 @@
1
+ class AkamaiApi::Cli::Eccu
2
+ class EntryRenderer
3
+
4
+ def self.render entries
5
+ output = ["----------"]
6
+ entries.each do |e|
7
+ output.concat new(e).render_entry
8
+ output << "----------"
9
+ end
10
+ output.join "\n"
11
+ end
12
+
13
+ attr_reader :entry
14
+
15
+ def initialize entry
16
+ @entry = entry
17
+ end
18
+
19
+ def render
20
+ [
21
+ "----------",
22
+ render_entry,
23
+ "----------"
24
+ ].join "\n"
25
+ end
26
+
27
+ def render_entry
28
+ output = [
29
+ "* Code : #{entry.code}",
30
+ entry_status,
31
+ " #{entry.status[:update_date]}",
32
+ entry_property
33
+ ]
34
+ output << "* Notes : #{entry.notes}" if entry.notes.present?
35
+ output << "* Email : #{entry.email}" if entry.email
36
+ output << "* Uploaded by #{entry.uploaded_by} on #{entry.upload_date}"
37
+ output << "* Content:\n#{entry.file[:content]}" if entry.file[:content].present?
38
+ output
39
+ end
40
+
41
+ private
42
+
43
+ def entry_property
44
+ base = "* Property: #{entry.property[:name]} (#{entry.property[:type]})"
45
+ if entry.property[:exact_match]
46
+ [
47
+ base,
48
+ " with exact match"
49
+ ].join "\n"
50
+ else
51
+ base
52
+ end
53
+ end
54
+
55
+ def entry_status
56
+ if entry.status[:extended].present?
57
+ "* Status : #{entry.status[:code]} - #{entry.status[:extended]}"
58
+ else
59
+ "* Status : #{entry.status[:code]}"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -7,11 +7,9 @@ module AkamaiApi
7
7
  def requests
8
8
  load_config
9
9
  requests = AkamaiApi::EccuRequest.all :verbose => options[:content]
10
- requests.each do |request|
11
- puts '------------'
12
- puts AkamaiApi::Cli::Template.eccu_request request
13
- end
14
- puts '------------'
10
+ puts EntryRenderer.render requests
11
+ rescue ::AkamaiApi::Unauthorized
12
+ puts "Your login credentials are invalid."
15
13
  end
16
14
 
17
15
  desc 'last_request', 'Print the last request made to ECCU'
@@ -19,10 +17,10 @@ module AkamaiApi
19
17
  :desc => 'Print request content too'
20
18
  def last_request
21
19
  load_config
22
- request = AkamaiApi::EccuRequest.last :verbose => options[:content]
23
- puts '------------'
24
- puts AkamaiApi::Cli::Template.eccu_request request
25
- puts '------------'
20
+ request = AkamaiApi::EccuRequest.last verbose: options[:content]
21
+ puts EntryRenderer.new(request).render
22
+ rescue ::AkamaiApi::Unauthorized
23
+ puts "Your login credentials are invalid."
26
24
  end
27
25
 
28
26
  desc 'publish_xml path/to/request.xml john.com', 'Publish a request made in XML for the specified Digital Property (usually the Host Header)'
@@ -46,11 +44,15 @@ module AkamaiApi
46
44
  :emails => options[:emails]
47
45
  }
48
46
  id = AkamaiApi::EccuRequest.publish_file property, source, args
49
- puts 'Request correctly published. Details:'
50
- puts '------------'
51
- puts AkamaiApi::Cli::Template.eccu_request AkamaiApi::EccuRequest.find id, :verbose => true
52
- puts '------------'
47
+ puts "Request correctly published:"
48
+ puts EntryRenderer.new(AkamaiApi::EccuRequest.find(id, :verbose => true)).render
49
+ rescue ::AkamaiApi::Unauthorized
50
+ puts "Your login credentials are invalid."
51
+ rescue Savon::SOAPFault
52
+ puts $!.message
53
53
  end
54
54
  end
55
55
  end
56
- end
56
+ end
57
+
58
+ require File.expand_path '../eccu/entry_renderer', __FILE__
@@ -1,3 +1,8 @@
1
- %w(template command ccu_cp_code ccu_arl ccu eccu app).each do |file|
1
+ require 'thor'
2
+
3
+ %w(command ccu eccu app).each do |file|
2
4
  require File.expand_path "../cli/#{file}", __FILE__
3
- end
5
+ end
6
+
7
+ module AkamaiApi::Cli
8
+ end
@@ -17,6 +17,9 @@ module AkamaiApi
17
17
  resp = client.call :set_notes, :message => body.to_s
18
18
  self.notes = notes
19
19
  resp.body[:set_notes_response][:success]
20
+ rescue Savon::HTTPError => e
21
+ raise ::AkamaiApi::Unauthorized if e.http.code == 401
22
+ raise
20
23
  end
21
24
 
22
25
  def update_email! email
@@ -29,6 +32,9 @@ module AkamaiApi
29
32
  successful = response.body[:set_status_change_email_response][:success]
30
33
  self.email = email if successful
31
34
  successful
35
+ rescue Savon::HTTPError => e
36
+ raise ::AkamaiApi::Unauthorized if e.http.code == 401
37
+ raise
32
38
  end
33
39
 
34
40
  def destroy
@@ -38,11 +44,17 @@ module AkamaiApi
38
44
  end
39
45
  response = client.call :delete, :message => body.to_s
40
46
  response.body[:delete_response][:success]
47
+ rescue Savon::HTTPError => e
48
+ raise ::AkamaiApi::Unauthorized if e.http.code == 401
49
+ raise
41
50
  end
42
51
 
43
52
  class << self
44
53
  def all_ids
45
54
  client.call(:get_ids).body[:get_ids_response][:file_ids][:file_ids]
55
+ rescue Savon::HTTPError => e
56
+ raise ::AkamaiApi::Unauthorized if e.http.code == 401
57
+ raise
46
58
  end
47
59
 
48
60
  def all args = {}
@@ -89,6 +101,9 @@ module AkamaiApi
89
101
  :uploaded_by => get_if_kind(response_body[:uploaded_by], String),
90
102
  :version_string => get_if_kind(response_body[:version_string], String)
91
103
  })
104
+ rescue Savon::HTTPError => e
105
+ raise ::AkamaiApi::Unauthorized if e.http.code == 401
106
+ raise
92
107
  end
93
108
 
94
109
  def publish_file property, file_name, args = {}
@@ -100,6 +115,9 @@ module AkamaiApi
100
115
  body = build_publish_soap_body property, content, args
101
116
  resp = client.call :upload, :message_tag => 'upload', :message => body.to_s
102
117
  resp.body[:upload_response][:file_id].to_i
118
+ rescue Savon::HTTPError => e
119
+ raise ::AkamaiApi::Unauthorized if e.http.code == 401
120
+ raise
103
121
  end
104
122
 
105
123
  private
@@ -142,4 +160,4 @@ module AkamaiApi
142
160
  self.class.send(:client)
143
161
  end
144
162
  end
145
- end
163
+ end
@@ -1,3 +1,3 @@
1
1
  module AkamaiApi
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/akamai_api.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require 'savon'
2
2
 
3
- %w(version cp_code soap_body ccu_response ccu eccu_request).each do |file|
3
+ %w(version soap_body ccu eccu_request).each do |file|
4
4
  require "akamai_api/#{file}"
5
5
  end
6
6
 
7
7
  module AkamaiApi
8
+ class Unauthorized < StandardError; end
9
+
8
10
  def self.config; @config ||= { :auth => ['', ''], :log => false }; end
9
11
  end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Given I request to purge an asset' do
4
+ subject { AkamaiApi::Ccu }
5
+
6
+ shared_examples 'purge helper' do
7
+ let(:method) { "#{action}_#{type}" }
8
+
9
+ it 'responds with a PurgeResponse object' do
10
+ VCR.use_cassette "akamai_api_ccu_#{type}_#{action}/single_item" do
11
+ subject.send(method, items).should be_a AkamaiApi::Ccu::Purge::Response
12
+ end
13
+ end
14
+
15
+ it 'raises error when user is not authorized' do
16
+ VCR.use_cassette "akamai_api_ccu_#{type}_#{action}/invalid_credentials" do
17
+ AkamaiApi::Ccu.stub auth: { username: 'foo', password: 'bar' }
18
+ expect { subject.send(method, items) }.to raise_error AkamaiApi::Unauthorized
19
+ end
20
+ end
21
+
22
+ it 'returns a different response when data are invalid' do
23
+ VCR.use_cassette "akamai_api_ccu_#{type}_#{action}/invalid_item" do
24
+ response = subject.send(method, items)
25
+ expect(response).to be_a AkamaiApi::Ccu::Purge::Response
26
+ expect(response.code).to eq 403
27
+ end
28
+ end
29
+
30
+ describe 'when data are correct' do
31
+ it 'returns the expected response code' do
32
+ VCR.use_cassette "akamai_api_ccu_#{type}_#{action}/single_item" do
33
+ subject.send(method, items).code.should == 201
34
+ end
35
+ end
36
+
37
+ it 'returns a successful message' do
38
+ VCR.use_cassette "akamai_api_ccu_#{type}_#{action}/single_item" do
39
+ subject.send(method, items).message.should == 'Request accepted.'
40
+ end
41
+ end
42
+
43
+ it 'returns a unique purge id' do
44
+ VCR.use_cassette "akamai_api_ccu_#{type}_#{action}/single_item" do
45
+ subject.send(method, items).purge_id.should == '12345678-1234-1234-1234-123456789012'
46
+ end
47
+ end
48
+
49
+ it 'returns a unique support id' do
50
+ VCR.use_cassette "akamai_api_ccu_#{type}_#{action}/single_item" do
51
+ subject.send(method, items).support_id.should == '12345678901234567890-123456789'
52
+ end
53
+ end
54
+
55
+ it 'returns the estimated time in seconds' do
56
+ VCR.use_cassette "akamai_api_ccu_#{type}_#{action}/single_item" do
57
+ subject.send(method, items).estimated_time.should == 420
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ context 'when this asset is an ARL and I want to invalidate it' do
64
+ let(:action) { 'invalidate' }
65
+ let(:type) { 'arl' }
66
+ let(:items) { ['http://www.foo.bar/t.txt'] }
67
+
68
+ it_should_behave_like 'purge helper'
69
+ end
70
+
71
+ context 'when this asset is a CPCode and I want to invalidate it' do
72
+ let(:action) { 'invalidate' }
73
+ let(:type) { 'cpcode' }
74
+ let(:items) { ['12345'] }
75
+
76
+ it_should_behave_like 'purge helper'
77
+ end
78
+
79
+ context 'when this asset is an ARL and I want to remove it' do
80
+ let(:action) { 'remove' }
81
+ let(:type) { 'arl' }
82
+ let(:items) { ['http://www.foo.bar/t.txt'] }
83
+
84
+ it_should_behave_like 'purge helper'
85
+ end
86
+
87
+ context 'when this asset is a CPCode and I want to remove it' do
88
+ let(:action) { 'remove' }
89
+ let(:type) { 'cpcode' }
90
+ let(:items) { ['12345'] }
91
+
92
+ it_should_behave_like 'purge helper'
93
+ end
94
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Given I request a purge status" do
4
+ subject { AkamaiApi::Ccu }
5
+
6
+ context "when login credentials are ok and progress uri is valid", vcr: { cassette_name: "akamai_api_ccu_purge_status/completed_request" } do
7
+ let(:progress_uri) { '/ccu/v2/purges/12345678-1234-5678-1234-123456789012' }
8
+
9
+ it "responds with a PurgeStatusResponse object" do
10
+ expect(subject.status progress_uri).to be_a AkamaiApi::Ccu::PurgeStatus::SuccessfulResponse
11
+ end
12
+ end
13
+
14
+ context "when login credentials are invalid", vcr: { cassette_name: "akamai_api_ccu_purge_status/invalid_credentials" } do
15
+ let(:progress_uri) { '/ccu/v2/purges/12345678-1234-5678-1234-123456789012' }
16
+
17
+ before do
18
+ AkamaiApi::Ccu.stub auth: { username: 'foo', password: 'bar' }
19
+ end
20
+
21
+ it "raises an error" do
22
+ expect { subject.status progress_uri }.to raise_error AkamaiApi::Unauthorized
23
+ end
24
+ end
25
+
26
+ context "when the given progress uri is invalid", vcr: { cassette_name: "akamai_api_ccu_purge_status/not_found_request" } do
27
+ it "returns a correct response" do
28
+ expect(subject.status 'foobarbaz').to be_a AkamaiApi::Ccu::PurgeStatus::NotFoundResponse
29
+ end
30
+ end
31
+ end