strapi 0.0.3 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1008e166d6c268ae6444d920af7a31d994be6e130569882e084ad334738188f5
4
- data.tar.gz: 4291af805a8ebcb38444060c9a9bff7adef39a128940ea93f8065d7325206370
3
+ metadata.gz: d53922d37bd74ca71fe212f659e11eae7ad4e234ff13d97086dfb726a5a1cf04
4
+ data.tar.gz: d5364ee3614d54b78b594111add3095672372a152b3ecdccfd8a5c2294dc05bc
5
5
  SHA512:
6
- metadata.gz: d766fba9e67d4bce68928c953fbb199ceb8a626d101938d863b4d4bc8260254e448da1b7c80db8c8b8a400a34743ca83681bb065577e3688cab3bc620729395a
7
- data.tar.gz: 9c9be3da48c59cd82263a8c3bfbc105b5074a1a041f0f76d68343137b1ecc15220e15f85e3d1b4523f9f2ba0eddac001f664c23644e7f8f91fada00f767e7208
6
+ metadata.gz: cde55210154b089606d519332a6b67dc8d9e06c9b2b656e582ebe60c19bf0d7694c4d7a8fc1b22ffb935af26bf6f8f9ca120f7a9a04f5a226efd1a61eefccba8
7
+ data.tar.gz: 816131f378cb4b7ea3855e6c51b78d62c84e7c597281449f23b66594781d6e1c02792dc23ddd63d87a5d7aad47aab7bf99d0295f21e81f0fed3d3ccab6401e06
data/Gemfile.lock CHANGED
@@ -1,16 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- strapi (0.0.3)
4
+ strapi (0.0.4)
5
5
  activesupport
6
6
  faraday (~> 2.0)
7
7
  faraday-net_http (~> 2.0)
8
- oj (~> 3.0)
9
8
 
10
9
  GEM
11
10
  remote: https://rubygems.org/
12
11
  specs:
13
- activesupport (7.0.0)
12
+ activesupport (7.0.1)
14
13
  concurrent-ruby (~> 1.0, >= 1.0.2)
15
14
  i18n (>= 1.6, < 2)
16
15
  minitest (>= 5.1)
@@ -24,10 +23,10 @@ GEM
24
23
  debug (1.4.0)
25
24
  irb (>= 1.3.6)
26
25
  reline (>= 0.2.7)
27
- faraday (2.0.0)
26
+ faraday (2.0.1)
27
+ faraday-net_http (~> 2.0)
28
28
  ruby2_keywords (>= 0.0.4)
29
- faraday-net_http (2.0.0)
30
- faraday (~> 2.0)
29
+ faraday-net_http (2.0.1)
31
30
  hashdiff (1.0.1)
32
31
  i18n (1.8.11)
33
32
  concurrent-ruby (~> 1.0)
@@ -35,7 +34,6 @@ GEM
35
34
  irb (1.4.1)
36
35
  reline (>= 0.3.0)
37
36
  minitest (5.15.0)
38
- oj (3.13.10)
39
37
  parallel (1.21.0)
40
38
  parser (3.0.3.2)
41
39
  ast (~> 2.4.1)
data/README.md CHANGED
@@ -27,22 +27,30 @@ And then execute:
27
27
 
28
28
  ## Usage
29
29
 
30
- This gem has only been tested with Strapi v4. It may work with previous versions of Strapi, but they
30
+ Note: this gem has only been tested with Strapi v4. It may work with previous versions of Strapi, but they
31
31
  remain untested at this time.
32
32
 
33
33
  ### Configuration
34
34
 
35
- You will first need to set an `ENV` variable for `STRAPI_HOST_URL` and `STRAPI_API_TOKEN` (the
36
- latter is only required if accessing content types requires authentication). This can be done with
37
- [`dotenv`](https://github.com/bkeepers/dotenv/), in an initializer, or some other mechanism.
35
+ You will first need to set an `ENV` variable for `STRAPI_HOST_URL`, `STRAPI_IDENTIFIER` and
36
+ `STRAPI_PASSWORD`. This can be done with [`dotenv`](https://github.com/bkeepers/dotenv/), in an
37
+ initializer, or some other mechanism.
38
38
 
39
39
  If using `dotev`, your `.env` file would contain:
40
40
 
41
41
  ```
42
42
  STRAPI_HOST_URL=http://localhost:1337
43
- STRAPI_API_TOKEN=asdf1234qwer5678
43
+ STRAPI_IDENTIFIER=admin@example.com
44
+ STRAPI_PASSWORD=password
44
45
  ```
45
46
 
47
+ The `STRAPI_IDENTIFIER` and `STRAPI_PASSWORD` should be the login information for a user with a role
48
+ that grants access to your authenticated content. [Strapi Authenticated request
49
+ documentation](https://docs.strapi.io/developer-docs/latest/guides/auth-request.html)
50
+
51
+ Upon running the first method that interacts with the Strapi API, a JWT token will be fetched and
52
+ cached.
53
+
46
54
  ### Defining Content Type Classes
47
55
 
48
56
  In Ruby, define some content type classes, i.e.:
@@ -122,6 +130,10 @@ farm.cows.first.name # => "Hershey"
122
130
  The class method `.where` also exists, which is the same implementation as `.all`, except a hash of
123
131
  API parameters is required.
124
132
 
133
+ ``` ruby
134
+ cows = Cow.where(filters: { name: { '$eq': 'Hershey' } })
135
+ ```
136
+
125
137
  ### Creating, Updating, Deleting
126
138
 
127
139
  You can create and update entries by calling `.save` on them:
@@ -5,28 +5,19 @@ module Strapi
5
5
  class Connection
6
6
  class << self
7
7
  def instance
8
- @instance ||= build_instance
9
- end
10
-
11
- private
12
-
13
- def build_instance
14
- unless Faraday.default_adapter
15
- require 'faraday/net_http'
16
- Faraday.default_adapter = :net_http
8
+ @instance ||= Faraday::Connection.new("#{ENV['STRAPI_HOST_URL']}/api") do |f|
9
+ f.request :json
10
+ f.response :json
11
+ f.adapter :net_http
17
12
  end
18
-
19
- Faraday::Connection.new("#{ENV['STRAPI_HOST_URL']}/api", options)
20
13
  end
21
14
 
22
- def options
23
- return unless (api_token = ENV['STRAPI_API_TOKEN'].presence)
24
-
25
- {
26
- headers: {
27
- 'Authorization' => "bearer #{api_token}"
28
- }
29
- }
15
+ def jwt_token
16
+ @jwt_token ||= instance.post(
17
+ 'auth/local',
18
+ identifier: ENV['STRAPI_IDENTIFIER'],
19
+ password: ENV['STRAPI_PASSWORD']
20
+ ).body['jwt']
30
21
  end
31
22
  end
32
23
  end
@@ -19,10 +19,10 @@ module Strapi
19
19
  other.is_a?(self.class) && id == other.id
20
20
  end
21
21
 
22
- def save(query_hash = {})
22
+ def save(params = {})
23
23
  return if deleted
24
24
 
25
- response = id ? update_request(query_hash) : create_request(query_hash)
25
+ response = id ? update_request(params) : create_request(params)
26
26
  entry = self.class.send(:new_from_response, response)
27
27
  tap do
28
28
  @attributes = attributes.deep_merge entry.attributes
@@ -30,8 +30,8 @@ module Strapi
30
30
  end
31
31
  end
32
32
 
33
- def delete(query_hash = {})
34
- Request.delete("#{self.class.send(:_plural_id)}/#{id}?#{query_hash.to_query}")
33
+ def delete(params = {})
34
+ Request.delete("#{self.class.send(:_plural_id)}/#{id}", params)
35
35
  @attributes = {}
36
36
  @id = nil
37
37
  @deleted = true
@@ -40,16 +40,16 @@ module Strapi
40
40
 
41
41
  private
42
42
 
43
- def create_request(query_hash)
43
+ def create_request(params)
44
44
  Request.post(
45
- "#{self.class.send(:_plural_id)}?#{query_hash.to_query}",
45
+ "#{self.class.send(:_plural_id)}?#{params.to_query}",
46
46
  data: attributes.slice(*self.class.fields)
47
47
  ).data
48
48
  end
49
49
 
50
- def update_request(query_hash)
50
+ def update_request(params)
51
51
  Request.put(
52
- "#{self.class.send(:_plural_id)}/#{id}?#{query_hash.to_query}",
52
+ "#{self.class.send(:_plural_id)}/#{id}?#{params.to_query}",
53
53
  data: attributes.slice(*self.class.fields)
54
54
  ).data
55
55
  end
@@ -61,7 +61,7 @@ module Strapi
61
61
  end
62
62
 
63
63
  def strapi_attr_value(attr, options)
64
- value = @attributes[attr]
64
+ return unless (value = @attributes[attr])
65
65
  return value unless (content_type = options[:content_type])
66
66
 
67
67
  content_type_class = content_type.is_a?(String) ? content_type.constantize : content_type
@@ -94,32 +94,34 @@ module Strapi
94
94
  end
95
95
  end
96
96
 
97
- def find(id, query_hash = {})
98
- new_from_response Request.get("#{_plural_id}/#{id}?#{query_hash.to_query}").data
97
+ def find(id, params = {})
98
+ new_from_response Request.get("#{_plural_id}/#{id}", params).data
99
99
  end
100
100
 
101
- def all(query_hash = {})
102
- get_list(query_hash)
101
+ def all(params = {})
102
+ get_list(params)
103
103
  end
104
104
 
105
- def where(query_hash)
106
- get_list(query_hash)
105
+ def where(params)
106
+ get_list(params)
107
107
  end
108
108
 
109
- def create(attributes, query_hash = {})
110
- new(attributes).save(query_hash)
109
+ def create(attributes, params = {})
110
+ new(attributes).save(params)
111
111
  end
112
112
 
113
113
  private
114
114
 
115
115
  def new_from_response(response)
116
+ return unless response
117
+
116
118
  new(response['attributes'].transform_keys(&:underscore)).tap do |entry|
117
119
  entry.instance_variable_set('@id', response['id'])
118
120
  end
119
121
  end
120
122
 
121
- def get_list(query_hash)
122
- Request.get("#{_plural_id}?#{query_hash.to_query}").data.map do |result|
123
+ def get_list(params)
124
+ Request.get(_plural_id, params).data.map do |result|
123
125
  new_from_response result
124
126
  end
125
127
  end
data/lib/strapi/media.rb CHANGED
@@ -19,7 +19,13 @@ module Strapi
19
19
  field :provider_metadata
20
20
 
21
21
  def url
22
- ENV['STRAPI_HOST_URL'] + @attributes['url']
22
+ return unless (url_string = @attributes[:url].presence)
23
+
24
+ if url_string.include?('//')
25
+ url_string
26
+ else
27
+ ENV['STRAPI_HOST_URL'] + url_string
28
+ end
23
29
  end
24
30
  end
25
31
  end
@@ -3,12 +3,35 @@
3
3
  module Strapi
4
4
  # A convenience wrapper around Faraday to make a request to the Strapi API
5
5
  class Request
6
+ ALLOWED_PARAM_KEYS = %i[sort filters populate fields pagination publicationState locale].freeze
7
+ REQUEST_VERBS_WITHOUT_BODIES = %i[get head delete trace].freeze
8
+
6
9
  class << self
7
10
  %i[get head delete trace post put patch].each do |method|
8
- define_method(method) do |*args|
9
- Response.new(Connection.instance.send(method, *args))
11
+ define_method(method) do |path, args = {}|
12
+ validate_query_params(args) if REQUEST_VERBS_WITHOUT_BODIES.include?(method)
13
+
14
+ Response.new(
15
+ Connection.instance.send(method, path, args) do |f|
16
+ f.headers = {
17
+ 'Authorization' => "bearer #{Connection.jwt_token}"
18
+ }
19
+ end
20
+ )
10
21
  end
11
22
  end
23
+
24
+ private
25
+
26
+ def validate_query_params(params)
27
+ unallowed_query_params = params.keys - ALLOWED_PARAM_KEYS
28
+ return unless unallowed_query_params.size.positive?
29
+
30
+ raise(
31
+ Error,
32
+ 'message' => "Unallowed query params - #{unallowed_query_params.join(', ')}"
33
+ )
34
+ end
12
35
  end
13
36
  end
14
37
  end
@@ -6,7 +6,7 @@ module Strapi
6
6
  attr_reader :parsed_response
7
7
 
8
8
  def initialize(faraday_response)
9
- @parsed_response = Oj.load(faraday_response.body)
9
+ @parsed_response = faraday_response.body
10
10
  return if faraday_response.success?
11
11
 
12
12
  raise Error, error
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Strapi
4
- VERSION = '0.0.3'
4
+ VERSION = '0.1.0'
5
5
  end
data/lib/strapi.rb CHANGED
@@ -5,7 +5,7 @@ require 'active_support/core_ext/string/inflections'
5
5
  require 'active_support/core_ext/object/to_query'
6
6
  require 'active_support/core_ext/hash/keys'
7
7
  require 'faraday'
8
- require 'oj'
8
+ require 'faraday/net_http'
9
9
 
10
10
  require_relative 'strapi/connection'
11
11
  require_relative 'strapi/content_type'
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.3
4
+ version: 0.1.0
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-05 00:00:00.000000000 Z
11
+ date: 2022-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.0'
55
- - !ruby/object:Gem::Dependency
56
- name: oj
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '3.0'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '3.0'
69
55
  description:
70
56
  email:
71
57
  - gmail@justintalbott.com
@@ -111,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
97
  - !ruby/object:Gem::Version
112
98
  version: '0'
113
99
  requirements: []
114
- rubygems_version: 3.2.32
100
+ rubygems_version: 3.3.3
115
101
  signing_key:
116
102
  specification_version: 4
117
103
  summary: Simple Ruby classes for Strapi content types