microcms-ruby-sdk 1.0.1 → 1.2.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 +4 -4
- data/.rubocop.yml +4 -0
- data/README.md +2 -2
- data/examples/examples.rb +3 -3
- data/lib/microcms/version.rb +1 -1
- data/lib/microcms.rb +85 -45
- data/microcms.gemspec +5 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ac22f7433f5c15562d37eb291ab04297082a36b02fe34b14206f578fdd8a6cb
|
4
|
+
data.tar.gz: 5411bed835d189ca46f280fada06bb7b25a2eb937d0fda8d478be9b52fe43560
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 294a44d8524dadb0389169bbb0022055dbfd94c53da7f99c74c37c8f88baa64192bb56f08a8b8c0915cad6297ac56c470ed2ba4de609d87320b7146fc9b5da6e
|
7
|
+
data.tar.gz: d0fb8d0095703342ddaa6145483691669c54f69f59a1a8446112bb353e0f28251bb6368f48b73e4d559a81d28e25f2cf702b51f5e93d6663963ea7fe65b8aaa5
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -49,7 +49,7 @@ puts MicroCMS.list('endpoint')
|
|
49
49
|
puts MicroCMS.list(
|
50
50
|
'endpoint',
|
51
51
|
{
|
52
|
-
|
52
|
+
draft_key: "abcd",
|
53
53
|
limit: 100,
|
54
54
|
offset: 1,
|
55
55
|
orders: ['updatedAt'],
|
@@ -152,4 +152,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
152
152
|
|
153
153
|
## Contributing
|
154
154
|
|
155
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
155
|
+
Bug reports and pull requests are welcome on GitHub at <https://github.com/microcmsio/microcms-ruby-sdk>.
|
data/examples/examples.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
MicroCMS.service_domain = ENV
|
4
|
-
MicroCMS.api_key = ENV
|
3
|
+
MicroCMS.service_domain = ENV.fetch('YOUR_DOMAIN', nil)
|
4
|
+
MicroCMS.api_key = ENV.fetch('YOUR_API_KEY', nil)
|
5
5
|
|
6
|
-
endpoint = ENV
|
6
|
+
endpoint = ENV.fetch('YOUR_ENDPOINT', nil)
|
7
7
|
|
8
8
|
puts MicroCMS.list(endpoint)
|
9
9
|
|
data/lib/microcms/version.rb
CHANGED
data/lib/microcms.rb
CHANGED
@@ -20,15 +20,79 @@ module MicroCMS
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
+
# HttpUtil
|
24
|
+
module HttpUtil
|
25
|
+
def send_http_request(method, endpoint, path, query = nil, body = nil)
|
26
|
+
uri = build_uri(endpoint, path, query)
|
27
|
+
http = build_http(uri)
|
28
|
+
req = build_request(method, uri, body)
|
29
|
+
res = http.request(req)
|
30
|
+
|
31
|
+
raise APIError.new(status_code: res.code.to_i, body: res.body) if res.code.to_i >= 400
|
32
|
+
|
33
|
+
JSON.parse(res.body, object_class: OpenStruct) if res.header['Content-Type'].include?('application/json') # rubocop:disable Style/OpenStructUse
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def get_request_class(method)
|
39
|
+
{
|
40
|
+
GET: Net::HTTP::Get,
|
41
|
+
POST: Net::HTTP::Post,
|
42
|
+
PUT: Net::HTTP::Put,
|
43
|
+
PATCH: Net::HTTP::Patch,
|
44
|
+
DELETE: Net::HTTP::Delete
|
45
|
+
}[method]
|
46
|
+
end
|
47
|
+
|
48
|
+
def build_request(method, uri, body)
|
49
|
+
req = get_request_class(method.to_sym).new(uri.request_uri)
|
50
|
+
req['X-MICROCMS-API-KEY'] = @api_key
|
51
|
+
if body
|
52
|
+
req['Content-Type'] = 'application/json'
|
53
|
+
req.body = JSON.dump(body)
|
54
|
+
end
|
55
|
+
|
56
|
+
req
|
57
|
+
end
|
58
|
+
|
59
|
+
def build_uri(endpoint, path, query)
|
60
|
+
origin = "https://#{@service_domain}.microcms.io"
|
61
|
+
path_with_id = path ? "/api/v1/#{endpoint}/#{path}" : "/api/v1/#{endpoint}"
|
62
|
+
encoded_query =
|
63
|
+
if !query || query.size.zero?
|
64
|
+
''
|
65
|
+
else
|
66
|
+
"?#{URI.encode_www_form(query)}"
|
67
|
+
end
|
68
|
+
|
69
|
+
URI.parse("#{origin}#{path_with_id}#{encoded_query}")
|
70
|
+
end
|
71
|
+
|
72
|
+
def build_http(uri)
|
73
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
74
|
+
http.use_ssl = true
|
75
|
+
|
76
|
+
http
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
23
80
|
# Client
|
24
81
|
class Client
|
82
|
+
include HttpUtil
|
83
|
+
|
25
84
|
def initialize(service_domain, api_key)
|
26
85
|
@service_domain = service_domain
|
27
86
|
@api_key = api_key
|
28
87
|
end
|
29
88
|
|
30
89
|
def list(endpoint, option = {})
|
31
|
-
send_http_request('GET', endpoint, nil, build_query(option))
|
90
|
+
list = send_http_request('GET', endpoint, nil, build_query(option))
|
91
|
+
if list[:totalCount]
|
92
|
+
list[:total_count] = list[:totalCount]
|
93
|
+
list.delete_field(:totalCount)
|
94
|
+
end
|
95
|
+
list
|
32
96
|
end
|
33
97
|
|
34
98
|
def get(endpoint, id = '', option = {})
|
@@ -63,6 +127,7 @@ module MicroCMS
|
|
63
127
|
|
64
128
|
private
|
65
129
|
|
130
|
+
# rubocop:disable Style/MethodLength
|
66
131
|
def build_query(option)
|
67
132
|
{
|
68
133
|
draftKey: option[:draftKey],
|
@@ -72,9 +137,11 @@ module MicroCMS
|
|
72
137
|
q: option[:q],
|
73
138
|
fields: option[:fields] ? option[:fields].join(',') : nil,
|
74
139
|
filters: option[:filters],
|
75
|
-
depth: option[:depth]
|
140
|
+
depth: option[:depth],
|
141
|
+
ids: option[:ids] ? option[:ids].join(',') : nil
|
76
142
|
}.select { |_key, value| value }
|
77
143
|
end
|
144
|
+
# rubocop:enable Style/MethodLength
|
78
145
|
|
79
146
|
def put(endpoint, content, option = {})
|
80
147
|
body = content.reject { |key, _value| key == :id }
|
@@ -84,57 +151,30 @@ module MicroCMS
|
|
84
151
|
def post(endpoint, content, option = {})
|
85
152
|
send_http_request('POST', endpoint, nil, option, content)
|
86
153
|
end
|
154
|
+
end
|
87
155
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
req = build_request(method, uri, body)
|
92
|
-
res = http.request(req)
|
156
|
+
# APIError
|
157
|
+
class APIError < StandardError
|
158
|
+
attr_accessor :status_code, :body
|
93
159
|
|
94
|
-
|
160
|
+
def initialize(status_code:, body:)
|
161
|
+
@status_code = status_code
|
162
|
+
@body = parse_body(body)
|
95
163
|
|
96
|
-
|
164
|
+
message = @body['message'] || 'Unknown error occured.'
|
165
|
+
super(message)
|
97
166
|
end
|
98
167
|
|
99
|
-
def
|
100
|
-
{
|
101
|
-
GET: Net::HTTP::Get,
|
102
|
-
POST: Net::HTTP::Post,
|
103
|
-
PUT: Net::HTTP::Put,
|
104
|
-
PATCH: Net::HTTP::Patch,
|
105
|
-
DELETE: Net::HTTP::Delete
|
106
|
-
}[method]
|
168
|
+
def inspect
|
169
|
+
"#<#{self.class.name} @status_code=#{status_code}, @body=#{body.inspect} @message=#{message.inspect}>"
|
107
170
|
end
|
108
171
|
|
109
|
-
|
110
|
-
req = get_request_class(method.to_sym).new(uri.request_uri)
|
111
|
-
req['X-MICROCMS-API-KEY'] = @api_key
|
112
|
-
if body
|
113
|
-
req['Content-Type'] = 'application/json'
|
114
|
-
req.body = JSON.dump(body)
|
115
|
-
end
|
116
|
-
|
117
|
-
req
|
118
|
-
end
|
119
|
-
|
120
|
-
def build_uri(endpoint, path, query)
|
121
|
-
origin = "https://#{@service_domain}.microcms.io"
|
122
|
-
path_with_id = path ? "/api/v1/#{endpoint}/#{path}" : "/api/v1/#{endpoint}"
|
123
|
-
encoded_query =
|
124
|
-
if !query || query.size.zero?
|
125
|
-
''
|
126
|
-
else
|
127
|
-
"?#{URI.encode_www_form(query)}"
|
128
|
-
end
|
129
|
-
|
130
|
-
URI.parse("#{origin}#{path_with_id}#{encoded_query}")
|
131
|
-
end
|
132
|
-
|
133
|
-
def build_http(uri)
|
134
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
135
|
-
http.use_ssl = true
|
172
|
+
private
|
136
173
|
|
137
|
-
|
174
|
+
def parse_body(body)
|
175
|
+
JSON.parse(body)
|
176
|
+
rescue JSON::ParserError
|
177
|
+
{}
|
138
178
|
end
|
139
179
|
end
|
140
180
|
end
|
data/microcms.gemspec
CHANGED
@@ -12,9 +12,11 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.homepage = 'https://github.com/microcmsio/microcms-ruby-sdk'
|
13
13
|
spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
|
14
14
|
|
15
|
-
spec.metadata['homepage_uri']
|
16
|
-
spec.metadata['source_code_uri'] =
|
17
|
-
spec.metadata['changelog_uri']
|
15
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
16
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
17
|
+
spec.metadata['changelog_uri'] = spec.homepage
|
18
|
+
|
19
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
18
20
|
|
19
21
|
# Specify which files should be added to the gem when it is released.
|
20
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: microcms-ruby-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- microCMS
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: microCMS Ruby SDK
|
14
14
|
email:
|
@@ -38,6 +38,7 @@ metadata:
|
|
38
38
|
homepage_uri: https://github.com/microcmsio/microcms-ruby-sdk
|
39
39
|
source_code_uri: https://github.com/microcmsio/microcms-ruby-sdk
|
40
40
|
changelog_uri: https://github.com/microcmsio/microcms-ruby-sdk
|
41
|
+
rubygems_mfa_required: 'true'
|
41
42
|
post_install_message:
|
42
43
|
rdoc_options: []
|
43
44
|
require_paths:
|