microcms-ruby-sdk 1.1.0 → 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/lib/microcms/version.rb +1 -1
- data/lib/microcms.rb +63 -53
- metadata +2 -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/lib/microcms/version.rb
CHANGED
data/lib/microcms.rb
CHANGED
@@ -20,8 +20,67 @@ 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
|
@@ -68,6 +127,7 @@ module MicroCMS
|
|
68
127
|
|
69
128
|
private
|
70
129
|
|
130
|
+
# rubocop:disable Style/MethodLength
|
71
131
|
def build_query(option)
|
72
132
|
{
|
73
133
|
draftKey: option[:draftKey],
|
@@ -77,9 +137,11 @@ module MicroCMS
|
|
77
137
|
q: option[:q],
|
78
138
|
fields: option[:fields] ? option[:fields].join(',') : nil,
|
79
139
|
filters: option[:filters],
|
80
|
-
depth: option[:depth]
|
140
|
+
depth: option[:depth],
|
141
|
+
ids: option[:ids] ? option[:ids].join(',') : nil
|
81
142
|
}.select { |_key, value| value }
|
82
143
|
end
|
144
|
+
# rubocop:enable Style/MethodLength
|
83
145
|
|
84
146
|
def put(endpoint, content, option = {})
|
85
147
|
body = content.reject { |key, _value| key == :id }
|
@@ -89,58 +151,6 @@ module MicroCMS
|
|
89
151
|
def post(endpoint, content, option = {})
|
90
152
|
send_http_request('POST', endpoint, nil, option, content)
|
91
153
|
end
|
92
|
-
|
93
|
-
def send_http_request(method, endpoint, path, query = nil, body = nil)
|
94
|
-
uri = build_uri(endpoint, path, query)
|
95
|
-
http = build_http(uri)
|
96
|
-
req = build_request(method, uri, body)
|
97
|
-
res = http.request(req)
|
98
|
-
|
99
|
-
raise APIError.new(status_code: res.code.to_i, body: res.body) if res.code.to_i >= 400
|
100
|
-
|
101
|
-
JSON.parse(res.body, object_class: OpenStruct) if res.header['Content-Type'].include?('application/json') # rubocop:disable Style/OpenStructUse
|
102
|
-
end
|
103
|
-
|
104
|
-
def get_request_class(method)
|
105
|
-
{
|
106
|
-
GET: Net::HTTP::Get,
|
107
|
-
POST: Net::HTTP::Post,
|
108
|
-
PUT: Net::HTTP::Put,
|
109
|
-
PATCH: Net::HTTP::Patch,
|
110
|
-
DELETE: Net::HTTP::Delete
|
111
|
-
}[method]
|
112
|
-
end
|
113
|
-
|
114
|
-
def build_request(method, uri, body)
|
115
|
-
req = get_request_class(method.to_sym).new(uri.request_uri)
|
116
|
-
req['X-MICROCMS-API-KEY'] = @api_key
|
117
|
-
if body
|
118
|
-
req['Content-Type'] = 'application/json'
|
119
|
-
req.body = JSON.dump(body)
|
120
|
-
end
|
121
|
-
|
122
|
-
req
|
123
|
-
end
|
124
|
-
|
125
|
-
def build_uri(endpoint, path, query)
|
126
|
-
origin = "https://#{@service_domain}.microcms.io"
|
127
|
-
path_with_id = path ? "/api/v1/#{endpoint}/#{path}" : "/api/v1/#{endpoint}"
|
128
|
-
encoded_query =
|
129
|
-
if !query || query.size.zero?
|
130
|
-
''
|
131
|
-
else
|
132
|
-
"?#{URI.encode_www_form(query)}"
|
133
|
-
end
|
134
|
-
|
135
|
-
URI.parse("#{origin}#{path_with_id}#{encoded_query}")
|
136
|
-
end
|
137
|
-
|
138
|
-
def build_http(uri)
|
139
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
140
|
-
http.use_ssl = true
|
141
|
-
|
142
|
-
http
|
143
|
-
end
|
144
154
|
end
|
145
155
|
|
146
156
|
# APIError
|
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.
|
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: 2022-
|
11
|
+
date: 2022-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: microCMS Ruby SDK
|
14
14
|
email:
|