mc_record 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 45add1ffe0738ebb9f9d925f7e66a733fa80dfc52afd51f1b578383976f151c1
4
+ data.tar.gz: 3006284b78f0a555e562c95f8c1be07cc966148f714e8f096377eefa6774e0c7
5
+ SHA512:
6
+ metadata.gz: e89b677a6711cd704253ce9802958fd1b19c9522476d9cca6ba42dc6756ab6c4bcf6e431d2c74fb38d5798e871722c303ee75e311f68f76f35e39a0d2a815534
7
+ data.tar.gz: 95a932b56203ddfadebf6f03fbec2aba09ba2466a2834704d44cd605c044d9c847a656f1874c008fb4b8e452626ab3428efc0c6f1263fbef70c12cece97e0904
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.4
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in mc_record.gemspec
6
+ gemspec
7
+
8
+ gem "activesupport", "7.0.6"
9
+ gem "microcms-ruby-sdk", "1.2.0"
10
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mc_record (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (7.0.6)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 1.6, < 2)
12
+ minitest (>= 5.1)
13
+ tzinfo (~> 2.0)
14
+ concurrent-ruby (1.2.2)
15
+ i18n (1.14.1)
16
+ concurrent-ruby (~> 1.0)
17
+ microcms-ruby-sdk (1.2.0)
18
+ minitest (5.18.1)
19
+ rake (13.0.6)
20
+ tzinfo (2.0.6)
21
+ concurrent-ruby (~> 1.0)
22
+
23
+ PLATFORMS
24
+ x86_64-linux
25
+
26
+ DEPENDENCIES
27
+ activesupport (= 7.0.6)
28
+ mc_record!
29
+ microcms-ruby-sdk (= 1.2.0)
30
+ rake (~> 13.0)
31
+
32
+ BUNDLED WITH
33
+ 2.4.13
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Akira kure
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,8 @@
1
+ TBD...
2
+
3
+ # McRecord
4
+
5
+ ## Installation
6
+
7
+
8
+ ## Usage
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,193 @@
1
+ require "active_support"
2
+ require "microcms"
3
+ require "date"
4
+
5
+ module McRecord
6
+ class ContentNotFound < StandardError
7
+ end
8
+
9
+ class Base
10
+ extend ActiveSupport::Concern
11
+
12
+ # @param [OpenStruct]
13
+ def initialize(attributes)
14
+ @attributes = attributes
15
+ end
16
+
17
+ def self.config(service_domain:, api_key:, end_point:)
18
+ MicroCMS.service_domain = service_domain
19
+ MicroCMS.api_key = api_key
20
+ @@end_point = end_point
21
+ end
22
+
23
+ # return Array<OpenStruct>
24
+ def self.all
25
+ contents = fetch_contents
26
+ contents.flatten!
27
+
28
+ open_structs_to_instances(contents)
29
+ rescue MicroCMS::APIError
30
+ raise ContentNotFound
31
+ end
32
+
33
+ # @param [String]
34
+ def self.find(id)
35
+ raise ".find is only support String parameter." unless id.instance_of?(String)
36
+
37
+ # OpenStruct
38
+ api_result = MicroCMS.get(
39
+ @@end_point,
40
+ id
41
+ )
42
+
43
+ attr_names = api_result.to_h.keys
44
+ attr_names.each do |attr_name|
45
+ define_method_attribute_if_needed(attr_name)
46
+ end
47
+
48
+ self.new(api_result)
49
+ rescue MicroCMS::APIError
50
+ raise ContentNotFound
51
+ end
52
+
53
+ # @param [Hash]
54
+ def self.where(arg)
55
+ param = build_filter_params(arg)
56
+
57
+ raise "param not specified" if param == ""
58
+
59
+ contents = fetch_contents(param)
60
+ contents.flatten!
61
+
62
+ open_structs_to_instances(contents)
63
+ end
64
+
65
+ # @param [Hash]
66
+ def self.where_not(arg)
67
+ param = build_not_filter_params(arg)
68
+
69
+ raise "param not specified" if param == ""
70
+
71
+ contents = fetch_contents(param)
72
+ contents.flatten!
73
+
74
+ open_structs_to_instances(contents)
75
+ end
76
+
77
+ class << self
78
+ private
79
+
80
+ def define_method_attribute_if_needed(attr_name)
81
+ unless self.method_defined?(attr_name)
82
+ class_eval %Q{
83
+ def #{attr_name}
84
+ read_attribute("#{attr_name}")
85
+ end
86
+ }
87
+ end
88
+
89
+ unless self.method_defined?("=#{attr_name}")
90
+ class_eval %Q{
91
+ def #{attr_name}=(value)
92
+ write_attribute("#{attr_name}", value)
93
+ end
94
+ }
95
+ end
96
+ end
97
+
98
+ # @param [Hash]
99
+ # @return [String]
100
+ def build_filter_params(arg)
101
+ filters_value = ""
102
+ arg.map do |k, v|
103
+ if v.instance_of?(Range)
104
+ begin_ = v.begin
105
+ end_ = v.end
106
+
107
+ result = ""
108
+ unless begin_.nil?
109
+ result += "#{k}[greater_than]#{begin_}"
110
+ end
111
+
112
+ unless end_.nil?
113
+ result += unless result == ""
114
+ "[and]#{k}[less_than]#{end_}"
115
+ else
116
+ "#{k}[less_than]#{end_}"
117
+ end
118
+ end
119
+
120
+ result
121
+ else
122
+ "#{k}[equals]#{v}"
123
+ end
124
+ end.join("[and]")
125
+ end
126
+
127
+ # @param [Hash]
128
+ # @return [String]
129
+ def build_not_filter_params(arg)
130
+ filters_value = ""
131
+ arg.map do |k, v|
132
+ if v.instance_of?(Range)
133
+ rais "not support parameter"
134
+ else
135
+ "#{k}[not_equals]#{v}"
136
+ end
137
+ end.join("[and]")
138
+ end
139
+
140
+ # @param [Array<OpenStruct>]
141
+ # @return [Array<Object>]
142
+ def open_structs_to_instances(contents)
143
+ contents.map do |content|
144
+ attr_names = content.to_h.keys
145
+ attr_names.each do |attr_name|
146
+ define_method_attribute_if_needed(attr_name)
147
+ end
148
+
149
+ self.new(content)
150
+ end
151
+ end
152
+
153
+ # @return [Array<OpenStruct>]
154
+ def fetch_contents(param = nil)
155
+ contents = []
156
+ limit = 10
157
+ offset_number = 0
158
+
159
+ print "fetching contents ."
160
+ loop do
161
+ print "."
162
+ api_result = MicroCMS.list(
163
+ @@end_point,
164
+ {
165
+ offset: offset_number,
166
+ limit: limit,
167
+ filters: param,
168
+ }
169
+ )
170
+
171
+ total_count = api_result.total_count
172
+ contents << api_result.contents
173
+
174
+ if total_count >= offset_number + 1
175
+ offset_number += limit
176
+ else
177
+ break
178
+ end
179
+ end
180
+
181
+ contents
182
+ end
183
+ end
184
+
185
+ def read_attribute(attr_name)
186
+ @attributes.to_h[attr_name.to_sym]
187
+ end
188
+
189
+ def write_attribute(attr_name, value)
190
+ @attributes[attr_name] = value
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module McRecord
4
+ VERSION = "0.1.0"
5
+ end
data/lib/mc_record.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "mc_record/base"
4
+ require_relative "mc_record/version"
5
+
6
+ module McRecord
7
+ end
data/mc_record.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/mc_record/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mc_record"
7
+ spec.version = McRecord::VERSION
8
+ spec.authors = ["Akira Kure"]
9
+ spec.email = ["kuredev@gmail.com"]
10
+
11
+ spec.summary = "MicroCMS API's ORM like ActiveRecord for Ruby."
12
+ spec.description = "MicroCMS API's ORM like ActiveRecord for Ruby."
13
+ spec.homepage = "https://github.com/kuredev/mc_record"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/kuredev/mc_record"
20
+ spec.metadata["changelog_uri"] = "https://github.com/kuredev/mc_record"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ # Uncomment to register a new dependency of your gem
34
+ # spec.add_dependency "example-gem", "~> 1.0"
35
+
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
data/sig/mc_record.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module McRecord
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mc_record
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Akira Kure
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-06-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: MicroCMS API's ORM like ActiveRecord for Ruby.
14
+ email:
15
+ - kuredev@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".ruby-version"
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/mc_record.rb
27
+ - lib/mc_record/base.rb
28
+ - lib/mc_record/version.rb
29
+ - mc_record.gemspec
30
+ - sig/mc_record.rbs
31
+ homepage: https://github.com/kuredev/mc_record
32
+ licenses: []
33
+ metadata:
34
+ homepage_uri: https://github.com/kuredev/mc_record
35
+ source_code_uri: https://github.com/kuredev/mc_record
36
+ changelog_uri: https://github.com/kuredev/mc_record
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 2.6.0
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.3.26
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: MicroCMS API's ORM like ActiveRecord for Ruby.
56
+ test_files: []