strapi 0.0.1 → 0.0.2
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/Gemfile.lock +2 -20
- data/README.md +54 -8
- data/lib/strapi/content_type.rb +59 -22
- data/lib/strapi/response.rb +0 -3
- data/lib/strapi/version.rb +1 -1
- data/lib/strapi.rb +1 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01f0caddbedae31af4265a1c71c53cd2e637b6ebee6e4781f39fed76a198bb38
|
4
|
+
data.tar.gz: 97c263e1c894cd5b78beb8fba42f8949bf9276271ae8506f2adba9e43599a096
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbc3c226031a49c023c5d901908903b1cbeffcef8f99cae0c9c0a88e193a25cace270144070995410546fd04d79734124cbbef904cd873992987bba74fc69fc6
|
7
|
+
data.tar.gz: 8abd4c2833d6210a4aa29c5dacb72bea903fa94c86fb1d30523622127976879a93dbcdbba8f5ffe417d16072828d87b58e364ca594b3746955c7edc01fcd76e1
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
strapi (0.0.
|
4
|
+
strapi (0.0.2)
|
5
5
|
activesupport (>= 5.0)
|
6
6
|
faraday (>= 1.0)
|
7
7
|
oj (>= 3.0)
|
@@ -23,25 +23,8 @@ GEM
|
|
23
23
|
debug (1.4.0)
|
24
24
|
irb (>= 1.3.6)
|
25
25
|
reline (>= 0.2.7)
|
26
|
-
faraday (
|
27
|
-
faraday-em_http (~> 1.0)
|
28
|
-
faraday-em_synchrony (~> 1.0)
|
29
|
-
faraday-excon (~> 1.1)
|
30
|
-
faraday-httpclient (~> 1.0.1)
|
31
|
-
faraday-net_http (~> 1.0)
|
32
|
-
faraday-net_http_persistent (~> 1.1)
|
33
|
-
faraday-patron (~> 1.0)
|
34
|
-
faraday-rack (~> 1.0)
|
35
|
-
multipart-post (>= 1.2, < 3)
|
26
|
+
faraday (2.0.0)
|
36
27
|
ruby2_keywords (>= 0.0.4)
|
37
|
-
faraday-em_http (1.0.0)
|
38
|
-
faraday-em_synchrony (1.0.0)
|
39
|
-
faraday-excon (1.1.0)
|
40
|
-
faraday-httpclient (1.0.1)
|
41
|
-
faraday-net_http (1.0.1)
|
42
|
-
faraday-net_http_persistent (1.2.0)
|
43
|
-
faraday-patron (1.0.0)
|
44
|
-
faraday-rack (1.0.0)
|
45
28
|
hashdiff (1.0.1)
|
46
29
|
i18n (1.8.11)
|
47
30
|
concurrent-ruby (~> 1.0)
|
@@ -49,7 +32,6 @@ GEM
|
|
49
32
|
irb (1.4.1)
|
50
33
|
reline (>= 0.3.0)
|
51
34
|
minitest (5.15.0)
|
52
|
-
multipart-post (2.1.1)
|
53
35
|
oj (3.13.10)
|
54
36
|
parallel (1.21.0)
|
55
37
|
parser (3.0.3.2)
|
data/README.md
CHANGED
@@ -12,6 +12,12 @@ your Strapi content types as it is to define them within Strapi itself.
|
|
12
12
|
Add this line to your application's Gemfile:
|
13
13
|
|
14
14
|
```ruby
|
15
|
+
gem 'strapi'
|
16
|
+
```
|
17
|
+
|
18
|
+
Or if you want to run the absolute latest, potentially unreleased version:
|
19
|
+
|
20
|
+
``` ruby
|
15
21
|
gem 'strapi', github: 'waymondo/strapi-ruby'
|
16
22
|
```
|
17
23
|
|
@@ -19,11 +25,11 @@ And then execute:
|
|
19
25
|
|
20
26
|
$ bundle install
|
21
27
|
|
22
|
-
This gem has only been tested with Strapi v4. It may work with previous versions of Strapi, but they
|
23
|
-
remain untested.
|
24
|
-
|
25
28
|
## Usage
|
26
29
|
|
30
|
+
This gem has only been tested with Strapi v4. It may work with previous versions of Strapi, but they
|
31
|
+
remain untested at this time.
|
32
|
+
|
27
33
|
### Configuration
|
28
34
|
|
29
35
|
You will first need to set an `ENV` variable for `STRAPI_HOST_URL` and `STRAPI_API_TOKEN` (the
|
@@ -37,7 +43,7 @@ STRAPI_HOST_URL=http://localhost:1337
|
|
37
43
|
STRAPI_API_TOKEN=asdf1234qwer5678
|
38
44
|
```
|
39
45
|
|
40
|
-
|
46
|
+
### Defining Content Type Classes
|
41
47
|
|
42
48
|
In Ruby, define some content type classes, i.e.:
|
43
49
|
|
@@ -107,7 +113,7 @@ parameter options are included by default, so if you want to eagerly load relate
|
|
107
113
|
example, you’ll need to specify that with the `populate` option:
|
108
114
|
|
109
115
|
``` ruby
|
110
|
-
cows = Cow.all(populate:
|
116
|
+
cows = Cow.all(populate: '*')
|
111
117
|
cows.first.farm.name # => "McDonald’s"
|
112
118
|
farm = Farm.find(1, populate: ['cows'])
|
113
119
|
farm.cows.first.name # => "Hershey"
|
@@ -116,9 +122,49 @@ farm.cows.first.name # => "Hershey"
|
|
116
122
|
The class method `.where` also exists, which is the same implementation as `.all`, except a hash of
|
117
123
|
API parameters is required.
|
118
124
|
|
119
|
-
###
|
125
|
+
### Creating, Updating, Deleting
|
126
|
+
|
127
|
+
You can create and update entries by calling `.save` on them:
|
128
|
+
|
129
|
+
``` ruby
|
130
|
+
cow = Cow.new(name: 'Hershey')
|
131
|
+
cow.id # => nil
|
132
|
+
cow.save # => performs POST request
|
133
|
+
cow.id # => 1
|
134
|
+
cow = Cow.find(1)
|
135
|
+
cow.name = 'Bessie'
|
136
|
+
cow.save # => performs PUT request
|
137
|
+
```
|
138
|
+
|
139
|
+
You can also use `create` class method to more concisely create entries:
|
120
140
|
|
121
|
-
|
141
|
+
``` ruby
|
142
|
+
cow = Cow.create(name: 'Bessie')
|
143
|
+
cow.id # => 1
|
144
|
+
```
|
145
|
+
|
146
|
+
You can delete entries by calling `.delete` on them:
|
147
|
+
|
148
|
+
``` ruby
|
149
|
+
cow = Cow.find(1)
|
150
|
+
cow.delete # => performs DELETE request
|
151
|
+
cow = Cow.find(1) # => raises Strapi::Error
|
152
|
+
```
|
153
|
+
|
154
|
+
### Error Handling
|
155
|
+
|
156
|
+
Any non successful Strapi request will raise a
|
157
|
+
[Strapi::Error](https://github.com/waymondo/strapi-ruby/blob/main/lib/strapi/error.rb) with the API
|
158
|
+
response’s status, message, and payload, which you can rescue and handle accordingly:
|
159
|
+
|
160
|
+
``` ruby
|
161
|
+
def show
|
162
|
+
cow = Cow.find(123)
|
163
|
+
render :cow, cow: cow
|
164
|
+
rescue Strapi::Error => e
|
165
|
+
render :error, message: e.message
|
166
|
+
end
|
167
|
+
```
|
122
168
|
|
123
169
|
## Development
|
124
170
|
|
@@ -133,7 +179,7 @@ file to [rubygems.org](https://rubygems.org).
|
|
133
179
|
|
134
180
|
## Contributing
|
135
181
|
|
136
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/waymondo/strapi.
|
182
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/waymondo/strapi-ruby.
|
137
183
|
|
138
184
|
## License
|
139
185
|
|
data/lib/strapi/content_type.rb
CHANGED
@@ -3,31 +3,57 @@
|
|
3
3
|
module Strapi
|
4
4
|
# The class for defining a Ruby class that represents a Strapi Content-Type
|
5
5
|
class ContentType
|
6
|
-
attr_reader :id, :attributes
|
6
|
+
attr_reader :id, :attributes, :deleted
|
7
7
|
|
8
|
-
def initialize(
|
9
|
-
@
|
10
|
-
@attributes = response_data['attributes'].transform_keys(&:underscore)
|
8
|
+
def initialize(attributes = {})
|
9
|
+
@attributes = attributes.symbolize_keys
|
11
10
|
end
|
12
11
|
|
13
|
-
|
14
|
-
|
12
|
+
%w[created_at updated_at published_at].each do |method|
|
13
|
+
define_method(method) do
|
14
|
+
datetime_from_timestamp method
|
15
|
+
end
|
15
16
|
end
|
16
17
|
|
17
|
-
def
|
18
|
-
|
18
|
+
def ==(other)
|
19
|
+
other.is_a?(self.class) && id == other.id
|
19
20
|
end
|
20
21
|
|
21
|
-
def
|
22
|
-
|
22
|
+
def save(query_hash = {})
|
23
|
+
return if deleted
|
24
|
+
|
25
|
+
response = id ? update_request(query_hash) : create_request(query_hash)
|
26
|
+
entry = self.class.send(:new_from_response, response)
|
27
|
+
tap do
|
28
|
+
@attributes = attributes.deep_merge entry.attributes
|
29
|
+
@id = entry.id
|
30
|
+
end
|
23
31
|
end
|
24
32
|
|
25
|
-
def
|
26
|
-
|
33
|
+
def delete(query_hash = {})
|
34
|
+
Request.delete("#{self.class.send(:_plural_id)}/#{id}?#{query_hash.to_query}")
|
35
|
+
@attributes = {}
|
36
|
+
@id = nil
|
37
|
+
@deleted = true
|
38
|
+
nil
|
27
39
|
end
|
28
40
|
|
29
41
|
private
|
30
42
|
|
43
|
+
def create_request(query_hash)
|
44
|
+
Request.post(
|
45
|
+
"#{self.class.send(:_plural_id)}?#{query_hash.to_query}",
|
46
|
+
data: attributes.slice(*self.class.fields)
|
47
|
+
).data
|
48
|
+
end
|
49
|
+
|
50
|
+
def update_request(query_hash)
|
51
|
+
Request.put(
|
52
|
+
"#{self.class.send(:_plural_id)}/#{id}?#{query_hash.to_query}",
|
53
|
+
data: attributes.slice(*self.class.fields)
|
54
|
+
).data
|
55
|
+
end
|
56
|
+
|
31
57
|
def datetime_from_timestamp(key)
|
32
58
|
return unless (timestamp = @attributes[key])
|
33
59
|
|
@@ -35,36 +61,41 @@ module Strapi
|
|
35
61
|
end
|
36
62
|
|
37
63
|
def strapi_attr_value(attr, options)
|
38
|
-
value = @attributes[attr
|
64
|
+
value = @attributes[attr]
|
39
65
|
return value unless (content_type = options[:content_type])
|
40
66
|
|
41
67
|
content_type_class = content_type.is_a?(String) ? content_type.constantize : content_type
|
42
68
|
if (data = value['data']).is_a?(Array)
|
43
69
|
data.map do |entry|
|
44
|
-
content_type_class.
|
70
|
+
content_type_class.send(:new_from_response, entry)
|
45
71
|
end
|
46
72
|
else
|
47
|
-
content_type_class.
|
73
|
+
content_type_class.send(:new_from_response, data)
|
48
74
|
end
|
49
75
|
end
|
50
76
|
|
51
77
|
class << self
|
78
|
+
attr_reader :fields
|
79
|
+
|
52
80
|
def plural_id(name)
|
53
81
|
@_plural_id = name
|
54
82
|
end
|
55
83
|
|
56
84
|
def field(attr, options = {})
|
85
|
+
@fields = [] if fields.nil?
|
86
|
+
fields << attr
|
87
|
+
|
57
88
|
define_method attr do
|
58
89
|
strapi_attr_value(attr, options)
|
59
90
|
end
|
60
91
|
|
61
92
|
define_method "#{attr}=" do |value|
|
62
|
-
attributes[attr
|
93
|
+
attributes[attr] = value
|
63
94
|
end
|
64
95
|
end
|
65
96
|
|
66
97
|
def find(id, query_hash = {})
|
67
|
-
|
98
|
+
new_from_response Request.get("#{_plural_id}/#{id}?#{query_hash.to_query}").data
|
68
99
|
end
|
69
100
|
|
70
101
|
def all(query_hash = {})
|
@@ -75,16 +106,22 @@ module Strapi
|
|
75
106
|
get_list(query_hash)
|
76
107
|
end
|
77
108
|
|
109
|
+
def create(attributes, query_hash = {})
|
110
|
+
new(attributes).save(query_hash)
|
111
|
+
end
|
112
|
+
|
78
113
|
private
|
79
114
|
|
80
|
-
def
|
81
|
-
|
82
|
-
|
115
|
+
def new_from_response(response)
|
116
|
+
new(response['attributes'].transform_keys(&:underscore)).tap do |entry|
|
117
|
+
entry.instance_variable_set('@id', response['id'])
|
83
118
|
end
|
84
119
|
end
|
85
120
|
|
86
|
-
def
|
87
|
-
Request.get("#{_plural_id}?#{query_hash.to_query}").data
|
121
|
+
def get_list(query_hash)
|
122
|
+
Request.get("#{_plural_id}?#{query_hash.to_query}").data.map do |result|
|
123
|
+
new_from_response result
|
124
|
+
end
|
88
125
|
end
|
89
126
|
|
90
127
|
def _plural_id
|
data/lib/strapi/response.rb
CHANGED
data/lib/strapi/version.rb
CHANGED
data/lib/strapi.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- justin talbott
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01-
|
11
|
+
date: 2022-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -75,12 +75,12 @@ files:
|
|
75
75
|
- lib/strapi/request.rb
|
76
76
|
- lib/strapi/response.rb
|
77
77
|
- lib/strapi/version.rb
|
78
|
-
homepage: https://github.com/waymondo/strapi
|
78
|
+
homepage: https://github.com/waymondo/strapi-ruby
|
79
79
|
licenses:
|
80
80
|
- MIT
|
81
81
|
metadata:
|
82
|
-
homepage_uri: https://github.com/waymondo/strapi
|
83
|
-
source_code_uri: https://github.com/waymondo/strapi
|
82
|
+
homepage_uri: https://github.com/waymondo/strapi-ruby
|
83
|
+
source_code_uri: https://github.com/waymondo/strapi-ruby
|
84
84
|
rubygems_mfa_required: 'true'
|
85
85
|
post_install_message:
|
86
86
|
rdoc_options: []
|