buttercms-ruby 1.4 → 1.9

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
- SHA1:
3
- metadata.gz: 6892f2c65b2714c507d356db4c7a0fa1ca0efe53
4
- data.tar.gz: aeee01cf3ad4a68c8fe365124db34e494dbf9ca1
2
+ SHA256:
3
+ metadata.gz: a0f437a3187e885b9c7250f9afbe5ff41cebb2dbc84127dd36ae0963b34fcba1
4
+ data.tar.gz: 2a9673e85ef2155f5b55f2febe48932dacf28fc4d7e7fdf4ffa050cb18d69c49
5
5
  SHA512:
6
- metadata.gz: 7f71a3ae6a84b213bc03c42c96b0217d1241a8f812c4f1b9acb8ef968293a4d5a5db6e727316e5f5b290dabd3c5219df02ef58a03360cf4bd845bf199e9a4d12
7
- data.tar.gz: 7a3afc5ae7c6a3c7f0d3c41b48a63bbdd43bd4107cfbffb7cad501ba40d8461168f2d95726bb45d40509938fb7495a741d4f6994c410f9abc96877ee5fd200d1
6
+ metadata.gz: 46738145c68da934fc76e9a8e3c8fdaa2ee8e9462e8c07ed69af2ad557cb1e659a657fc164f1573d9d9a5f0fd35b8eceec046f2fae2c40dbe5a518c374cd970a
7
+ data.tar.gz: f47192c16cf33c9557aa151e90db0538bf3e3a31dd03e68fe4666cd72093b1c6f95414d640d1121a19eabc491dcbdb10939a8940175de409d7ebcabef4268a65
data/.gitignore CHANGED
@@ -19,4 +19,5 @@ tmp
19
19
  .ruby-version
20
20
  .rvmrc
21
21
  *.rdb
22
- *.store
22
+ *.store
23
+ test.rb
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
  ```
@@ -44,8 +47,12 @@ page = ButterCMS::Page.get('news', 'hello-world', params)
44
47
  https://buttercms.com/docs/api/?ruby#retrieve-a-collection
45
48
 
46
49
  ```ruby
47
- params = {page: 1, page_size: 10, locale: 'en', preview: 1, fields.headline: 'foo bar', levels: 2} # optional
48
- ButterCMS::Content.fetch(['testimonials'], params)
50
+ # list each instance of a given collection with meta data for fetching the next page.
51
+ params = { page: 1, page_size: 10, locale: 'en', preview: 1, 'fields.headline': 'foo bar', levels: 2 } # optional
52
+ ButterCMS::Content.list('collection1', params)
53
+
54
+ # list instances for multiple collections, this will not return meta data for pagination control.
55
+ ButterCMS::Content.fetch(['collection1', 'collection2'], params)
49
56
 
50
57
  # Test mode can be used to setup a staging website for previewing Collections or for testing content during local development. To fetch content from test mode add the following configuration:
51
58
  ButterCMS::test_mode = true
@@ -131,3 +138,5 @@ ButterCMS::logger = MyLogger.new
131
138
  ### Other
132
139
 
133
140
  View Ruby [Blog engine](https://buttercms.com/ruby-blog-engine/) and [Full CMS](https://buttercms.com/ruby-cms/) for other examples of using ButterCMS with Ruby.
141
+
142
+ ### Development
@@ -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,7 @@ 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
+ path = "#{@api_url.path}#{URI.encode_www_form(path)}?#{URI.encode_www_form(query)}"
74
75
 
75
76
  response =
76
77
  Net::HTTP.start(@api_url.host, @api_url.port, http_options) do |http|
@@ -172,7 +173,7 @@ module ButterCMS
172
173
 
173
174
  def self.http_options
174
175
  {
175
- open_timeout: 2.0,
176
+ open_timeout: open_timeout || 2.0,
176
177
  read_timeout: read_timeout || 5.0,
177
178
  ssl_timeout: 2.0,
178
179
  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,22 +1,19 @@
1
1
  module ButterCMS
2
- class Content
3
- attr_reader :data
4
-
5
- def initialize(json)
6
- @json = json
7
- @data = HashToObject.convert(json["data"])
8
- end
9
-
10
- def inspect
11
- id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
12
- "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@json)
2
+ class Content < ButterResource
3
+ def self.resource_path
4
+ "/content/"
13
5
  end
14
6
 
15
- def self.fetch(keys = [], options = {})
16
- params = {keys: keys.join(',')}.merge(options)
17
-
18
- response = ButterCMS.request("/content/", params)
7
+ def self.list(collection_slug, options = {})
8
+ response = ButterCMS.request(self.endpoint(collection_slug), options)
19
9
 
10
+ self.create_collection(response)
11
+ end
12
+
13
+ def self.fetch(collection_slugs, options = {})
14
+ params = { keys: collection_slugs.join(',') }.merge(options)
15
+ response = ButterCMS.request(self.resource_path, params)
16
+
20
17
  self.new(response)
21
18
  end
22
19
  end
@@ -1,3 +1,3 @@
1
1
  module ButterCMS
2
- VERSION = '1.4'
3
- end
2
+ VERSION = '1.9'
3
+ end
data/lib/console.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/inline'
2
+
3
+ gemfile do
4
+ source 'https://rubygems.org'
5
+ gem 'buttercms-ruby', :path => '../buttercms-ruby'
6
+ end
7
+
8
+ # start a REPL session
9
+ ButterCMS::api_token = "" # add your dev token here
10
+ binding.irb
@@ -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,10 +8,21 @@ 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
14
- expect(ButterCMS).to receive(:request).with('/', {})
25
+ expect(ButterCMS).to receive(:request).with('', {})
15
26
  ButterCMS::ButterResource.all()
16
27
  end
17
28
 
@@ -24,7 +35,7 @@ describe ButterCMS::ButterResource do
24
35
 
25
36
  describe '.find' do
26
37
  it 'should make a request with the correct endpoint' do
27
- expect(ButterCMS).to receive(:request).with('/1', {})
38
+ expect(ButterCMS).to receive(:request).with('1/', {})
28
39
  ButterCMS::ButterResource.find(1)
29
40
  end
30
41
 
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe ButterCMS::Content do
4
+ before do
5
+ allow(ButterCMS).to receive(:token).and_return('test123')
6
+ allow(ButterCMS).to receive(:request).and_return({
7
+ "meta"=>{
8
+ "next_page"=>2,
9
+ "previous_page"=>nil,
10
+ "count"=>2
11
+ },
12
+ "data"=>{
13
+ "author"=>[
14
+ { "name"=>"Charles Dickens"},
15
+ { "name"=>"J.K. Rowling"}
16
+ ]
17
+ }
18
+ })
19
+
20
+ @response = ButterCMS::Content.list('slug', {
21
+ page: 1,
22
+ page_size: 2
23
+ })
24
+ end
25
+
26
+ it "has meta and collection info" do
27
+ expect(@response.meta.next_page).to eq(2)
28
+ expect(@response.to_a.first.data.first).to eq('author')
29
+ expect(@response.to_a.first.data.last.first).to have_attributes(
30
+ name: "Charles Dickens"
31
+ )
32
+ end
33
+ 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.4'
4
+ version: '1.9'
5
5
  platform: ruby
6
6
  authors:
7
7
  - ButterCMS
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-18 00:00:00.000000000 Z
11
+ date: 2021-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -52,7 +52,6 @@ files:
52
52
  - LICENSE
53
53
  - README.md
54
54
  - Rakefile
55
- - VERSION
56
55
  - buttercms-ruby.gemspec
57
56
  - lib/buttercms-ruby.rb
58
57
  - lib/buttercms/author.rb
@@ -69,10 +68,12 @@ files:
69
68
  - lib/buttercms/post.rb
70
69
  - lib/buttercms/tag.rb
71
70
  - lib/buttercms/version.rb
71
+ - lib/console.rb
72
72
  - lib/core_ext/ostruct.rb
73
73
  - spec/lib/butter-ruby_spec.rb
74
74
  - spec/lib/buttercms/butter_collection_spec.rb
75
75
  - spec/lib/buttercms/butter_resource_spec.rb
76
+ - spec/lib/buttercms/content_spec.rb
76
77
  - spec/lib/buttercms/hash_to_object_spec.rb
77
78
  - spec/spec_helper.rb
78
79
  homepage: https://buttercms.com/docs
@@ -94,8 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  - !ruby/object:Gem::Version
95
96
  version: '0'
96
97
  requirements: []
97
- rubyforge_project:
98
- rubygems_version: 2.6.12
98
+ rubygems_version: 3.0.3
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: A simple Ruby client for the buttercms.com REST API
@@ -103,5 +103,6 @@ test_files:
103
103
  - spec/lib/butter-ruby_spec.rb
104
104
  - spec/lib/buttercms/butter_collection_spec.rb
105
105
  - spec/lib/buttercms/butter_resource_spec.rb
106
+ - spec/lib/buttercms/content_spec.rb
106
107
  - spec/lib/buttercms/hash_to_object_spec.rb
107
108
  - spec/spec_helper.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.4