fastly 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41770bdcf1d13d916bbc1c33620497ced6259270
4
- data.tar.gz: ddf4eb5b4b18d0932ddf340dffe73571ac47f68b
3
+ metadata.gz: 6d5c728bbd87d08a643b21e96f1fb7876e6ff740
4
+ data.tar.gz: 7a140e024d66ce6d6117f60ad7e0b13aa8d6094a
5
5
  SHA512:
6
- metadata.gz: 9602cf07f2d1f405f13afb0ea0aca1238722ebf5539b0c1e8079058b487027d541ce29d83f31145fcc4fb3e7c7e40c7e48976eac113265c8149dfa089c92890f
7
- data.tar.gz: 529cc34f51929cf9dbb8b876002a79709348427ba57b1e4e698fa7f5d20549d11188b88714c9535b2a83446acf96b58446728de4ec0c99f526c5cd2aacaac9c7
6
+ metadata.gz: 18e471560d67453d3c1fe17f91542050f1e8feb5aac1b2566b0bace8703e1ac50c8fd6eb55803c670f6f1aab345edb2661995be4b62caaad7f6e9e228398d136
7
+ data.tar.gz: 18da338c2b1c674c2c1bc982ba361751b3c032efb5329a4b67e70fabddd52bfa43cc7f470f73597eb5a1a46586fb9577d3f127daa61a94d606b13888143ae302
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ### 1.4.0 / 2016-05-09
4
+ * Adds support for soft purge for a service
5
+
3
6
  ### 1.3.0 / 2016-04-04
4
7
  * Adds missing fields for Backend, Healthcheck, S3, & Syslog (@raybejjani)
5
8
  * URL escape names when used in URLs (@raybejjani)
data/README.md CHANGED
@@ -132,21 +132,44 @@ every time you issue a purge:
132
132
  fastly = Fastly.new(api_key: 'YOUR_API_KEY')
133
133
  service = Fastly::Service.new({ id: 'YOUR_SERVICE_ID' }, fastly)
134
134
 
135
+ # purge an individual url
136
+ fastly.purge(url)
137
+
135
138
  # purge everything:
136
139
  service.purge_all
137
140
 
138
141
  # purge by key:
139
142
  service.purge_by_key('YOUR_SURROGATE_KEY')
143
+
144
+ # 'soft' purging
145
+ # see https://docs.fastly.com/guides/purging/soft-purges
146
+ fastly.purge(url, true)
147
+ service.purge_by_key('YOUR_SURROGATE_KEY', true)
140
148
  ```
141
149
 
142
- You can also purge without involving the Fastly client at all by sending a POST
143
- request with your Fastly API key in a `Fastly-Key` header:
150
+ You can also purge without involving the Fastly client by sending a PURGE request directly
151
+ to the URL you want to purge. You can also send a POST request to the API with your Fastly API key
152
+ in a `Fastly-Key` header:
144
153
 
145
154
  ```
155
+ curl -X PURGE YOUR URL
156
+
146
157
  curl -H 'Fastly-Key: YOUR_API_KEY' -X POST \
147
158
  https://api.fastly.com/service/YOUR_SERVICE_ID/purge/YOUR_SURROGATE_KEY
148
159
  ```
149
160
 
161
+ Previously purging made an POST call to the `/purge` endpoint of the Fastly API.
162
+
163
+ The new method of purging is done by making an HTTP request against the URL using the `PURGE` HTTP method.
164
+
165
+ This gem now uses the new method. The old method can be used by passing the `use_old_purge_method` option into the constructor.
166
+
167
+ ```ruby
168
+ fastly = Fastly.new(login_opts.merge(use_old_purge_method: true))
169
+ fastly.purge(url, true)
170
+ service.purge_by_key('YOUR_SURROGATE_KEY', true)
171
+ ```
172
+
150
173
  See the [Fastly purging API documentation](https://docs.fastly.com/api/purge)
151
174
  for more information and examples.
152
175
 
data/lib/fastly.rb CHANGED
@@ -74,8 +74,8 @@ class Fastly
74
74
  end
75
75
 
76
76
  # Purge the specified path from your cache.
77
- def purge(path)
78
- client.post("/purge/#{path}")
77
+ def purge(url, soft=false)
78
+ client.purge(url, soft ? { headers: { 'Fastly-Soft-Purge' => "1"} } : {})
79
79
  end
80
80
 
81
81
  # Fetches historical stats for each of your fastly services and groups the results by service id.
data/lib/fastly/client.rb CHANGED
@@ -13,6 +13,7 @@ class Fastly
13
13
  @user = opts.fetch(:user, nil)
14
14
  @password = opts.fetch(:password, nil)
15
15
  @customer = opts.fetch(:customer, nil)
16
+ @oldpurge = opts.fetch(:use_old_purge_method, false)
16
17
 
17
18
  base = opts.fetch(:base_url, 'https://api.fastly.com')
18
19
  uri = URI.parse(base)
@@ -60,8 +61,9 @@ class Fastly
60
61
  end
61
62
 
62
63
  def get(path, params = {})
64
+ extras = params.delete(:headers) || {}
63
65
  path += "?#{make_params(params)}" unless params.empty?
64
- resp = http.get(path, headers)
66
+ resp = http.get(path, headers(extras))
65
67
  fail Error, resp.body unless resp.kind_of?(Net::HTTPSuccess)
66
68
  JSON.parse(resp.body)
67
69
  end
@@ -85,23 +87,37 @@ class Fastly
85
87
  post_and_put(:put, path, params)
86
88
  end
87
89
 
88
- def delete(path)
89
- resp = http.delete(path, headers)
90
+ def delete(path, params = {})
91
+ extras = params.delete(:headers) || {}
92
+ resp = http.delete(path, headers(extras))
90
93
  resp.kind_of?(Net::HTTPSuccess)
91
94
  end
92
95
 
96
+ def purge(url, params = {})
97
+ return post("/purge/#{url}", params) if @oldpurge
98
+
99
+ extras = params.delete(:headers) || {}
100
+ uri = URI.parse(url)
101
+ http = Net::HTTP.new(uri.host, uri.port)
102
+ resp = http.request Net::HTTP::Purge.new(uri.request_uri, headers(extras))
103
+
104
+ fail Error, resp.body unless resp.kind_of?(Net::HTTPSuccess)
105
+ JSON.parse(resp.body)
106
+ end
107
+
93
108
  private
94
109
 
95
110
  def post_and_put(method, path, params = {})
111
+ extras = params.delete(:headers) || {}
96
112
  query = make_params(params)
97
- resp = http.send(method, path, query, headers.merge('Content-Type' => 'application/x-www-form-urlencoded'))
113
+ resp = http.send(method, path, query, headers(extras).merge('Content-Type' => 'application/x-www-form-urlencoded'))
98
114
  fail Error, resp.body unless resp.kind_of?(Net::HTTPSuccess)
99
115
  JSON.parse(resp.body)
100
116
  end
101
117
 
102
- def headers
118
+ def headers(extras={})
103
119
  headers = fully_authed? ? { 'Cookie' => cookie } : { 'Fastly-Key' => api_key }
104
- headers.merge('Content-Accept' => 'application/json')
120
+ headers.merge('Content-Accept' => 'application/json').merge(extras.keep_if {|k,v| !v.nil? })
105
121
  end
106
122
 
107
123
  def make_params(params)
@@ -122,3 +138,10 @@ class Fastly
122
138
  end
123
139
  end
124
140
  end
141
+
142
+ # See Net::HTTPGenericRequest for attributes and methods.
143
+ class Net::HTTP::Purge < Net::HTTPRequest
144
+ METHOD = 'PURGE'
145
+ REQUEST_HAS_BODY = false
146
+ RESPONSE_HAS_BODY = true
147
+ end
@@ -1,4 +1,4 @@
1
1
  # The current version of the library
2
2
  class Fastly
3
- VERSION = "1.3.0"
3
+ VERSION = "1.4.0"
4
4
  end
@@ -69,9 +69,9 @@ class Fastly
69
69
  # Purge anything with the specific key from the given service.
70
70
  #
71
71
  # See README.md for examples of purging
72
- def purge_by_key(key)
72
+ def purge_by_key(key, soft=false)
73
73
  require_api_key!
74
- fetcher.client.post("#{Service.get_path(id)}/purge/#{key}")
74
+ fetcher.client.post("#{Service.get_path(id)}/purge/#{key}", soft ? { headers: { 'Fastly-Soft-Purge' => "1"} } : {})
75
75
  end
76
76
 
77
77
  # Get a sorted array of all the versions that this service has had.
data/test/api_key_test.rb CHANGED
@@ -22,6 +22,32 @@ class Fastly
22
22
  assert_instance_of Hash, client.get('/current_customer')
23
23
  assert_instance_of Customer, fastly.current_customer
24
24
  end
25
+
26
+ describe 'purging' do
27
+ before do
28
+ @opts = login_opts(:api_key)
29
+ @client = Fastly::Client.new(@opts)
30
+ @fastly = Fastly.new(@opts)
31
+ service_name = "fastly-test-service-#{random_string}"
32
+ @service = @fastly.create_service(:name => service_name)
33
+ end
34
+
35
+ after do
36
+ @fastly.delete_service(@service)
37
+ end
38
+
39
+ it 'allows purging' do
40
+ response = @service.purge_by_key('somekey')
41
+
42
+ assert_equal 'ok', response['status']
43
+ end
44
+
45
+ it 'allows soft purging' do
46
+ response = @service.purge_by_key('somekey', soft: true)
47
+
48
+ assert_equal 'ok', response['status']
49
+ end
50
+ end
25
51
  end
26
52
  end
27
53
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastly
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fastly
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-04 00:00:00.000000000 Z
11
+ date: 2016-05-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Client library for the Fastly acceleration system
14
14
  email: