Skip to content
This repository was archived by the owner on Jan 31, 2019. It is now read-only.

Commit 89008c7

Browse files
committed
importing maxcdn, fixes #820
1 parent 4fafefc commit 89008c7

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

docs/maxcdn

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Purge your MaxCDN Pull Zone
2+
---------------------------
3+
4+
**Setup Notes**
5+
6+
1. Get your "Company Alias", "Consumer Key" and "Consumer Secret" from your [MaxCDN Account API Page][account_api_url].
7+
2. Get your "Zone ID" from your [MaxCDN Account Pull Zones Overview page][account_pull_zone].
8+
3. "Static Only" will only purge your zone if modified files include static files &mdash `css`, `js`, `jpg`, `jpeg`, `gif`, `ico`, `png`, `bmp`, `pict`, `csv`, `doc`, `pdf`, `pls`, `ppt`, `tif`, `tiff`, `eps`, `ejs`, `swf`, `midi`, `mid`, `txt`, `ttf`, `eot`, `woff`, `otf`, `svg`, `svgz`, `webp`, `docx`, `xlsx`, `xls`, `pptx`, `ps`, `rss`, `class`, `jar`.
9+
4. Whitelist the Github web hook IP block with MaxCDN. For instructions, see MaxCDN's support page on [How To Whitelsit Your Server IP To Use The API][whitelist_article].
10+
11+
> Github's IPs may change from time to time, if you're having issues, verify that the IP you've whitelisted is still current, by checking the "hook" key at [Github's "meta" API endpoint][meta_endpoint].
12+
13+
[account_api_url]: https://cp.maxcdn.com/account/api
14+
[account_pull_zone]: https://cp.maxcdn.com/zones/pull
15+
[whitelist_article]: http://support.maxcdn.com/tutorials/how-to-whitelist-your-server-ip-to-use-the-api/
16+
[meta_endpoint]: https://api.github.com/meta
17+

github-services.gemspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Gem::Specification.new do |spec|
3333
# Twitter
3434
spec.add_dependency "oauth", "0.4.4"
3535

36+
# MaxCDN
37+
spec.add_dependency "maxcdn", "~> 0.1.6"
38+
3639
# Campfire
3740
spec.add_dependency "tinder", "1.8.0.github"
3841

lib/services/maxcdn.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require "maxcdn"
2+
3+
class Service::MaxCDN < Service
4+
5+
STATIC_EXTENSIONS = [
6+
:css, :js, :jpg, :jpeg, :gif, :ico, :png, :bmp,
7+
:pict, :csv, :doc, :pdf, :pls, :ppt, :tif, :tiff,
8+
:eps, :ejs, :swf, :midi, :mid, :txt, :ttf, :eot,
9+
:woff, :otf, :svg, :svgz, :webp, :docx, :xlsx,
10+
:xls, :pptx, :ps, :rss, :class, :jar
11+
].freeze
12+
13+
string :alias,
14+
:key,
15+
:secret,
16+
:zone_id
17+
18+
boolean :static_only
19+
20+
url "http://docs.maxcdn.com/"
21+
logo_url "http://www.maxcdn.com/wp-content/themes/maxcdnv4/img/png/maxcdn-colored-logo.png"
22+
23+
maintained_by :github => "jmervine",
24+
:email => "joshua@mervine.net",
25+
:twitter => "@mervinej"
26+
27+
supported_by :web => "http://support.maxcdn.com/",
28+
:email => "support@maxcdn.com",
29+
:twitter => "@MaxCDN"
30+
31+
def receive_push
32+
return unless payload["commits"]
33+
return if data["static_only"] and has_static?
34+
35+
begin
36+
maxcdn.purge data["zone_id"]
37+
rescue ::MaxCDN::APIException => e
38+
raise_config_error(e.message)
39+
end
40+
end
41+
42+
def modified_files
43+
payload["commits"].map { |commit| commit["modified"] }.flatten!.uniq!
44+
end
45+
46+
def extensions
47+
::Service::MaxCDN::STATIC_EXTENSIONS
48+
end
49+
50+
def has_static?
51+
files = modified_files.clone.select! do |file|
52+
matched = false
53+
extensions.each do |ext|
54+
matched = true if /#{ext}/.match(file)
55+
end
56+
matched
57+
end
58+
59+
(files.size > 0)
60+
end
61+
62+
def maxcdn
63+
@maxcdn ||= ::MaxCDN::Client.new(data["alias"], data["key"], data["secret"])
64+
end
65+
end
66+

test/maxcdn_test.rb

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
require File.expand_path("../helper", __FILE__)
2+
3+
# stub
4+
require "maxcdn"
5+
module MaxCDN
6+
class Client
7+
def initialize *args
8+
@purge_calls = 0
9+
@fake_error = false
10+
end
11+
12+
def purge id
13+
raise ::MaxCDN::APIException.new("test error") if @fake_error
14+
15+
@purge_calls += 1
16+
return { "code" => "200" }
17+
end
18+
19+
def fake_error
20+
@fake_error = true
21+
end
22+
end
23+
end
24+
25+
class MaxCDNTest < Service::TestCase
26+
def setup
27+
@arguments ||= {
28+
"alias" => "foobar_alias",
29+
"key" => "foobar_key",
30+
"secret" => "foobar_secret",
31+
"zone_id" => 123456,
32+
"static_only" => false
33+
}
34+
35+
@svc = service(@arguments, dynamic_payload)
36+
end
37+
38+
def test_maxcdn
39+
assert @svc.maxcdn
40+
end
41+
42+
def test_extensions
43+
assert_includes @svc.extensions, :js
44+
end
45+
46+
def test_has_static?
47+
refute @svc.has_static?
48+
49+
svc = service(@arguments, static_payload)
50+
assert svc.has_static?
51+
end
52+
53+
def test_receive_push
54+
assert @svc.receive_push
55+
assert_equal 1, @svc.maxcdn.instance_variable_get(:@purge_calls)
56+
57+
@svc.maxcdn.fake_error
58+
error = assert_raises ::Service::ConfigurationError do
59+
@svc.receive_push
60+
end
61+
62+
assert_match /test error/, error.message
63+
64+
arguments = @arguments.clone
65+
arguments["static_only"] = true
66+
svc = service(arguments, static_payload)
67+
68+
refute svc.receive_push
69+
end
70+
71+
def dynamic_payload
72+
unless defined? @dynamic_payload
73+
# Default payload is all .rb files and thus, a
74+
# non-static payload. However, to be sure (should
75+
# something change in the future) I'll ensure it.
76+
@dynamic_payload = payload.clone
77+
@dynamic_payload["commits"].each_index do |commit|
78+
@dynamic_payload["commits"][commit]["modified"].each_index do |file|
79+
@dynamic_payload["commits"][commit]["modified"][file].gsub!(/\.[a-z0-9]+$/, ".rb")
80+
end
81+
end
82+
end
83+
@dynamic_payload
84+
end
85+
86+
def static_payload
87+
unless defined? @static_payload
88+
# Creating a static payload, by replacing a single
89+
# file in the payload with a static file extension.
90+
@static_payload = payload.clone
91+
@static_payload["commits"]
92+
.first["modified"]
93+
.last
94+
.gsub!(/\.[a-z0-9]+$/, ".js")
95+
end
96+
@static_payload
97+
end
98+
99+
def service(*args)
100+
super Service::MaxCDN, *args
101+
end
102+
end

0 commit comments

Comments
 (0)