buttercms-ruby 1.3.2 → 1.4

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: c7b4c68f74294cbca04ea1d4c68fa40f241a5f92
4
- data.tar.gz: 124e11b182d91dc136042d7a2ae35a09e4e19d2c
3
+ metadata.gz: 6892f2c65b2714c507d356db4c7a0fa1ca0efe53
4
+ data.tar.gz: aeee01cf3ad4a68c8fe365124db34e494dbf9ca1
5
5
  SHA512:
6
- metadata.gz: e4eb9a5fab1a68b855786b401a5870deb6d962d1b00bca58b2cfa1e44d975b5d5721361944c8192597c5b5866964f166411d46c38e4be88e4c460a3682c619fe
7
- data.tar.gz: 8560d77aee2ad9615c0f6ec985a9badb83c6cf95e00ff35fac70289bf6421e89919670558f8e31351c4fc4561c56249755e49753cc6259e724f98857aa1c9395
6
+ metadata.gz: 7f71a3ae6a84b213bc03c42c96b0217d1241a8f812c4f1b9acb8ef968293a4d5a5db6e727316e5f5b290dabd3c5219df02ef58a03360cf4bd845bf199e9a4d12
7
+ data.tar.gz: 7a3afc5ae7c6a3c7f0d3c41b48a63bbdd43bd4107cfbffb7cad501ba40d8461168f2d95726bb45d40509938fb7495a741d4f6994c410f9abc96877ee5fd200d1
data/README.md CHANGED
@@ -30,26 +30,31 @@ To setup your project, follow these steps:
30
30
 
31
31
  ## Pages
32
32
 
33
+ https://buttercms.com/docs/api/?ruby#pages
34
+
35
+
33
36
  ```ruby
34
- params = {foo: 'bar'} # optional
37
+ params = {page: 1, page_size: 10, locale: 'en', preview: 1, fields.headline: 'foo bar', levels: 2} # optional
35
38
  pages = ButterCMS::Page.list('news', params)
36
39
  page = ButterCMS::Page.get('news', 'hello-world', params)
37
40
  ```
38
41
 
39
- ## Content Fields
42
+ ## Collections
40
43
 
41
- ```ruby
42
- ButterCMS::Content.fetch(['homepage_headline'])
44
+ https://buttercms.com/docs/api/?ruby#retrieve-a-collection
43
45
 
44
- # Localization
45
- ButterCMS::Content.fetch(['homepage_headline'], locale: 'es')
46
+ ```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)
46
49
 
47
- # Test mode can be used to setup a staging website for previewing Content Fields or for testing content during local development. To fetch content from test mode add the following configuration:
50
+ # 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:
48
51
  ButterCMS::test_mode = true
49
52
  ```
50
53
 
51
54
  ## Blog Engine
52
55
 
56
+ https://buttercms.com/docs/api/?ruby#blog-engine
57
+
53
58
  ```ruby
54
59
  posts = ButterCMS::Post.all({:page => 1, :page_size => 10})
55
60
  puts posts.first.title
@@ -61,6 +66,39 @@ puts posts.first.title
61
66
  post = ButterCMS::Post.find("post-slug")
62
67
  puts post.title
63
68
 
69
+ # Create a Post.
70
+ ButterCMS::write_api_token = "YourWriteToken"
71
+ ButterCMS::Post.create({
72
+ slug: 'blog-slug',
73
+ title: 'blog-title'
74
+ })
75
+
76
+ # Update a Post
77
+ ButterCMS::Post.update('blog-slug', {
78
+ title: 'blog-title-v2'
79
+ })
80
+
81
+ # Create a page
82
+ ButterCMS::Page.create({
83
+ slug: 'page-slug',
84
+ title: 'page-title',
85
+ status: 'published',
86
+ "page-type": 'page_type',
87
+ fields: {
88
+ meta_title: 'test meta title'
89
+ }
90
+ })
91
+
92
+ # update a Page
93
+ ButterCMS::Page.update('page-slug-2', {
94
+ status: 'published',
95
+ fields: {
96
+ meta_title: 'test meta title'
97
+ }
98
+ })
99
+
100
+
101
+
64
102
  author = ButterCMS::Author.find("author-slug")
65
103
  puts author.first_name
66
104
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.2
1
+ 1.4
@@ -27,6 +27,7 @@ module ButterCMS
27
27
 
28
28
  class << self
29
29
  attr_accessor :api_token
30
+ attr_accessor :write_api_token
30
31
  attr_accessor :test_mode
31
32
  attr_accessor :read_timeout
32
33
  attr_reader :data_store
@@ -71,13 +72,6 @@ module ButterCMS
71
72
 
72
73
  path = "#{@api_url.path}#{URI.encode(path)}?#{URI.encode_www_form(query)}"
73
74
 
74
- http_options = {
75
- open_timeout: 2.0,
76
- read_timeout: read_timeout || 5.0,
77
- ssl_timeout: 2.0,
78
- use_ssl: @api_url.scheme == "https",
79
- }
80
-
81
75
  response =
82
76
  Net::HTTP.start(@api_url.host, @api_url.port, http_options) do |http|
83
77
  request = Net::HTTP::Get.new(path)
@@ -128,4 +122,60 @@ module ButterCMS
128
122
 
129
123
  return JSON.parse(result)
130
124
  end
125
+
126
+ def self.write_request(path, options = {})
127
+ raise ArgumentError.new "Please set your write API token" unless write_api_token
128
+ result = write_api_request(path, options)
129
+
130
+ return JSON.parse(result)
131
+ end
132
+
133
+ def self.write_api_request(path, options = {})
134
+ query = options.dup
135
+ token_for_request = query.delete(:auth_token) || write_api_token
136
+
137
+ path = "#{@api_url.path}#{URI.encode(path)}"
138
+
139
+ response =
140
+ Net::HTTP.start(@api_url.host, @api_url.port, http_options) do |http|
141
+ write_type = query.delete(:method) || "Post"
142
+ request_type = "Net::HTTP::#{write_type}".constantize
143
+ request = request_type.new(path)
144
+ request["User-Agent"] = "ButterCMS/Ruby #{ButterCMS::VERSION}"
145
+ request["Accept"] = "application/json"
146
+ request["Content-Type"] = "application/json"
147
+ request["Authorization"] = "Token #{token_for_request}"
148
+ request.body = query.except(:auth_token).to_json
149
+
150
+ http.request(request)
151
+ end
152
+
153
+ case response
154
+ when Net::HTTPNotFound
155
+ raise ::ButterCMS::NotFound, JSON.parse(response.body)["detail"]
156
+ when Net::HTTPBadRequest
157
+ parsed_body = JSON.parse(response.body)
158
+ errors = if parsed_body.is_a?(Array)
159
+ parsed_body.join(' ')
160
+ else
161
+ parsed_body.map do |k, v|
162
+ "#{k}: #{v}"
163
+ end.join(" ")
164
+ end
165
+ raise ::ButterCMS::BadRequest, errors
166
+ end
167
+
168
+ response.body
169
+ end
170
+
171
+ private
172
+
173
+ def self.http_options
174
+ {
175
+ open_timeout: 2.0,
176
+ read_timeout: read_timeout || 5.0,
177
+ ssl_timeout: 2.0,
178
+ use_ssl: @api_url.scheme == "https",
179
+ }
180
+ end
131
181
  end
@@ -25,6 +25,12 @@ module ButterCMS
25
25
  # API expects all endpoints to include trailing slashes
26
26
  resource_path + (id ? "#{id}/" : '')
27
27
  end
28
+
29
+ def self.patch_endpoint(id)
30
+ # Append trailing slash when id is added to path because
31
+ # API expects all endpoints to include trailing slashes
32
+ resource_path + "*/#{id}/"
33
+ end
28
34
 
29
35
  def self.resource_path
30
36
  raise "resource_path must be set"
@@ -41,6 +47,25 @@ module ButterCMS
41
47
 
42
48
  self.create_object(response)
43
49
  end
50
+
51
+ def self.create(options = {})
52
+ options[:method] = 'Post'
53
+ response = ButterCMS.write_request(self.endpoint, options)
54
+
55
+ self.create_object(response)
56
+ end
57
+
58
+ def self.update(id, options = {})
59
+ options[:method] = 'Patch'
60
+ _endpoint = if resource_path.include?("/pages/")
61
+ self.patch_endpoint(id)
62
+ else
63
+ self.endpoint(id)
64
+ end
65
+ response = ButterCMS.write_request(_endpoint, options)
66
+
67
+ self.create_object(response)
68
+ end
44
69
 
45
70
  private
46
71
 
@@ -6,4 +6,7 @@ module ButterCMS
6
6
  # NotFound is raised when a resource cannot be found
7
7
  class NotFound < Error
8
8
  end
9
+
10
+ class BadRequest < Error
11
+ end
9
12
  end
@@ -1,3 +1,3 @@
1
1
  module ButterCMS
2
- VERSION = '1.3.2'
2
+ VERSION = '1.4'
3
3
  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.3.2
4
+ version: '1.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - ButterCMS
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-20 00:00:00.000000000 Z
11
+ date: 2019-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec