webflow-ruby 0.6.0 → 1.2.1

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
  SHA256:
3
- metadata.gz: 0f0ae105e0b3751752fb45b19468c361c969c283565bc604ee99a534f3bc93a3
4
- data.tar.gz: d81ca44d2625e44a07b67fe7e71f8b7cb7133a89e861dc62abaa0fb1da22e35f
3
+ metadata.gz: 21d5899eec3b30b220cc9504ac6bfae90a5ca8513527a7549ae92e0f3506f9a8
4
+ data.tar.gz: be3d265daec2d442c26ceb5711991d665223ed919434066b13ede91f6f1ac6b9
5
5
  SHA512:
6
- metadata.gz: 1c458dee0176fc2a97b6df6d67415a6c3ed88d83afb6e7991857db100bbd193a0208501e8c11b0d0628260af641630fd943536d885d9716277a54c11d76dff0f
7
- data.tar.gz: de5e2448a9121fd9320f38ef74996a9201fd0d802079332e0666d0744bb8643019c1e007d4e59ef20e095e4dc7decc7b542c92edcdce24dd7682c8a49dfa95d8
6
+ metadata.gz: fc7d8c4dbd5b1e2305652a6909c7eb5c5d441338f7b0778644cce633640eb159d8b5893dd4ef93242712ae57f56b775443f64ca377101202b4ac79a355e95dee
7
+ data.tar.gz: dd9a55df590331f6dedc470055eff82d50511c19ca8d8d7eb80cd79ca4d8d0b66576373eac3562a0e93e9910a3f626038623d29cfbaaf2c65d78c69de7489ea2
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  *.gem
10
+ *.swo
11
+ *.swp
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.5.3
1
+ 2.7.1
data/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
1
+ ## 1.2.1
2
+ * [FEATURE] Handle Problems in Validation errors https://github.com/penseo/webflow-ruby/pull/14 https://github.com/penseo/webflow-ruby/pull/16 😍 @vfonic @sega
3
+
4
+ ## 1.2.0
5
+ * [FEATURE] Add patch support for updating items https://github.com/penseo/webflow-ruby/issues/10 😍 @ukd1
6
+
7
+ ## 1.1.0
8
+
9
+ * [FEATURE] Allow passing a block to `items` that yields the results by page https://github.com/penseo/webflow-ruby/issues/9 😍 @emilesilvis
10
+ * [FEATURE] Handle Webflow live feature https://github.com/penseo/webflow-ruby/pull/8 😍 @emilesilvis
11
+
12
+ ## 1.0.0
13
+
14
+ * [BREAKING] Raise errors when status > 200, see also https://github.com/penseo/webflow-ruby/pull/7 😍 @sega
15
+
16
+ ## 0.7.0
17
+
18
+ * [FEATURE] Configuration class to store api token https://github.com/penseo/webflow-ruby/pull/6 😍 @mateuscruz
19
+
1
20
  ## 0.6.0
2
21
 
3
22
  * [CHANGE] Use pure ruby dependencies
data/Gemfile.lock CHANGED
@@ -1,38 +1,43 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- webflow-ruby (0.6.0)
4
+ webflow-ruby (1.2.0)
5
5
  http
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- addressable (2.6.0)
11
- public_suffix (>= 2.0.2, < 4.0)
12
- byebug (11.0.1)
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ byebug (11.1.1)
13
13
  crack (0.4.3)
14
14
  safe_yaml (~> 1.0.0)
15
15
  domain_name (0.5.20190701)
16
16
  unf (>= 0.0.5, < 1.0.0)
17
- hashdiff (1.0.0)
18
- http (4.1.1)
17
+ ffi (1.12.2)
18
+ ffi-compiler (1.0.1)
19
+ ffi (>= 1.0.0)
20
+ rake
21
+ hashdiff (1.0.1)
22
+ http (4.4.1)
19
23
  addressable (~> 2.3)
20
24
  http-cookie (~> 1.0)
21
- http-form_data (~> 2.0)
22
- http_parser.rb (~> 0.6.0)
25
+ http-form_data (~> 2.2)
26
+ http-parser (~> 1.2.0)
23
27
  http-cookie (1.0.3)
24
28
  domain_name (~> 0.5)
25
- http-form_data (2.1.1)
26
- http_parser.rb (0.6.0)
27
- minitest (5.11.3)
28
- public_suffix (3.1.1)
29
- rake (12.3.3)
29
+ http-form_data (2.3.0)
30
+ http-parser (1.2.1)
31
+ ffi-compiler (>= 1.0, < 2.0)
32
+ minitest (5.14.0)
33
+ public_suffix (4.0.4)
34
+ rake (13.0.1)
30
35
  safe_yaml (1.0.5)
31
36
  unf (0.1.4)
32
37
  unf_ext
33
- unf_ext (0.0.7.6)
34
- vcr (5.0.0)
35
- webmock (3.6.0)
38
+ unf_ext (0.0.7.7)
39
+ vcr (5.1.0)
40
+ webmock (3.8.3)
36
41
  addressable (>= 2.3.6)
37
42
  crack (>= 0.3.2)
38
43
  hashdiff (>= 0.4.0, < 2.0.0)
@@ -48,6 +53,3 @@ DEPENDENCIES
48
53
  vcr
49
54
  webflow-ruby!
50
55
  webmock
51
-
52
- BUNDLED WITH
53
- 2.0.2
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2017 phoet
3
+ Copyright (c) 2019 phoet
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # Webflow [![Build Status](https://travis-ci.org/penseo/webflow-ruby.svg?branch=master)](https://travis-ci.org/penseo/webflow-ruby)
2
2
 
3
3
  ## Installation
4
-
5
4
  Add this line to your application's Gemfile:
6
5
 
7
6
  ```ruby
@@ -10,33 +9,35 @@ gem 'webflow-ruby'
10
9
 
11
10
  And then execute:
12
11
 
13
- $ bundle
12
+ ```bash
13
+ $ bundle
14
+ ```
14
15
 
15
16
  Or install it yourself as:
16
17
 
17
- $ gem install webflow-ruby
18
+ ```bash
19
+ $ gem install webflow-ruby
20
+ ```
18
21
 
19
22
  ## Usage
20
-
21
23
  Have a look at the tests, seriously!
22
24
 
23
25
  ### Quick Start
24
26
  ```ruby
25
- client = Webflow::Client.new(WEBFLOW_API_TOKEN)
26
- sites = client.sites
27
+ client = Webflow::Client.new(WEBFLOW_API_TOKEN)
28
+ sites = client.sites
27
29
  ```
28
30
 
29
31
  ## Todo
30
-
31
- * Error handling, it's all JSON for now
32
32
  * Resource mapping, it's plain hashes for now
33
- * Proper docs
33
+ * Proper docs (please look at the tests for now)
34
34
 
35
35
  ## Contributing
36
-
37
36
  Bug reports and pull requests are welcome on GitHub at https://github.com/penseo/webflow-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
38
37
 
38
+ ## Plugins
39
+ * [webflow_sync](https://github.com/vfonic/webflow_sync) - Keep Rails models in sync with WebFlow collections.
39
40
 
40
- ## License
41
41
 
42
+ ## License
42
43
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -4,13 +4,21 @@ module Webflow
4
4
  class Client
5
5
  HOST = 'https://api.webflow.com'
6
6
 
7
- def initialize(token)
8
- @token = token
7
+ def initialize(token = nil)
8
+ @token = token || Webflow.config.api_token
9
9
  @rate_limit = {}
10
10
  end
11
11
 
12
12
  def rate_limit
13
- @rate_limit
13
+ @rate_limit || {}
14
+ end
15
+
16
+ def limit
17
+ rate_limit['X-Ratelimit-Limit'].to_i
18
+ end
19
+
20
+ def remaining
21
+ rate_limit['X-Ratelimit-Remaining'].to_i
14
22
  end
15
23
 
16
24
  def info
@@ -31,7 +39,7 @@ module Webflow
31
39
 
32
40
  def publish(site_id, domain_names: nil)
33
41
  domain_names ||= domains(site_id).map { |domain| domain['name'] }
34
- post("/sites/#{site_id}/publish", domains: domain_names)
42
+ post("/sites/#{site_id}/publish", {domains: domain_names})
35
43
  end
36
44
 
37
45
  def collections(site_id)
@@ -67,7 +75,9 @@ module Webflow
67
75
 
68
76
  num_pages.times do |i|
69
77
  resp = paginate_items(collection_id, per_page: per_page, page: i+1)
70
- fetched_items += resp['items']
78
+ items = resp['items']
79
+ yield(items) if block_given?
80
+ fetched_items += items
71
81
  limit -= resp['count']
72
82
  break if limit <= 0 || resp['total'] <= fetched_items.length
73
83
  end
@@ -80,14 +90,12 @@ module Webflow
80
90
  json['items'].first
81
91
  end
82
92
 
83
- def create_item(collection_id, data)
84
- post("/collections/#{collection_id}/items", fields: data)
93
+ def create_item(collection_id, data, live: false)
94
+ post("/collections/#{collection_id}/items", {fields: data}, live: live)
85
95
  end
86
96
 
87
- def update_item(item, data)
88
- # FIXME: (PS) looks like the API does not have partial updates...
89
- base = item.reject {|key, _| ['_id', 'published-by', 'published-on', 'created-on', 'created-by', 'updated-by', 'updated-on', '_cid'].include?(key) }
90
- put("/collections/#{item['_cid']}/items/#{item['_id']}", fields: base.merge(data))
97
+ def update_item(item, data, live: false)
98
+ patch("/collections/#{item['_cid']}/items/#{item['_id']}", {fields: data}, live: live)
91
99
  end
92
100
 
93
101
  def delete_item(item)
@@ -100,12 +108,14 @@ module Webflow
100
108
  request(path, method: :get, params: params)
101
109
  end
102
110
 
103
- def post(path, data)
104
- request(path, method: :post, data: data)
111
+ def post(path, data, live: nil)
112
+ params = { live: 'true' } if live
113
+ request(path, method: :post, params: params, data: data)
105
114
  end
106
115
 
107
- def put(path, data)
108
- request(path, method: :put, data: data)
116
+ def patch(path, data, live: nil)
117
+ params = { live: 'true' } if live
118
+ request(path, method: :patch, params: params, data: data)
109
119
  end
110
120
 
111
121
  def delete(path)
@@ -119,30 +129,17 @@ module Webflow
119
129
 
120
130
  response = HTTP.auth(bearer).headers(headers).request(method, url, params: params, json: data)
121
131
 
122
- rate_limit = response.headers.select { |key, value| key =~ /X-Ratelimit/ }.to_h
123
- @rate_limit = rate_limit unless rate_limit.empty?
132
+ track_rate_limit(response.headers)
124
133
 
125
- JSON.parse(response.body)
126
- end
134
+ result = JSON.parse(response.body)
135
+ raise Webflow::Error.new(result) if response.code >= 400
127
136
 
128
- def track_rate_limit(headers)
137
+ result
129
138
  end
130
139
 
131
- # https://developers.webflow.com/#errors
132
- def self.error_codes
133
- {
134
- [400, SyntaxError] => 'Request body was incorrectly formatted. Likely invalid JSON being sent up.',
135
- [400, InvalidAPIVersion] => 'Requested an invalid API version',
136
- [400, UnsupportedVersion] => 'Requested an API version that in unsupported by the requested route',
137
- [400, NotImplemented] => 'This feature is not currently implemented',
138
- [400, ValidationError] => 'Validation failure (see err field in the response)',
139
- [400, Conflict] => 'Request has a conflict with existing data.',
140
- [401, Unauthorized] => 'Provided access token is invalid or does not have access to requested resource',
141
- [404, NotFound] => 'Requested resource not found',
142
- [429, RateLimit] => 'The rate limit of the provided access_token has been reached. Please have your application respect the X-RateLimit-Remaining header we include on API responses.',
143
- [500, ServerError] => 'We had a problem with our server. Try again later.',
144
- [400, UnknownError] => 'An error occurred which is not enumerated here, but is not a server error.',
145
- }
140
+ def track_rate_limit(headers)
141
+ rate_limit = headers.select { |key, value| key =~ /X-Ratelimit/ }.to_h
142
+ @rate_limit = rate_limit unless rate_limit.empty?
146
143
  end
147
144
  end
148
145
  end
@@ -0,0 +1,15 @@
1
+ module Webflow
2
+ class << self
3
+ def config
4
+ @config ||= Config.new
5
+ end
6
+
7
+ def configure
8
+ yield config
9
+ end
10
+ end
11
+
12
+ class Config
13
+ attr_accessor :api_token
14
+ end
15
+ end
@@ -0,0 +1,43 @@
1
+ module Webflow
2
+ class Error < StandardError
3
+ # https://developers.webflow.com/#errors
4
+ # 400 SyntaxError Request body was incorrectly formatted. Likely invalid JSON being sent up.
5
+ # 400 InvalidAPIVersion Requested an invalid API version
6
+ # 400 UnsupportedVersion Requested an API version that in unsupported by the requested route
7
+ # 400 NotImplemented This feature is not currently implemented
8
+ # 400 ValidationError Validation failure (see problems field in the response)
9
+ # 400 Conflict Request has a conflict with existing data.
10
+ # 401 Unauthorized Provided access token is invalid or does not have access to requested resource
11
+ # 404 NotFound Requested resource not found
12
+ # 429 RateLimit The rate limit of the provided access_token has been reached. Please have your application respect the X-RateLimit-Remaining header we include on API responses.
13
+ # 500 ServerError We had a problem with our server. Try again later.
14
+ # 400 UnknownError An error occurred which is not enumerated here, but is not a server error.
15
+ #
16
+ # Sample error response
17
+ #
18
+ # {
19
+ # "msg": "Cannot access resource",
20
+ # "code": 401,
21
+ # "name": "Unauthorized",
22
+ # "path": "/sites/invalid_site",
23
+ # "err": "Unauthorized: Cannot access resource"
24
+ # }
25
+
26
+ attr_reader :data
27
+
28
+ def initialize(data)
29
+ @data = data
30
+
31
+ message = "#{data['msg']}#{': ' + Array(problems).join(', ') if problems?}"
32
+ super(message)
33
+ end
34
+
35
+ def problems?
36
+ !!data['problems']
37
+ end
38
+
39
+ def problems
40
+ data['problems']
41
+ end
42
+ end
43
+ end
data/lib/webflow/ruby.rb CHANGED
@@ -1,2 +1,4 @@
1
1
  require 'webflow/version'
2
+ require 'webflow/config'
2
3
  require 'webflow/client'
4
+ require 'webflow/error'
@@ -1,3 +1,3 @@
1
1
  module Webflow
2
- VERSION = "0.6.0"
2
+ VERSION = "1.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webflow-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - phoet
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-29 00:00:00.000000000 Z
11
+ date: 2021-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -130,6 +130,8 @@ files:
130
130
  - bin/setup
131
131
  - lib/webflow.rb
132
132
  - lib/webflow/client.rb
133
+ - lib/webflow/config.rb
134
+ - lib/webflow/error.rb
133
135
  - lib/webflow/ruby.rb
134
136
  - lib/webflow/version.rb
135
137
  - webflow.gemspec
@@ -152,8 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
154
  - !ruby/object:Gem::Version
153
155
  version: '0'
154
156
  requirements: []
155
- rubyforge_project:
156
- rubygems_version: 2.7.6
157
+ rubygems_version: 3.1.2
157
158
  signing_key:
158
159
  specification_version: 4
159
160
  summary: Webflow API bindings for Ruby