buttercms-ruby 1.5 → 2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 348ca14f5f921c18ed7368f6f49bc97f9649304fa55e64ee1819515060bfbe01
4
- data.tar.gz: 00e814b0baece7fcf1bba2fc29d16ae42c17b47ab32e796b48c249db922357ef
3
+ metadata.gz: 69371855dc4fc9883ba72a8eabd2ede8e8b5118a79b0435457da54609bde25f4
4
+ data.tar.gz: 72c527a3185fc8ed00eb56735bc4e2f3abe343058214cef23ce6debba066c169
5
5
  SHA512:
6
- metadata.gz: 5584f22c5db27a1556bea7370aaf02137b9ffdcf0f613899bad05e85ddfd2795d82ff9f94c0fe1f96b3c2e04dc6659bab08df68e85a80528cfa267307de542b7
7
- data.tar.gz: '0215907376576dc8605406b2983aad57aa3fecdb800e3c291178c94a5cf97f84442236c4138d76c22c0ae088c3ca6e7b2d5a725c73b98f603910a4a7197bc157'
6
+ metadata.gz: 3c7f9c52c35df85a8ed5f38a3531deddee771529bbb2717c5d5b2cca2a788503a0aecea2672be6bb5f22837a1b453ecbcef7d4f4856dea4b7060e7fc98b74ebf
7
+ data.tar.gz: 70f72d7bf27b9532904b37aff8c40d1060d33511f34d4bcc2d6c0c24aa24ab19f3a9d587b1d132390a672ecfa5c99172ea2c1880cff33263666ce5bf3330d42b
data/README.md CHANGED
@@ -26,6 +26,9 @@ To setup your project, follow these steps:
26
26
 
27
27
  # Set read timeout (Default is 5.0)
28
28
  # ButterCMS::read_timeout = 5.0
29
+
30
+ # Set open timeout (Default is 2.0)
31
+ # ButterCMS::open_timeout = 2.0
29
32
  ```
30
33
 
31
34
  ## Pages
@@ -34,7 +37,7 @@ https://buttercms.com/docs/api/?ruby#pages
34
37
 
35
38
 
36
39
  ```ruby
37
- params = {page: 1, page_size: 10, locale: 'en', preview: 1, fields.headline: 'foo bar', levels: 2} # optional
40
+ params = {page: 1, page_size: 10, locale: 'en', preview: 1, 'fields.headline': 'foo bar', levels: 2} # optional
38
41
  pages = ButterCMS::Page.list('news', params)
39
42
  page = ButterCMS::Page.get('news', 'hello-world', params)
40
43
  ```
@@ -45,7 +48,7 @@ https://buttercms.com/docs/api/?ruby#retrieve-a-collection
45
48
 
46
49
  ```ruby
47
50
  # list each instance of a given collection with meta data for fetching the next page.
48
- params = { page: 1, page_size: 10, locale: 'en', preview: 1, fields.headline: 'foo bar', levels: 2 } # optional
51
+ params = { page: 1, page_size: 10, locale: 'en', preview: 1, 'fields.headline': 'foo bar', levels: 2 } # optional
49
52
  ButterCMS::Content.list('collection1', params)
50
53
 
51
54
  # list instances for multiple collections, this will not return meta data for pagination control.
@@ -30,6 +30,7 @@ module ButterCMS
30
30
  attr_accessor :write_api_token
31
31
  attr_accessor :test_mode
32
32
  attr_accessor :read_timeout
33
+ attr_accessor :open_timeout
33
34
  attr_reader :data_store
34
35
  attr_writer :logger
35
36
  end
@@ -70,7 +71,13 @@ module ButterCMS
70
71
  query[:test] = 1
71
72
  end
72
73
 
73
- path = "#{@api_url.path}#{URI.encode(path)}?#{URI.encode_www_form(query)}"
74
+ # If the user has passed in a "/" leading path, don't interpret that
75
+ # as wanting to get rid of the API prefix
76
+ if path.start_with?("/")
77
+ path = path[1..-1]
78
+ end
79
+
80
+ path = Pathname.new(@api_url.path).join(path).to_s + "?#{URI.encode_www_form(query)}"
74
81
 
75
82
  response =
76
83
  Net::HTTP.start(@api_url.host, @api_url.port, http_options) do |http|
@@ -172,7 +179,7 @@ module ButterCMS
172
179
 
173
180
  def self.http_options
174
181
  {
175
- open_timeout: 2.0,
182
+ open_timeout: open_timeout || 2.0,
176
183
  read_timeout: read_timeout || 5.0,
177
184
  ssl_timeout: 2.0,
178
185
  use_ssl: @api_url.scheme == "https",
@@ -2,6 +2,7 @@ module ButterCMS
2
2
  class ButterCollection
3
3
  include Enumerable
4
4
 
5
+ attr_reader :items
5
6
  attr_reader :meta
6
7
 
7
8
  def initialize(klass, json)
@@ -18,4 +19,4 @@ module ButterCMS
18
19
  end
19
20
  end
20
21
  end
21
- end
22
+ end
@@ -7,12 +7,19 @@ module ButterCMS
7
7
  @data = HashToObject.convert(json["data"])
8
8
  @meta = HashToObject.convert(json["meta"]) if json["meta"]
9
9
 
10
- if json["data"].is_a?(Hash)
11
- json["data"].each do |key, value|
12
- instance_variable_set("@#{key}", @data.send(key))
13
- self.class.send(:attr_reader, key)
14
- end
15
- end
10
+ define_attribute_methods(@data)
11
+ end
12
+
13
+ def marshal_dump
14
+ { json: @json, data: @data, meta: @meta }
15
+ end
16
+
17
+ def marshal_load(dump)
18
+ @json = dump[:json]
19
+ @data = dump[:data]
20
+ @meta = dump[:meta]
21
+
22
+ define_attribute_methods(@data)
16
23
  end
17
24
 
18
25
  def inspect
@@ -25,7 +32,7 @@ module ButterCMS
25
32
  # API expects all endpoints to include trailing slashes
26
33
  resource_path + (id ? "#{id}/" : '')
27
34
  end
28
-
35
+
29
36
  def self.patch_endpoint(id)
30
37
  # Append trailing slash when id is added to path because
31
38
  # API expects all endpoints to include trailing slashes
@@ -47,14 +54,14 @@ module ButterCMS
47
54
 
48
55
  self.create_object(response)
49
56
  end
50
-
57
+
51
58
  def self.create(options = {})
52
59
  options[:method] = 'Post'
53
60
  response = ButterCMS.write_request(self.endpoint, options)
54
61
 
55
62
  self.create_object(response)
56
63
  end
57
-
64
+
58
65
  def self.update(id, options = {})
59
66
  options[:method] = 'Patch'
60
67
  _endpoint = if resource_path.include?("/pages/")
@@ -76,5 +83,14 @@ module ButterCMS
76
83
  def self.create_object(response)
77
84
  self.new(response)
78
85
  end
86
+
87
+ def define_attribute_methods(methods)
88
+ return unless methods.respond_to?(:each_pair)
89
+
90
+ methods.each_pair do |key, value|
91
+ instance_variable_set("@#{key}", value)
92
+ self.class.send(:attr_reader, key)
93
+ end
94
+ end
79
95
  end
80
96
  end
@@ -1,3 +1,3 @@
1
1
  module ButterCMS
2
- VERSION = '1.5'
3
- end
2
+ VERSION = '2.0'
3
+ end
@@ -14,6 +14,22 @@ describe ButterCMS do
14
14
  ButterCMS.request('')
15
15
  expect(request).to have_been_made
16
16
  end
17
+
18
+ it "should properly escape paths" do
19
+ request = stub_request(
20
+ :get,
21
+ "https://api.buttercms.com/v2/pages/*/homepage%20en?auth_token=test123"
22
+ ).to_return(body: JSON.generate({data: {test: 'test'}}))
23
+
24
+ # support leading slashes
25
+ ButterCMS.request('/pages/*/homepage en')
26
+
27
+ # and no leading slashes
28
+ ButterCMS.request('pages/*/homepage en')
29
+
30
+
31
+ expect(request).to have_been_made.twice
32
+ end
17
33
  end
18
34
 
19
35
  context 'without an api token' do
@@ -4,6 +4,12 @@ describe ButterCMS::ButterCollection do
4
4
  let(:json) { {"data" => ["foo"], "meta" => {}} }
5
5
  let(:klass) { double('klass', :new => 'bar') }
6
6
 
7
+ it 'implements #items' do
8
+ collection = ButterCMS::ButterCollection.new(klass, json)
9
+
10
+ expect(collection.items).to match_array(["bar"])
11
+ end
12
+
7
13
  it 'implements #meta' do
8
14
  collection = ButterCMS::ButterCollection.new(klass, json)
9
15
 
@@ -15,4 +21,20 @@ describe ButterCMS::ButterCollection do
15
21
 
16
22
  expect(collection.count).to eq 1
17
23
  end
18
- end
24
+
25
+ # Marshal.load (used by Rails for caching) was not restoring the ButterResource's dynamic methods
26
+ # See https://github.com/ButterCMS/buttercms-ruby/issues/13
27
+ describe 'marshal load' do
28
+ subject { described_class.new(ButterCMS::ButterResource, 'data' => [{ 'name' => 'Test Name', 'description' => 'Test Description' }]) }
29
+
30
+ it 'restores the ButterResource dynamic methods' do
31
+ collection = Marshal.load(Marshal.dump(subject))
32
+ resource = collection.first
33
+
34
+ aggregate_failures do
35
+ expect(resource.name).to eq('Test Name')
36
+ expect(resource.description).to eq('Test Description')
37
+ end
38
+ end
39
+ end
40
+ end
@@ -8,6 +8,17 @@ describe ButterCMS::ButterResource do
8
8
  allow(ButterCMS::ButterResource).to receive(:resource_path).and_return('')
9
9
  end
10
10
 
11
+ describe 'auto-generated methods' do
12
+ let(:resource) { described_class.new('data' => { 'name' => 'Test Name', 'description' => 'Test Description' }) }
13
+
14
+ it 'creates attribute reader methods for data pairs' do
15
+ aggregate_failures do
16
+ expect(resource.name).to eq('Test Name')
17
+ expect(resource.description).to eq('Test Description')
18
+ end
19
+ end
20
+ end
21
+
11
22
  describe '.all' do
12
23
 
13
24
  it 'should make a request with the correct endpoint' do
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.5'
4
+ version: '2.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - ButterCMS
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-10 00:00:00.000000000 Z
11
+ date: 2021-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec