strapi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2fb69f8122cef08e5397ed248b28d6cadaec1f9bd957324d94741dc674515111
4
+ data.tar.gz: 148174f62efc4a4566cbe6b96982f8a9981d37dba5b6e875ce17146308c7a3c5
5
+ SHA512:
6
+ metadata.gz: cea0d86d27e200156e9f08ab505e32bd576ac6977de4965df0393e3106a92d549185577f580fc8a6b6d414ce4c1a1883496b3ec05b2a2776921b674768b8c759
7
+ data.tar.gz: f16548cd8666e6d8e9ee9f31f16c1d1dadf93d06a8c05e09397553ac9246b962503aabba2b8480a67f360b65275b4638e9d23b12c52c717d82c472fe0151e5af
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.7
4
+ SuggestExtensions: false
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'debug', '~> 1.4'
8
+ gem 'minitest', '~> 5.0'
9
+ gem 'rake', '~> 13.0'
10
+ gem 'rubocop', '~> 1.21'
11
+ gem 'webmock', '~> 3.14'
data/Gemfile.lock ADDED
@@ -0,0 +1,98 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ strapi (0.0.1)
5
+ activesupport (>= 5.0)
6
+ faraday (>= 1.0)
7
+ oj (>= 3.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (7.0.0)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 1.6, < 2)
15
+ minitest (>= 5.1)
16
+ tzinfo (~> 2.0)
17
+ addressable (2.8.0)
18
+ public_suffix (>= 2.0.2, < 5.0)
19
+ ast (2.4.2)
20
+ concurrent-ruby (1.1.9)
21
+ crack (0.4.5)
22
+ rexml
23
+ debug (1.4.0)
24
+ irb (>= 1.3.6)
25
+ reline (>= 0.2.7)
26
+ faraday (1.8.0)
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)
36
+ 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
+ hashdiff (1.0.1)
46
+ i18n (1.8.11)
47
+ concurrent-ruby (~> 1.0)
48
+ io-console (0.5.11)
49
+ irb (1.4.1)
50
+ reline (>= 0.3.0)
51
+ minitest (5.15.0)
52
+ multipart-post (2.1.1)
53
+ oj (3.13.10)
54
+ parallel (1.21.0)
55
+ parser (3.0.3.2)
56
+ ast (~> 2.4.1)
57
+ public_suffix (4.0.6)
58
+ rainbow (3.0.0)
59
+ rake (13.0.6)
60
+ regexp_parser (2.2.0)
61
+ reline (0.3.1)
62
+ io-console (~> 0.5)
63
+ rexml (3.2.5)
64
+ rubocop (1.24.1)
65
+ parallel (~> 1.10)
66
+ parser (>= 3.0.0.0)
67
+ rainbow (>= 2.2.2, < 4.0)
68
+ regexp_parser (>= 1.8, < 3.0)
69
+ rexml
70
+ rubocop-ast (>= 1.15.1, < 2.0)
71
+ ruby-progressbar (~> 1.7)
72
+ unicode-display_width (>= 1.4.0, < 3.0)
73
+ rubocop-ast (1.15.1)
74
+ parser (>= 3.0.1.1)
75
+ ruby-progressbar (1.11.0)
76
+ ruby2_keywords (0.0.5)
77
+ tzinfo (2.0.4)
78
+ concurrent-ruby (~> 1.0)
79
+ unicode-display_width (2.1.0)
80
+ webmock (3.14.0)
81
+ addressable (>= 2.8.0)
82
+ crack (>= 0.3.2)
83
+ hashdiff (>= 0.4.0, < 2.0.0)
84
+
85
+ PLATFORMS
86
+ arm64-darwin-21
87
+ x86_64-linux
88
+
89
+ DEPENDENCIES
90
+ debug (~> 1.4)
91
+ minitest (~> 5.0)
92
+ rake (~> 13.0)
93
+ rubocop (~> 1.21)
94
+ strapi!
95
+ webmock (~> 3.14)
96
+
97
+ BUNDLED WITH
98
+ 2.2.32
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 justin talbott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # Strapi
2
+
3
+ [Strapi](https://strapi.io) is an Open source Node.js Headless CMS to easily build customizable
4
+ APIs. It’s great for quickly building your data layer alongside a UI to manage it.
5
+
6
+ Say you (like me) like to quickly build sites and applications in Ruby and/or/on Rails. The goal of
7
+ the Strapi gem is to make it just as easy to define Ruby classes that represent and interact with
8
+ your Strapi content types as it is to define them within Strapi itself.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'strapi', github: 'waymondo/strapi-ruby'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle install
21
+
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
+ ## Usage
26
+
27
+ ### Configuration
28
+
29
+ You will first need to set an `ENV` variable for `STRAPI_HOST_URL` and `STRAPI_API_TOKEN` (the
30
+ latter is only required if accessing content types requires authentication). This can be done with
31
+ [`dotenv`](https://github.com/bkeepers/dotenv/), in an initializer, or some other mechanism.
32
+
33
+ If using `dotev`, your `.env` file would contain:
34
+
35
+ ```
36
+ STRAPI_HOST_URL=http://localhost:1337
37
+ STRAPI_API_TOKEN=asdf1234qwer5678
38
+ ```
39
+
40
+ #### Defining Content Type Classes
41
+
42
+ In Ruby, define some content type classes, i.e.:
43
+
44
+ ``` ruby
45
+ class Farm < Strapi::ContentType
46
+ field :name
47
+ field :cows, content_type: 'Cow'
48
+ field :photo, content_type: 'Strapi::Media'
49
+ end
50
+ ```
51
+
52
+ ``` ruby
53
+ class Cow < Strapi::ContentType
54
+ field :name
55
+ field :farm, content_type: 'Farm'
56
+ end
57
+ ```
58
+
59
+ Using the `field` class method will define getter and setter methods for the class objects. If you
60
+ supply a class name to the `content_type` option, it will transform it to an instance of that class
61
+ when using the getter method. This works for both one-to-one and one-to-many relations:
62
+
63
+ ``` ruby
64
+ cow.name # => "Hershey"
65
+ cow.farm # => #<Farm>
66
+ farm.name # => "McDonald’s"
67
+ farm.cows # => [#<Cow>, #<Cow>]
68
+ ```
69
+
70
+ In keeping with ruby conventions, attribute keys returned from your Strapi API will be underscored,
71
+ so if you have an attribute of `"colorPattern": "jersey"` in your JSON, your field definition should
72
+ be `field :color_pattern`.
73
+
74
+ [`Strapi::Media`](https://github.com/waymondo/strapi-ruby/blob/main/lib/strapi/media.rb) is an
75
+ included content type class to represent photos, videos, and files. Feel free to extend this class
76
+ if you would like to add additional functionality or granularity.
77
+
78
+ ``` ruby
79
+ farm.photo # => #<Strapi::Media>
80
+ farm.photo.url # => "http://localhost:1337/uploads/farm-1.jpg"
81
+ ```
82
+
83
+ By default, `Strapi::ContentType` will infer it’s API plural id from the demodulized, dasherized,
84
+ pluralized name of the ruby class. For example, it would assume a class of `FarmWorker` would have a
85
+ Strapi plural ID of `farm-workers`. If you would like to customize this, you can set it in the class
86
+ definition:
87
+
88
+ ``` ruby
89
+ class FarmWorker
90
+ plural_id 'farmers'
91
+ end
92
+ ```
93
+
94
+ ### Fetching Entries
95
+
96
+ `Strapi::ContentType` provides some predictable methods for retrieving entries from your Strapi API:
97
+
98
+ ``` ruby
99
+ Cow.all # => [#<Cow>, #<Cow>, #<Cow>]
100
+ cow = Cow.find(1)
101
+ cow.id # => 1
102
+ ```
103
+
104
+ Both `.all` and `.find` accept a hash of options that map to Strapi’s allowed [API
105
+ parameters](https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html). No
106
+ parameter options are included by default, so if you want to eagerly load related content types for
107
+ example, you’ll need to specify that with the `populate` option:
108
+
109
+ ``` ruby
110
+ cows = Cow.all(populate: "*")
111
+ cows.first.farm.name # => "McDonald’s"
112
+ farm = Farm.find(1, populate: ['cows'])
113
+ farm.cows.first.name # => "Hershey"
114
+ ```
115
+
116
+ The class method `.where` also exists, which is the same implementation as `.all`, except a hash of
117
+ API parameters is required.
118
+
119
+ ### CRUD
120
+
121
+ Coming Soon
122
+
123
+ ## Development
124
+
125
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run
126
+ the tests. You can also run `bin/console` for an interactive prompt that will allow you to
127
+ experiment.
128
+
129
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new
130
+ version, update the version number in `version.rb`, and then run `bundle exec rake release`, which
131
+ will create a git tag for the version, push git commits and the created tag, and push the `.gem`
132
+ file to [rubygems.org](https://rubygems.org).
133
+
134
+ ## Contributing
135
+
136
+ Bug reports and pull requests are welcome on GitHub at https://github.com/waymondo/strapi.
137
+
138
+ ## License
139
+
140
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ t.warning = false
11
+ end
12
+
13
+ require 'rubocop/rake_task'
14
+
15
+ RuboCop::RakeTask.new
16
+
17
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'strapi'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strapi
4
+ # The singleton class representing the Faraday connection to Strapi
5
+ class Connection
6
+ class << self
7
+ def instance
8
+ @instance ||= Faraday::Connection.new("#{ENV['STRAPI_HOST_URL']}/api", options)
9
+ end
10
+
11
+ def options
12
+ return unless (api_token = ENV['STRAPI_API_TOKEN'].presence)
13
+
14
+ {
15
+ headers: {
16
+ 'Authorization' => "bearer #{api_token}"
17
+ }
18
+ }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strapi
4
+ # The class for defining a Ruby class that represents a Strapi Content-Type
5
+ class ContentType
6
+ attr_reader :id, :attributes
7
+
8
+ def initialize(response_data)
9
+ @id = response_data['id']
10
+ @attributes = response_data['attributes'].transform_keys(&:underscore)
11
+ end
12
+
13
+ def created_at
14
+ datetime_from_timestamp 'created_at'
15
+ end
16
+
17
+ def updated_at
18
+ datetime_from_timestamp 'updated_at'
19
+ end
20
+
21
+ def published_at
22
+ datetime_from_timestamp 'published_at'
23
+ end
24
+
25
+ def ==(other)
26
+ other.is_a?(self.class) && id == other.id
27
+ end
28
+
29
+ private
30
+
31
+ def datetime_from_timestamp(key)
32
+ return unless (timestamp = @attributes[key])
33
+
34
+ DateTime.parse timestamp
35
+ end
36
+
37
+ def strapi_attr_value(attr, options)
38
+ value = @attributes[attr.to_s]
39
+ return value unless (content_type = options[:content_type])
40
+
41
+ content_type_class = content_type.is_a?(String) ? content_type.constantize : content_type
42
+ if (data = value['data']).is_a?(Array)
43
+ data.map do |entry|
44
+ content_type_class.new(entry)
45
+ end
46
+ else
47
+ content_type_class.new(data)
48
+ end
49
+ end
50
+
51
+ class << self
52
+ def plural_id(name)
53
+ @_plural_id = name
54
+ end
55
+
56
+ def field(attr, options = {})
57
+ define_method attr do
58
+ strapi_attr_value(attr, options)
59
+ end
60
+
61
+ define_method "#{attr}=" do |value|
62
+ attributes[attr.to_s] = value
63
+ end
64
+ end
65
+
66
+ def find(id, query_hash = {})
67
+ new Request.get("#{_plural_id}/#{id}?#{query_hash.to_query}").data
68
+ end
69
+
70
+ def all(query_hash = {})
71
+ get_list(query_hash)
72
+ end
73
+
74
+ def where(query_hash)
75
+ get_list(query_hash)
76
+ end
77
+
78
+ private
79
+
80
+ def get_list(query_hash)
81
+ strapi_filter_query(query_hash).map do |result|
82
+ new result
83
+ end
84
+ end
85
+
86
+ def strapi_filter_query(query_hash)
87
+ Request.get("#{_plural_id}?#{query_hash.to_query}").data
88
+ end
89
+
90
+ def _plural_id
91
+ @_plural_id ||= to_s.demodulize.tableize.dasherize
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strapi
4
+ # An error raised within the Strapi ruby library
5
+ class Error < StandardError
6
+ attr_reader :status, :name, :message, :details
7
+
8
+ def initialize(hash = {})
9
+ super
10
+ @status = hash['status']
11
+ @name = hash['name']
12
+ @message = hash['message']
13
+ @details = hash['details']
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strapi
4
+ # The Content-Type for uploaded Strapi photos, videos, and files
5
+ class Media < ContentType
6
+ field :name
7
+ field :alternative_text
8
+ field :caption
9
+ field :width
10
+ field :height
11
+ field :formats # TODO: attribute value
12
+ field :hash
13
+ field :ext
14
+ field :mime
15
+ field :size
16
+ field :url
17
+ field :preview_url
18
+ field :preview_provider
19
+ field :provider_metadata
20
+
21
+ def url
22
+ ENV['STRAPI_HOST_URL'] + @attributes['url']
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strapi
4
+ # A convenience wrapper around Faraday to make a request to the Strapi API
5
+ class Request
6
+ class << self
7
+ %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))
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strapi
4
+ # The parsed response returned from the Strapi API
5
+ class Response
6
+ attr_reader :parsed_response
7
+
8
+ def initialize(faraday_response)
9
+ @parsed_response = Oj.load(faraday_response.body)
10
+ puts '########################'
11
+ p @parsed_response
12
+ puts '########################'
13
+ return if faraday_response.success?
14
+
15
+ raise Error, error
16
+ end
17
+
18
+ %w[data error].each do |method|
19
+ define_method(method) do
20
+ @parsed_response[method]
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strapi
4
+ VERSION = '0.0.1'
5
+ end
data/lib/strapi.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+ require 'active_support/core_ext/string/inflections'
5
+ require 'active_support/core_ext/object/to_query'
6
+ require 'faraday'
7
+ require 'oj'
8
+
9
+ require_relative 'strapi/connection'
10
+ require_relative 'strapi/content_type'
11
+ require_relative 'strapi/error'
12
+ require_relative 'strapi/media'
13
+ require_relative 'strapi/request'
14
+ require_relative 'strapi/response'
15
+ require_relative 'strapi/version'
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - justin talbott
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: oj
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Easily define Strapi content as Ruby classes
56
+ email:
57
+ - gmail@justintalbott.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rubocop.yml"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/strapi.rb
71
+ - lib/strapi/connection.rb
72
+ - lib/strapi/content_type.rb
73
+ - lib/strapi/error.rb
74
+ - lib/strapi/media.rb
75
+ - lib/strapi/request.rb
76
+ - lib/strapi/response.rb
77
+ - lib/strapi/version.rb
78
+ homepage: https://github.com/waymondo/strapi
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ homepage_uri: https://github.com/waymondo/strapi
83
+ source_code_uri: https://github.com/waymondo/strapi
84
+ rubygems_mfa_required: 'true'
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.7.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.2.32
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Easily define Strapi content as Ruby classes
104
+ test_files: []