cloudflair 0.0.4 → 0.0.5

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: 381b97720e882dc8ee8d05e5336f19839650f1df
4
- data.tar.gz: b43756bfe13ca34d5a02d88d81431492a04a07b6
3
+ metadata.gz: 5544a93b5a42bf842ac8e55baa5d7fcb4bc19702
4
+ data.tar.gz: 7664480c03564666fb4ab299aa8c75a51357cddd
5
5
  SHA512:
6
- metadata.gz: af84248ed5de70250c0283dec79973c0dc09004ded9e4caf83db88226701471931780f9682d43e33be719c917b01fd835014f6a8aa97f590b5c506ab88435a30
7
- data.tar.gz: c7fd3d70509c80edb7a8204654d078ad4c067057b3b45813d50ec4d4efeb70a1860538abdef76d87ba0943de828f47911bc2a57bc5a86f6532f0e8d831bc3c44
6
+ metadata.gz: 48a1afcdf0395bf0fe4c71e298861ab67f2b1435dde27f8b4653f3bd53941a8720e3a4c52ffacfe9adafab3e0f0eb14f197479495b530dd4c91e2a1fb294f8e0
7
+ data.tar.gz: 13f6ad242a69ad4991ea15e39382c38e26b1e0f0219e4435025925ffaeff81d9e633b343ad87e75eb021394a296f7e8821f297c60450cd4b50430cc227146a2c
data/README.md CHANGED
@@ -43,6 +43,8 @@ Cloudflair.configure do |config|
43
43
  # these are optional:
44
44
  config.cloudflare.api_base_url = 'https://your_cloudflare_mock.local'
45
45
  config.faraday.adapter = :your_preferred_faraday_adapter
46
+ # built-in options: :logger, :detailed_logger; default: nil
47
+ config.faraday.logger = :logger
46
48
  end
47
49
  ```
48
50
 
data/cloudflair.gemspec CHANGED
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_runtime_dependency 'faraday', '~> 0.9.0'
25
25
  spec.add_runtime_dependency 'faraday_middleware', '~> 0.10.0'
26
26
  spec.add_runtime_dependency 'dry-configurable', '~> 0.1'
27
+ spec.add_runtime_dependency 'faraday-detailed_logger', '~> 2.1.0'
27
28
 
28
29
  spec.add_development_dependency 'bundler', '~> 1.12'
29
30
  spec.add_development_dependency 'rake', '~> 10.0'
@@ -10,18 +10,26 @@ module Cloudflair
10
10
  @zone_id = zone_id
11
11
  end
12
12
 
13
- def purge_all_files(purge_everything=false)
14
- response connection.delete path, purge_everything: purge_everything
13
+ ##
14
+ # @param purge_everything must be set to true
15
+ def everything(purge_everything)
16
+ resp = connection.delete(path) { |req| req.body = { purge_everything: purge_everything } }
17
+ response resp
18
+ self
15
19
  end
16
20
 
17
21
  ##
18
22
  # @param [Hash] cache_identifier Consists of :files and :tags, which are String Arrays themselves. Sample: <code>{files: ['https://foo.bar/index.htmll'], tags: ['css','js']}
19
- def purge(cache_identifier = {})
23
+ def selective(cache_identifier = {})
24
+ return self if cache_identifier.nil? || cache_identifier.empty?
20
25
 
26
+ resp = connection.delete(path) { |req| req.body = cache_identifier }
27
+ response resp
28
+ self
21
29
  end
22
30
 
23
- def connection
24
- Connection.new
31
+ def path
32
+ "zones/#{zone_id}/purge_cache"
25
33
  end
26
34
  end
27
35
  end
@@ -7,15 +7,11 @@ module Cloudflair
7
7
  attr_accessor :zone_id
8
8
 
9
9
  patchable_fields :value
10
+ path 'zones/:zone_id/settings/development_mode'
10
11
 
11
12
  def initialize(zone_id)
12
13
  @zone_id = zone_id
13
14
  end
14
15
 
15
- private
16
-
17
- def path
18
- "zones/#{zone_id}/settings/development_mode"
19
- end
20
16
  end
21
17
  end
@@ -1,11 +1,8 @@
1
1
  require 'cloudflair/api/zone/settings'
2
+ require 'cloudflair/api/zone/purge_cache'
2
3
  require 'cloudflair/entity'
3
4
 
4
5
  module Cloudflair
5
- def self.zone(zone_id)
6
- Zone.new zone_id
7
- end
8
-
9
6
  class Zone
10
7
  include Cloudflair::Entity
11
8
 
@@ -13,6 +10,7 @@ module Cloudflair
13
10
 
14
11
  patchable_fields :paused, :vanity_name_servers, :plan
15
12
  deletable true
13
+ path 'zones/:zone_id'
16
14
 
17
15
  def initialize(zone_id)
18
16
  @zone_id = zone_id
@@ -22,10 +20,8 @@ module Cloudflair
22
20
  @settings ||= Cloudflair::Settings.new zone_id
23
21
  end
24
22
 
25
- private
26
-
27
- def path
28
- "zones/#{zone_id}"
23
+ def purge_cache
24
+ @purge_cache ||= Cloudflair::PurgeCache.new zone_id
29
25
  end
30
26
  end
31
27
  end
@@ -0,0 +1,7 @@
1
+ require 'cloudflair/api/zone'
2
+
3
+ module Cloudflair
4
+ def self.zone(zone_id)
5
+ Zone.new zone_id
6
+ end
7
+ end
@@ -5,6 +5,20 @@ require 'cloudflair/connection'
5
5
  module Cloudflair
6
6
  module Communication
7
7
  def response(response)
8
+ return nil if response.status == 304
9
+
10
+ http_error? response.status
11
+
12
+ parse response
13
+ end
14
+
15
+ def connection
16
+ Cloudflair::Connection.new
17
+ end
18
+
19
+ private
20
+
21
+ def parse(response)
8
22
  body = response.body
9
23
 
10
24
  unless body['success']
@@ -15,8 +29,29 @@ module Cloudflair
15
29
  body['result']
16
30
  end
17
31
 
18
- def connection
19
- Cloudflair::Connection.new
32
+ def http_error?(status)
33
+ case status
34
+ when 200..399 then
35
+ return
36
+ when 400 then
37
+ fail Cloudflair::CloudflairError, '400 Bad Request'
38
+ when 401 then
39
+ fail Cloudflair::CloudflairError, '401 Unauthorized'
40
+ when 403 then
41
+ fail Cloudflair::CloudflairError, '403 Forbidden'
42
+ when 429 then
43
+ fail Cloudflair::CloudflairError, '429 Too Many Requests'
44
+ when 405 then
45
+ fail Cloudflair::CloudflairError, '405 Method Not Allowed'
46
+ when 415 then
47
+ fail Cloudflair::CloudflairError, '415 Unsupported Media Type'
48
+ when 400..499 then
49
+ fail Cloudflair::CloudflairError, "#{status} Request Error"
50
+ when 500..599 then
51
+ fail Cloudflair::CloudflairError, "#{status} Remote Error"
52
+ else
53
+ fail Cloudflair::CloudflairError, "#{status} Unknown Error Code"
54
+ end
20
55
  end
21
56
  end
22
57
  end
@@ -1,3 +1,7 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'faraday/detailed_logger'
4
+
1
5
  module Cloudflair
2
6
  class Connection
3
7
  def self.new
@@ -5,7 +9,7 @@ module Cloudflair
5
9
 
6
10
  Faraday.new(url: config.cloudflare.api_base_url, headers: headers) do |faraday|
7
11
  faraday.request :json
8
- faraday.response :logger
12
+ faraday.response config.faraday.adapter if config.faraday.logger
9
13
  faraday.response :json, content_type: /\bjson$/
10
14
 
11
15
  faraday.adapter config.faraday.adapter || Faraday.default_adapter
@@ -26,6 +26,14 @@ module Cloudflair
26
26
 
27
27
  @deletable = deletable
28
28
  end
29
+
30
+ def path(path = nil)
31
+ return @path if @path
32
+
33
+ fail ArgumentError, 'path is not defined' if path.nil?
34
+
35
+ @path = path
36
+ end
29
37
  end
30
38
 
31
39
  def revert
@@ -132,6 +140,17 @@ module Cloudflair
132
140
  self.class.deletable
133
141
  end
134
142
 
143
+ def path
144
+ return @path if @path
145
+
146
+ path = self.class.path
147
+ interpreted_path = path.clone
148
+ path.scan /:([a-zA-Z_][a-zA-Z0-9_]+[!?=]?)/ do |match, *|
149
+ interpreted_path.gsub! ":#{match}", send(match).to_s
150
+ end
151
+ @path = interpreted_path
152
+ end
153
+
135
154
  def dirty
136
155
  @dirty ||= {}
137
156
  end
@@ -1,3 +1,3 @@
1
1
  module Cloudflair
2
- VERSION = '0.0.4'.freeze
2
+ VERSION = '0.0.5'.freeze
3
3
  end
data/lib/cloudflair.rb CHANGED
@@ -1,6 +1,4 @@
1
- require 'cloudflair/api/zone'
2
- require 'cloudflair/communication'
3
- require 'cloudflair/connection'
1
+ require 'cloudflair/api'
4
2
  require 'cloudflair/version'
5
3
  require 'dry-configurable'
6
4
 
@@ -15,8 +13,11 @@ require 'dry-configurable'
15
13
  # config.cloudflare.auth.user_service_key = 'YOUR_USER_SERVICE_KEY'
16
14
  #
17
15
  # # these are optional:
16
+ #
18
17
  # config.cloudflare.api_base_url = 'https://your_cloudflare_mock.local'
19
18
  # config.faraday.adapter = :your_preferred_faraday_adapter
19
+ # # built-in options: :logger, :detailed_logger; default: nil
20
+ # config.faraday.logger = :logger
20
21
  # end
21
22
  # </code>
22
23
  module Cloudflair
@@ -34,5 +35,6 @@ module Cloudflair
34
35
 
35
36
  setting :faraday do
36
37
  setting :adapter, :net_http
38
+ setting :logger
37
39
  end
38
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudflair
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Mäder
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday-detailed_logger
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.1.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -113,6 +127,7 @@ files:
113
127
  - bin/setup
114
128
  - cloudflair.gemspec
115
129
  - lib/cloudflair.rb
130
+ - lib/cloudflair/api.rb
116
131
  - lib/cloudflair/api/zone.rb
117
132
  - lib/cloudflair/api/zone/purge_cache.rb
118
133
  - lib/cloudflair/api/zone/settings.rb