fastly 1.4.3 → 1.5.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: 6e44e269ced0743d4656f7373ccc0c5d1aeee74a
4
- data.tar.gz: 0e665bf5745afc421ce6362c2b23bb9459eecc3a
3
+ metadata.gz: 303ad518137e3776d1eb3d3c8f01248428cb9cae
4
+ data.tar.gz: 4eb4cceb4e98c19e9da337df56ab520b271cdb8c
5
5
  SHA512:
6
- metadata.gz: f0629f64bb97d2608d3d43192434b1a3e1668ad8d7f5deec2cec452157172f57eba1b4af5fddd892b08338d0973047bc8acc307d4cacbf570c85472d00d096df
7
- data.tar.gz: e357b6626db187915cdfed105a40c45550f43d5da05740f41ebf215fecf148481e9f86495286aaa7ad89a6d06aa53879d71824d6cdac99513585c87d4263d620
6
+ metadata.gz: efe685e5ffb5ed74d0817ee3fda2a4e9e4239685e817c20df42a56fa6ac7ae5d53d8f21c2a52a05fb6a4a9454a59121c8b6d8cbaa3a311cd7f632f629dcf1551
7
+ data.tar.gz: e6156561472906f58a67ccd7e6da2586836b6926397371b31732516f05675f32632126d60268d7aa642a2919fcce314af44d2543cb7d841e3ade9b86cf0af288
@@ -2,6 +2,8 @@ language: ruby
2
2
  script: 'bundle exec rake test:unit'
3
3
  sudo: false
4
4
  rvm:
5
- - '1.9.3'
6
- - '2.1.1'
7
- - '2.3.0'
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1.9
8
+ - 2.2.5
9
+ - 2.3.1
@@ -14,6 +14,7 @@ require 'fastly/cache_setting'
14
14
  require 'fastly/condition'
15
15
  require 'fastly/customer'
16
16
  require 'fastly/dictionary'
17
+ require 'fastly/dictionary_item'
17
18
  require 'fastly/director'
18
19
  require 'fastly/domain'
19
20
  require 'fastly/header'
@@ -46,6 +47,10 @@ class Fastly
46
47
  #
47
48
  # Some methods require full username and password rather than just auth token.
48
49
  def initialize(opts)
50
+ if opts[:api_key].nil? && (opts[:password].nil? || opts[:user].nil?)
51
+ raise ArgumentError, "Required options missing. Please pass either ':api_key' or both ':user' and ':password'."
52
+ end
53
+
49
54
  client(opts)
50
55
  self
51
56
  end
@@ -138,7 +143,7 @@ class Fastly
138
143
  client.get_stats('/stats/regions')
139
144
  end
140
145
 
141
- [User, Customer, Backend, CacheSetting, Condition, Dictionary, Director, Domain, Header, Healthcheck, Gzip, Match, Origin, RequestSetting, ResponseObject, Service, S3Logging, Syslog, VCL, Version].each do |klass|
146
+ [User, Customer, Backend, CacheSetting, Condition, Dictionary, DictionaryItem, Director, Domain, Header, Healthcheck, Gzip, Match, Origin, RequestSetting, ResponseObject, Service, S3Logging, Syslog, VCL, Version].each do |klass|
142
147
  type = Util.class_to_path(klass)
143
148
 
144
149
  if klass.respond_to?(:pluralize)
@@ -182,6 +187,10 @@ class Fastly
182
187
  # :method: create_dictionary(opts)
183
188
  # opts must contain service_id, version and name params
184
189
 
190
+ ##
191
+ # :method: create_dictionary_item(opts)
192
+ # opts must contain service_id, dictionary_id, item_key and item_value params
193
+
185
194
  ##
186
195
  # :method: create_director(opts)
187
196
  # opts must contain service_id, version and name params
@@ -262,6 +271,10 @@ class Fastly
262
271
  # :method: get_dictionary(service_id, number, name)
263
272
  # Get a single dictionary
264
273
 
274
+ ##
275
+ # :method: get_dictionary_item(service_id, dictionary_id, name)
276
+ # Get a single dictionary item
277
+
265
278
  ##
266
279
  # :method: get_director(service_id, number, name)
267
280
  # Get a Director
@@ -356,6 +369,11 @@ class Fastly
356
369
  # You can also call
357
370
  # dictionary.save!
358
371
 
372
+ ##
373
+ # :method: update_dictionary_item(dictionary_item)
374
+ # You can also call
375
+ # dictionary_item.save!
376
+
359
377
  ##
360
378
  # :method: update_director(director)
361
379
  # You can also call
@@ -466,6 +484,11 @@ class Fastly
466
484
  # You can also call
467
485
  # dictionary.delete!
468
486
 
487
+ ##
488
+ # :method: delete_dictionary_item(dictionary_item)
489
+ # You can also call
490
+ # dictionary_item.delete!
491
+
469
492
  ##
470
493
  # :method: delete_director(backend)
471
494
  # You can also call
@@ -569,6 +592,10 @@ class Fastly
569
592
  #
570
593
  # Get a list of all dictionaries
571
594
 
595
+ # :method: list_dictionary_items(:service_id => service.id, :dictionary_id => dictionary.name)
596
+ #
597
+ # Get a list of all items belonging to a dictionary
598
+
572
599
  # :method: list_domains(:service_id => service.id, :version => version.number)
573
600
  #
574
601
  # Get a list of all domains
@@ -6,6 +6,9 @@ require 'openssl'
6
6
  class Fastly
7
7
  # The UserAgent to communicate with the API
8
8
  class Client #:nodoc: all
9
+
10
+ DEFAULT_URL = 'https://api.fastly.com'.freeze
11
+
9
12
  attr_accessor :http, :api_key, :user, :password, :cookie, :customer
10
13
 
11
14
  def initialize(opts)
@@ -15,7 +18,7 @@ class Fastly
15
18
  @customer = opts.fetch(:customer, nil)
16
19
  @oldpurge = opts.fetch(:use_old_purge_method, false)
17
20
 
18
- base = opts.fetch(:base_url, 'https://api.fastly.com')
21
+ base = opts.fetch(:base_url, DEFAULT_URL)
19
22
  uri = URI.parse(base)
20
23
  options = if uri.is_a? URI::HTTPS
21
24
  {
@@ -2,6 +2,38 @@ class Fastly
2
2
  class Dictionary < BelongsToServiceAndVersion
3
3
  attr_accessor :id, :name, :service_id
4
4
 
5
+ def items
6
+ fetcher.list_dictionary_items(:service_id => service_id, :dictionary_id => id)
7
+ end
8
+
9
+ # Returns a Fastly::DictionaryItem corresponding to this dictionary and the +key+
10
+ #
11
+ # * +key+ - Key of the dictionary item
12
+ def item(key)
13
+ fetcher.get_dictionary_item(service_id, id, key)
14
+ rescue Fastly::Error => e
15
+ raise unless e.message =~ /Record not found/
16
+ end
17
+
18
+ def add_item(key, value)
19
+ fetcher.create_dictionary_item(service_id: service_id, dictionary_id: id, item_key: key, item_value: value)
20
+ end
21
+
22
+ def update_item(key, value)
23
+ di = items.select {|item| item.item_key.eql? key }.first
24
+ if di
25
+ di.item_value = value
26
+ fetcher.update_dictionary_item(di)
27
+ else
28
+ add_item(key, value)
29
+ end
30
+ end
31
+
32
+ def delete_item(key)
33
+ di = items.select {|item| item.item_key.eql? key }.first
34
+ fetcher.delete_dictionary_item(di) if di
35
+ end
36
+
5
37
  def self.pluralize
6
38
  'dictionaries'
7
39
  end
@@ -0,0 +1,40 @@
1
+ require 'cgi'
2
+
3
+ class Fastly
4
+ class DictionaryItem < Base
5
+ attr_accessor :dictionary_id, :item_key, :item_value, :service_id
6
+
7
+ alias_method :key, :item_key
8
+ alias_method :value, :item_value
9
+
10
+ # Return the Service object this belongs to
11
+ def service
12
+ @service ||= fetcher.get(Service, service_id)
13
+ end
14
+
15
+ # :nodoc:
16
+ def as_hash
17
+ super.delete_if { |var| %w(service_id dictionary_id).include?(var) }
18
+ end
19
+
20
+ def self.get_path(service, dictionary_id, item_key, _opts = {})
21
+ "/service/#{service}/dictionary/#{dictionary_id}/item/#{CGI::escape(item_key)}"
22
+ end
23
+
24
+ def self.post_path(opts)
25
+ "/service/#{opts[:service_id]}/dictionary/#{opts[:dictionary_id]}/item"
26
+ end
27
+
28
+ def self.put_path(obj)
29
+ get_path(obj.service_id, obj.dictionary_id, obj.item_key)
30
+ end
31
+
32
+ def self.delete_path(obj)
33
+ put_path(obj)
34
+ end
35
+
36
+ def self.list_path(opts = {})
37
+ "/service/#{opts[:service_id]}/dictionary/#{opts[:dictionary_id]}/items"
38
+ end
39
+ end
40
+ end
@@ -1,4 +1,4 @@
1
1
  # The current version of the library
2
2
  class Fastly
3
- VERSION = "1.4.3"
3
+ VERSION = "1.5.0"
4
4
  end
@@ -1,9 +1,9 @@
1
- require 'test_helper'
1
+ require_relative '../test_helper'
2
2
 
3
3
  describe Fastly::Client do
4
- let(:user) { "test@example.com" }
5
- let(:password) { "notasecret" }
6
- let(:api_key) { "notasecreteither" }
4
+ let(:user) { "test@example.com" }
5
+ let(:password) { "notasecret" }
6
+ let(:api_key) { "notasecreteither" }
7
7
 
8
8
  describe 'initialize' do
9
9
  it 'raises ArgumentError when no options provided' do
@@ -45,6 +45,17 @@ describe Fastly::Client do
45
45
  client = Fastly::Client.new(user: user, password: pass)
46
46
  assert_equal "tasty!", client.cookie
47
47
  end
48
+
49
+ it 'raises an Error if username is used in place of user as an option' do
50
+ stub_request(:any, /api.fastly.com/).
51
+ to_return(body: JSON.generate(i: "dont care"), status: 200, headers: { 'Set-Cookie' => 'tasty!' })
52
+
53
+ assert_raises(ArgumentError) { Fastly.new(username: user, password: pass) }
54
+
55
+ Fastly.new(user: user, password: pass)
56
+ Fastly.new(api_key: api_key)
57
+ Fastly.new(api_key: api_key, user: user, password: pass)
58
+ end
48
59
  end
49
60
 
50
61
  describe 'get' do
@@ -0,0 +1,53 @@
1
+ require_relative '../test_helper'
2
+
3
+ describe Fastly::Dictionary do
4
+
5
+ let(:client) { Fastly.new(user: 'test@example.com', password: 'password') }
6
+ let(:service_id) { SecureRandom.hex(6) }
7
+ let(:version) { 1 }
8
+ let(:dictionary) { Fastly::Dictionary.new({id: SecureRandom.hex(6), service_id: service_id, version: 1}, client) }
9
+
10
+ before {
11
+ stub_request(:post, "#{Fastly::Client::DEFAULT_URL}/login").to_return(body: '{}', status: 200, headers: { 'Set-Cookie' => 'tasty!' })
12
+ }
13
+
14
+ describe '#item' do
15
+ it 'returns the nil when item is not present' do
16
+ item_key = 'key'
17
+ get_item_url = "#{Fastly::Client::DEFAULT_URL}/service/#{service_id}/dictionary/#{dictionary.id}/item/#{item_key}"
18
+
19
+ response_body = JSON.dump(
20
+ "msg" => "Record not found",
21
+ "detail" => "Couldn't find dictionary item '{ service => #{service_id}, dictionary_id => #{dictionary.id}, item_key => #{item_key}, deleted => 0000-00-00 00:00:00'",
22
+ )
23
+
24
+ stub_request(:get, get_item_url).to_return(body: response_body, status: 404)
25
+
26
+ assert_nil dictionary.item('key')
27
+ end
28
+
29
+ it 'returns the corresponding dictionary item when present' do
30
+ item_key = 'key'
31
+ item_value = 'value'
32
+
33
+ response_body = JSON.dump(
34
+ "dictionary_id" => dictionary.id,
35
+ "service_id" => service_id,
36
+ "item_key" => item_key,
37
+ "item_value" => item_value,
38
+ "created_at" => "2016-04-21T18:14:32+00:00",
39
+ "deleted_at" => nil,
40
+ "updated_at" => "2016-04-21T18:14:32+00:00",
41
+ )
42
+
43
+ get_item_url = "#{Fastly::Client::DEFAULT_URL}/service/#{service_id}/dictionary/#{dictionary.id}/item/#{item_key}"
44
+
45
+ stub_request(:get, get_item_url).to_return(body: response_body, status: 200)
46
+
47
+ item = dictionary.item('key')
48
+
49
+ assert_equal item_key, item.key
50
+ assert_equal item_value, item.value
51
+ end
52
+ end
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.4.3
4
+ version: 1.5.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-05-27 00:00:00.000000000 Z
11
+ date: 2016-07-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Client library for the Fastly acceleration system
14
14
  email:
@@ -42,6 +42,7 @@ files:
42
42
  - lib/fastly/condition.rb
43
43
  - lib/fastly/customer.rb
44
44
  - lib/fastly/dictionary.rb
45
+ - lib/fastly/dictionary_item.rb
45
46
  - lib/fastly/director.rb
46
47
  - lib/fastly/domain.rb
47
48
  - lib/fastly/fetcher.rb
@@ -66,6 +67,7 @@ files:
66
67
  - test/api_key_test.rb
67
68
  - test/common.rb
68
69
  - test/fastly/client_test.rb
70
+ - test/fastly/dictionary_test.rb
69
71
  - test/fastly/util_test.rb
70
72
  - test/full_login_test.rb
71
73
  - test/helper.rb
@@ -101,9 +103,11 @@ test_files:
101
103
  - test/api_key_test.rb
102
104
  - test/common.rb
103
105
  - test/fastly/client_test.rb
106
+ - test/fastly/dictionary_test.rb
104
107
  - test/fastly/util_test.rb
105
108
  - test/full_login_test.rb
106
109
  - test/helper.rb
107
110
  - test/missing_api_key_test.rb
108
111
  - test/stats_test.rb
109
112
  - test/test_helper.rb
113
+ has_rdoc: