strapi_ruby 0.1.1 → 0.1.3
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 +4 -4
- data/.DS_Store +0 -0
- data/CHANGELOG.md +10 -1
- data/README.md +17 -11
- data/lib/strapi_ruby/client.rb +7 -9
- data/lib/strapi_ruby/endpoint/builder.rb +5 -5
- data/lib/strapi_ruby/endpoint/strapi_parameters.rb +20 -26
- data/lib/strapi_ruby/interface.rb +11 -12
- data/lib/strapi_ruby/tasks/config.rake +1 -1
- data/lib/strapi_ruby/validations.rb +2 -2
- data/lib/strapi_ruby/version.rb +1 -1
- metadata +3 -4
- data/features.md +0 -7
- data/strapi_ruby-0.1.0.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1ea7267c293e0bdd14967f64eedc922615f448b49d39a9ebdba914bb65b9336
|
4
|
+
data.tar.gz: 99c88f5464b84da4e5eb99e106b77c00436cdc413a9be26b9aa6e44e23a0a31a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96fffc52dfc440ebb3dcdefc07b26ced071d07cd96812d868388b7f4e382a2ef73814d9eb419e8c1f291c264fa68339fc3c263658b7b2535147709568b0a72e3
|
7
|
+
data.tar.gz: 4e8c52118420a2d2dd2273e66221fa00c38ab7639fa7898a00647e53aec279557bcbfa0fb40859e911735890f947346a0990a22dd55d639dc44b2ab3cc1115ce
|
data/.DS_Store
ADDED
Binary file
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,15 @@
|
|
1
|
+
## [0.1.3] - 2023-10-20
|
2
|
+
|
3
|
+
- Check for errors when using collection strapi parameters like sort/filters... on single items
|
4
|
+
- Fix Typo on ConfigError => ConfigurationError
|
5
|
+
|
6
|
+
## [0.1.2] - 2023-10-04
|
7
|
+
|
8
|
+
- Updated README
|
9
|
+
|
1
10
|
## [0.1.1] - 2023-10-04
|
2
11
|
|
3
|
-
- Added logger for ClientError and
|
12
|
+
- Added logger for ClientError and ConfigurationError
|
4
13
|
|
5
14
|
## [0.1.0] - 2023-10-04
|
6
15
|
|
data/README.md
CHANGED
@@ -15,10 +15,11 @@ I think it's one of the actual coolest solution for integrating a CMS into Rails
|
|
15
15
|
- [Installation](#installation)
|
16
16
|
- Usage:
|
17
17
|
- [API](#api):
|
18
|
-
- [get](#get)
|
19
|
-
- [post](#post)
|
20
|
-
- [put](#put)
|
21
|
-
- [delete](#delete)
|
18
|
+
- [.get](#get)
|
19
|
+
- [.post](#post)
|
20
|
+
- [.put](#put)
|
21
|
+
- [.delete](#delete)
|
22
|
+
- [.escape_empty_answer](#escape_empty_answer)
|
22
23
|
- [Basic Example: Rails](#basic-example-rails)
|
23
24
|
- [Strapi Parameters](#strapi-parameters):
|
24
25
|
- [populate](#populate)
|
@@ -34,6 +35,9 @@ I think it's one of the actual coolest solution for integrating a CMS into Rails
|
|
34
35
|
- [DateTime conversion](#datetime-conversion)
|
35
36
|
- [Markdown conversion](#markdown-conversion)
|
36
37
|
- [Faraday block](#faraday-block)
|
38
|
+
- [Handling Errors](#handling-errors)
|
39
|
+
- [Errors Classes](#errors-classes)
|
40
|
+
- [Graceful degradation](#graceful-degradation)
|
37
41
|
- [Contributing](#contributing)
|
38
42
|
- [Tests](#tests)
|
39
43
|
|
@@ -43,13 +47,15 @@ Add this line to your application's Gemfile:
|
|
43
47
|
|
44
48
|
```ruby
|
45
49
|
# Gemfile
|
46
|
-
|
47
50
|
gem "strapi_ruby"
|
48
51
|
```
|
49
52
|
|
53
|
+
|
54
|
+
|
50
55
|
Then if you use Rails, run in your terminal to generate a config initializer. Otherwise copy paste and fill the config block.
|
51
56
|
|
52
57
|
```bash
|
58
|
+
bundle
|
53
59
|
rake strapi_ruby:config
|
54
60
|
```
|
55
61
|
|
@@ -151,7 +157,7 @@ StrapiRuby.delete(resource: :articles, id: 12)
|
|
151
157
|
|
152
158
|
#### .escape_empty_answer
|
153
159
|
|
154
|
-
See [
|
160
|
+
See [`Graceful degradation`](#graceful-degradation)
|
155
161
|
|
156
162
|
### Basic Example: Rails
|
157
163
|
|
@@ -163,7 +169,7 @@ def home
|
|
163
169
|
end
|
164
170
|
```
|
165
171
|
|
166
|
-
```
|
172
|
+
```erb
|
167
173
|
# home.html.erb
|
168
174
|
|
169
175
|
<% StrapiRuby.escape_empty_answer(@articles) do %>
|
@@ -327,7 +333,7 @@ StrapiRuby.get(resource: :restaurants,
|
|
327
333
|
# --------------------------------
|
328
334
|
|
329
335
|
# Complex filtering with $and and $or
|
330
|
-
|
336
|
+
StrapiRuby.get(resource: :books,
|
331
337
|
filters: {
|
332
338
|
"$or" => [
|
333
339
|
{
|
@@ -394,7 +400,7 @@ StrapiRuby.get(resource: :articles, locale: :fr)
|
|
394
400
|
|
395
401
|
#### publication_state
|
396
402
|
|
397
|
-
Use
|
403
|
+
Use `:preview` or `:live`
|
398
404
|
|
399
405
|
```ruby
|
400
406
|
StrapiRuby.get(resource: :articles, publication_state: :preview)
|
@@ -507,7 +513,7 @@ default_headers = { "Content-Type" => "application/json",
|
|
507
513
|
|
508
514
|
Depending on your utilisation, there are multiple ways to handle errors.
|
509
515
|
|
510
|
-
####
|
516
|
+
#### Errors Classes
|
511
517
|
|
512
518
|
```ruby
|
513
519
|
# Config Error
|
@@ -527,7 +533,7 @@ class BadRequestError < ClientError
|
|
527
533
|
class JSONParsingError < ClientError
|
528
534
|
```
|
529
535
|
|
530
|
-
####
|
536
|
+
#### Graceful degradation
|
531
537
|
|
532
538
|
One way to handle errors and gracefuly degrade is using `.escape_empty_answer` and use a block to nest your data accessing code.
|
533
539
|
|
data/lib/strapi_ruby/client.rb
CHANGED
@@ -48,15 +48,13 @@ module StrapiRuby
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def performs_request
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
raise ConnectionError, "#{ErrorMessage.unexpected} #{e.message}"
|
59
|
-
end
|
51
|
+
yield
|
52
|
+
rescue Faraday::ConnectionFailed => e
|
53
|
+
raise ConnectionError, "#{ErrorMessage.connection_failed} #{e.message}"
|
54
|
+
rescue Faraday::TimeoutError => e
|
55
|
+
raise ConnectionError, "#{ErrorMessage.timeout} #{e.message}"
|
56
|
+
rescue StandardError => e
|
57
|
+
raise ConnectionError, "#{ErrorMessage.unexpected} #{e.message}"
|
60
58
|
end
|
61
59
|
|
62
60
|
def convert_json_to_open_struct(json)
|
@@ -17,10 +17,10 @@ module StrapiRuby
|
|
17
17
|
private
|
18
18
|
|
19
19
|
def build_endpoint
|
20
|
-
@result = if
|
21
|
-
"#{base_uri}/#{@resource}/#{@id}"
|
22
|
-
else
|
20
|
+
@result = if collection?
|
23
21
|
"#{base_uri}/#{@resource}"
|
22
|
+
else
|
23
|
+
"#{base_uri}/#{@resource}/#{@id}"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -28,8 +28,8 @@ module StrapiRuby
|
|
28
28
|
@result += @query if @query
|
29
29
|
end
|
30
30
|
|
31
|
-
def
|
32
|
-
|
31
|
+
def collection?
|
32
|
+
@id.nil?
|
33
33
|
end
|
34
34
|
|
35
35
|
def base_uri
|
@@ -6,6 +6,7 @@ module StrapiRuby
|
|
6
6
|
private
|
7
7
|
|
8
8
|
def sort(args)
|
9
|
+
check_collection
|
9
10
|
build_query_from_args(args, :sort)
|
10
11
|
end
|
11
12
|
|
@@ -18,6 +19,7 @@ module StrapiRuby
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def filters(args)
|
22
|
+
check_collection
|
21
23
|
build_query_from_args(args, :filters)
|
22
24
|
end
|
23
25
|
|
@@ -25,54 +27,39 @@ module StrapiRuby
|
|
25
27
|
raise TypeError, "#{ErrorMessage.expected_integer} Got #{number.class.name}" unless number.is_a?(Integer)
|
26
28
|
|
27
29
|
check_single_pagination
|
30
|
+
check_collection
|
28
31
|
@result += "#{prefix}pagination[pageSize]=#{number}"
|
29
32
|
end
|
30
33
|
|
31
|
-
# Sets the page number for the query result.
|
32
|
-
#
|
33
|
-
# @param number [Integer] An Integer representing the page number.
|
34
|
-
#
|
35
|
-
# @return [String] The updated query string with the page number option added.
|
36
34
|
def page(number)
|
37
35
|
raise TypeError, "#{ErrorMessage.expected_integer} Got #{number.class.name}" unless number.is_a?(Integer)
|
38
36
|
|
39
37
|
check_single_pagination
|
38
|
+
check_collection
|
40
39
|
@result += "#{prefix}pagination[page]=#{number}"
|
41
40
|
end
|
42
41
|
|
43
|
-
# Sets the offset for the query result.
|
44
|
-
#
|
45
|
-
# @param number [Integer] An Integer representing the offset.
|
46
|
-
#
|
47
|
-
# @return [String] The updated query string with the offset option added.
|
48
42
|
def start(number)
|
49
43
|
raise TypeError, "#{ErrorMessage.expected_integer} Got #{number.class.name}" unless number.is_a?(Integer)
|
50
44
|
|
51
45
|
check_single_pagination
|
46
|
+
check_collection
|
52
47
|
@result += "#{prefix}pagination[start]=#{number}"
|
53
48
|
end
|
54
49
|
|
55
|
-
# Sets the limit for the query result.
|
56
|
-
#
|
57
|
-
# @param number [Integer] An Integer representing the limit.
|
58
|
-
#
|
59
|
-
# @return [String] The updated query string with the limit option added.
|
60
50
|
def limit(number)
|
61
51
|
raise TypeError unless number.is_a?(Integer)
|
62
52
|
|
63
53
|
check_single_pagination
|
54
|
+
check_collection
|
64
55
|
@result += "#{prefix}pagination[limit]=#{number}"
|
65
56
|
end
|
66
57
|
|
67
|
-
##
|
68
|
-
# Sets the locale for the query result.
|
69
|
-
#
|
70
|
-
# @params arg [String, Symbol] A String or Symbol representing the locale.
|
71
|
-
#
|
72
|
-
# @return [String] The updated query string with the locale option added.
|
73
58
|
def locale(arg)
|
74
59
|
raise TypeError, "#{ErrorMessage.expected_string_symbol} Got #{arg.class.name}" unless arg.is_a?(String) || arg.is_a?(Symbol)
|
75
60
|
|
61
|
+
check_collection
|
62
|
+
|
76
63
|
@result += "#{prefix}locale=#{arg}"
|
77
64
|
end
|
78
65
|
|
@@ -80,11 +67,11 @@ module StrapiRuby
|
|
80
67
|
raise TypeError, "#{ErrorMessage.expected_string_symbol} Got #{arg.class.name}" unless arg.is_a?(String) || arg.is_a?(Symbol)
|
81
68
|
raise ArgumentError, "#{ErrorMessage.publication_state} Got #{arg}" unless arg.to_sym == :live || arg.to_sym == :preview
|
82
69
|
|
70
|
+
check_collection
|
71
|
+
|
83
72
|
@result += "#{prefix}publicationState=#{arg}"
|
84
73
|
end
|
85
74
|
|
86
|
-
# Checks params don't combine pagination methods.
|
87
|
-
#
|
88
75
|
def check_single_pagination
|
89
76
|
return unless (@options.key?(:page) && @options.key?(:start)) ||
|
90
77
|
(@options.key(:page) && @options.key?(:limit)) ||
|
@@ -94,9 +81,16 @@ module StrapiRuby
|
|
94
81
|
raise ArgumentError, ErrorMessage.pagination
|
95
82
|
end
|
96
83
|
|
97
|
-
|
98
|
-
|
99
|
-
|
84
|
+
def collection?
|
85
|
+
@options[:id].nil?
|
86
|
+
end
|
87
|
+
|
88
|
+
def check_collection
|
89
|
+
return if collection?
|
90
|
+
|
91
|
+
raise ArgumentError, ErrorMessage.collection_query
|
92
|
+
end
|
93
|
+
|
100
94
|
def prefix
|
101
95
|
@result.empty? ? "?" : "&"
|
102
96
|
end
|
@@ -20,23 +20,22 @@ module StrapiRuby
|
|
20
20
|
|
21
21
|
def escape_empty_answer(answer)
|
22
22
|
return answer.error.message if answer.data.nil? && answer.error
|
23
|
+
|
23
24
|
yield
|
24
25
|
end
|
25
26
|
|
26
27
|
private
|
27
28
|
|
28
29
|
def request(http_verb, options = {})
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
meta = answer.meta
|
30
|
+
validate_options(options)
|
31
|
+
@endpoint = build_endpoint(options)
|
32
|
+
answer = build_answer(http_verb, @endpoint, options)
|
33
|
+
data = format_data(answer.data, options)
|
34
|
+
meta = answer.meta
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
36
|
+
return_success_open_struct(data, meta, options)
|
37
|
+
rescue StrapiRuby::ClientError, StrapiRuby::ConfigurationError => e
|
38
|
+
return_error_open_struct(e, options)
|
40
39
|
end
|
41
40
|
|
42
41
|
def build_answer(http_verb, endpoint, options)
|
@@ -53,7 +52,7 @@ module StrapiRuby
|
|
53
52
|
options[:show_endpoint] || StrapiRuby.config.show_endpoint
|
54
53
|
end
|
55
54
|
|
56
|
-
def return_success_open_struct(data, meta,
|
55
|
+
def return_success_open_struct(data, meta, _error = nil, options = {})
|
57
56
|
if show_endpoint?(options)
|
58
57
|
OpenStruct.new(data: data,
|
59
58
|
meta: meta,
|
@@ -63,7 +62,7 @@ module StrapiRuby
|
|
63
62
|
end
|
64
63
|
end
|
65
64
|
|
66
|
-
def return_error_open_struct(error,
|
65
|
+
def return_error_open_struct(error, _options = {})
|
67
66
|
OpenStruct.new(error: OpenStruct.new(message: "#{error.class}: #{error.message}"),
|
68
67
|
endpoint: @endpoint,
|
69
68
|
data: nil,
|
@@ -20,7 +20,7 @@ namespace :strapi_ruby do
|
|
20
20
|
config.strapi_server_uri = "YOUR_SERVER_URI"
|
21
21
|
config.strapi_token = "YOUR_TOKEN"
|
22
22
|
end
|
23
|
-
|
23
|
+
CONFIG
|
24
24
|
File.write(config_file, config)
|
25
25
|
puts "StrapiRuby configuration file created at config/initializers/strapi_ruby.rb."
|
26
26
|
end
|
@@ -26,7 +26,7 @@ module StrapiRuby
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def validate_show_endpoint_config(show_endpoint)
|
29
|
-
raise TypeError,
|
29
|
+
raise TypeError, ErrorMessage.expected_boolean unless [true, false].include?(show_endpoint)
|
30
30
|
end
|
31
31
|
|
32
32
|
def validate_faraday_block(faraday)
|
@@ -52,7 +52,7 @@ module StrapiRuby
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def validate_show_endpoint_params(options)
|
55
|
-
raise TypeError,
|
55
|
+
raise TypeError, ErrorMessage.expected_boolean if options[:show_endpoint] && ![true, false].include?(options[:show_endpoint])
|
56
56
|
end
|
57
57
|
|
58
58
|
def validate_body(options)
|
data/lib/strapi_ruby/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strapi_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maxence Robinet
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-10-
|
11
|
+
date: 2023-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -90,6 +90,7 @@ executables: []
|
|
90
90
|
extensions: []
|
91
91
|
extra_rdoc_files: []
|
92
92
|
files:
|
93
|
+
- ".DS_Store"
|
93
94
|
- ".rspec"
|
94
95
|
- ".rubocop.yml"
|
95
96
|
- CHANGELOG.md
|
@@ -97,7 +98,6 @@ files:
|
|
97
98
|
- README.md
|
98
99
|
- Rakefile
|
99
100
|
- assets/strapi_ruby_logo.png
|
100
|
-
- features.md
|
101
101
|
- lib/strapi_ruby.rb
|
102
102
|
- lib/strapi_ruby/client.rb
|
103
103
|
- lib/strapi_ruby/config.rb
|
@@ -120,7 +120,6 @@ files:
|
|
120
120
|
- lib/strapi_ruby/version.rb
|
121
121
|
- rubocop.txt
|
122
122
|
- sig/strapi_ruby.rbs
|
123
|
-
- strapi_ruby-0.1.0.gem
|
124
123
|
- strapi_ruby.gemspec
|
125
124
|
homepage: https://github.com/saint-james-fr/strapi_ruby
|
126
125
|
licenses:
|
data/features.md
DELETED
data/strapi_ruby-0.1.0.gem
DELETED
Binary file
|