buttercms-ruby 1.0.10 → 1.1.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: cf196fb4aad8f73afc7b5553f4db65169c71fc2c
4
- data.tar.gz: ccf34d34ff943a7cd62357463f2c0d6c6c404e0b
3
+ metadata.gz: 8a5b25e75cd089178453db6e0bf8998d234dd5c4
4
+ data.tar.gz: cf916ea47c139bf2fd8ce739fdac79785d59da6d
5
5
  SHA512:
6
- metadata.gz: ca83066a05a7acd2142fc81bd7cecf2b6cb5ecae8b0b84598f7401215bde14a8d9b40000dde1c1e71d2cd2c7aa91cb77a0380781a9e1ab7fd9cdf5aa2e9f9e22
7
- data.tar.gz: d815c60c62f9ca9690d6a64e0393f19d500e00157cf36aadce71d676d69d3c51bc2275bd6f5cd31a313f60b116c17be7a01a071f0b196da43d6eb4dde9cb8a27
6
+ metadata.gz: 24878819c0724a9c61c7cf99cab33904c8ccbb1451dd629f10b24e49214d6bc1927ed3d53768efb7a1d3aa9c97314d44fdb4d24efc07c4616608aa482cdcb22b
7
+ data.tar.gz: ec48e2d3483619a45cdfac7cc5602e9a2984288539d1fb8e59492b9a6e77d5b1e3f463b8dad536dd2c98017acd3cd316ce41511189a8c8a8c9508eb797ce2191
data/.gitignore CHANGED
@@ -17,4 +17,6 @@ test/version_tmp
17
17
  tmp
18
18
  .DS_Store
19
19
  .ruby-version
20
- .rvmrc
20
+ .rvmrc
21
+ *.rdb
22
+ *.store
data/README.md CHANGED
@@ -47,7 +47,6 @@ p tags
47
47
  rss_feed = ButterCMS::Feed.find(:rss)
48
48
  puts rss_feed.data
49
49
 
50
- # Try our new custom content feature
51
50
  content = ButterCMS::Content.fetch([
52
51
  :homepage_html_title,
53
52
  :homepage_meta_description,
@@ -55,3 +54,17 @@ content = ButterCMS::Content.fetch([
55
54
  ])
56
55
  ```
57
56
 
57
+ ## Fallback Data Store
58
+
59
+ This client supports automatic fallback to a data store when API requests fail. When a data store is set, on every successful API request the response is written to the data store. When a subsequent API request fails, the client attempts to fallback to the value in the data store. Currently, Redis and YAML Store are supported.
60
+
61
+ ```ruby
62
+ # Use YAMLstore
63
+ ButterCMS::data_store = :yaml, "/File/Path/For/buttercms.store"
64
+
65
+ # Use Redis
66
+ ButterCMS::data_store = :redis, ENV['REDIS_URL']
67
+
68
+ # Set logger (optional)
69
+ ButterCMS::logger = MyLogger.new
70
+ ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.10
1
+ 1.1.0
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.license = 'MIT'
16
16
 
17
17
  s.add_dependency 'rest-client', '>= 1.7.0', '< 2'
18
+ s.add_dependency 'redis', '>= 3.0.0'
18
19
 
19
20
  s.add_development_dependency 'rspec', '~> 2.7'
20
21
  s.add_development_dependency 'webmock'
@@ -1,6 +1,8 @@
1
1
  require 'json'
2
2
  require 'rest_client'
3
3
  require 'ostruct'
4
+ require "redis"
5
+ require 'yaml/store'
4
6
 
5
7
  require_relative 'buttercms/hash_to_object'
6
8
  require_relative 'buttercms/butter_collection'
@@ -12,6 +14,9 @@ require_relative 'buttercms/post'
12
14
  require_relative 'buttercms/feed'
13
15
  require_relative 'buttercms/content'
14
16
 
17
+ require_relative 'buttercms/data_store_adapters/yaml'
18
+ require_relative 'buttercms/data_store_adapters/redis'
19
+
15
20
  # See https://github.com/jruby/jruby/issues/3113
16
21
  if RUBY_VERSION < '2.0.0'
17
22
  require_relative 'core_ext/ostruct'
@@ -19,33 +24,83 @@ end
19
24
 
20
25
  module ButterCMS
21
26
  @api_url = 'https://api.buttercms.com/v2'
22
- @token = nil
23
27
 
24
- def self.api_token=(token)
25
- @token = token
28
+ class <<self
29
+ attr_accessor :api_token
30
+ attr_reader :data_store
31
+ attr_writer :logger
26
32
  end
27
33
 
28
- def self.token
29
- @token
34
+ def self.logger
35
+ @logger ||= Logger.new($stdout).tap do |log|
36
+ log.progname = "ButterCMS"
37
+ end
30
38
  end
31
39
 
32
- def self.endpoint
33
- @api_url
34
- end
40
+ def self.data_store=(*args)
41
+ args.flatten!
35
42
 
36
- def self.request(path, options = {})
37
- raise ArgumentError.new "Please set your API token" unless token
43
+ if args.count < 2
44
+ raise ArgumentError.new "Wrong number of arguments"
45
+ end
46
+
47
+ strategy = args.first
48
+ options = args.drop(1)
49
+
50
+ case strategy
51
+ when :yaml
52
+ @data_store = ButterCMS::DataStoreAdapters::Yaml.new(options)
53
+ when :redis
54
+ @data_store = ButterCMS::DataStoreAdapters::Redis.new(options)
55
+ else
56
+ raise ArgumentError.new "Invalid ButterCMS data store #{strategy}"
57
+ end
58
+ end
38
59
 
60
+ def self.api_request(path, options = {})
39
61
  response = RestClient::Request.execute(
40
62
  method: :get,
41
- url: endpoint + path,
63
+ url: @api_url + path,
42
64
  headers: {
43
- params: options.merge(auth_token: @token)
65
+ params: options.merge(auth_token: api_token)
44
66
  },
45
67
  verify_ssl: false
46
68
  )
47
69
 
48
- JSON.parse(response.body)
70
+ response.body
49
71
  end
50
- end
51
72
 
73
+ def self.request(path, options = {})
74
+ raise ArgumentError.new "Please set your API token" unless api_token
75
+
76
+ key = "buttercms:#{path}:#{options}"
77
+
78
+ begin
79
+ result = api_request(path, options)
80
+
81
+ if data_store
82
+ data_store.set(key, result)
83
+ logger.info "Set key #{key}"
84
+ end
85
+
86
+ # TODO - more selective exception handling (RestClient::Exception, SocketError)
87
+ rescue Exception => e
88
+
89
+ if data_store
90
+ if result = data_store.get(key)
91
+ logger.info "Fetched key #{key}"
92
+
93
+ # Log request error
94
+ logger.error e
95
+ else
96
+ logger.info "No data for key #{key}"
97
+ end
98
+ end
99
+
100
+ # Raise request exception if there's no data store or value returned
101
+ raise e unless data_store && result
102
+ end
103
+
104
+ return JSON.parse(result)
105
+ end
106
+ end
@@ -0,0 +1,19 @@
1
+ module ButterCMS
2
+ module DataStoreAdapters
3
+ class Redis
4
+ def initialize(options)
5
+ redis_url = options.first
6
+
7
+ @redis = ::Redis.new(url: redis_url)
8
+ end
9
+
10
+ def set(key, value)
11
+ @redis.set(key, value)
12
+ end
13
+
14
+ def get(key)
15
+ @redis.get(key)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module ButterCMS
2
+ module DataStoreAdapters
3
+ class Yaml
4
+ def initialize(options)
5
+ file_path = options.first
6
+
7
+ @store = YAML::Store.new file_path
8
+ end
9
+
10
+ def set(key, value)
11
+ @store.transaction do
12
+ @store[key] = value
13
+ end
14
+ end
15
+
16
+ def get(key)
17
+ @store.transaction do
18
+ @store[key]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module ButterCMS
2
- VERSION = '1.0.10'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -4,11 +4,11 @@ describe ButterCMS do
4
4
  describe '.request' do
5
5
  context 'with an api token' do
6
6
  before do
7
- ButterCMS.stub(:token).and_return('test123')
7
+ ButterCMS.stub(:api_token).and_return('test123')
8
8
  end
9
9
 
10
10
  it 'should make an api request' do
11
- stub_request(:get, ButterCMS.endpoint).to_return(body: JSON.generate({data: {test: 'test'}}))
11
+ stub_request(:get, 'https://api.buttercms.com/v2?auth_token=test123').to_return(body: JSON.generate({data: {test: 'test'}}))
12
12
  expect{ ButterCMS.request('') }.to_not raise_error
13
13
  end
14
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buttercms-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.10
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ButterCMS
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-08 00:00:00.000000000 Z
11
+ date: 2016-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: redis
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 3.0.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.0.0
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: rspec
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,8 @@ files:
80
94
  - lib/buttercms/butter_resource.rb
81
95
  - lib/buttercms/category.rb
82
96
  - lib/buttercms/content.rb
97
+ - lib/buttercms/data_store_adapters/redis.rb
98
+ - lib/buttercms/data_store_adapters/yaml.rb
83
99
  - lib/buttercms/errors/buttercms_error.rb
84
100
  - lib/buttercms/feed.rb
85
101
  - lib/buttercms/hash_to_object.rb