contenttruck-rb 1.0.0 → 1.0.1

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: 86c1b2b13c1dbfe1aef7cc2d1cd7c4dc6c7fa57b55b19b94d99d098555a6e473
4
- data.tar.gz: 834aea2e6ca0d766fcdbe0f447694af07f757920391ac576cf87b1eb5b03b7ff
3
+ metadata.gz: 10f91fed1dcb74097e86b9ce6385ed9b1ff60ae8664aca186721a656f9fcdb0e
4
+ data.tar.gz: 53a2eca6ffadd08571dab8929577e1a7259594405b97da5df5517bf9b6bb0dfb
5
5
  SHA512:
6
- metadata.gz: 17f2624823545a636be54aa9789cd4770d73c10ee59c788e4265ce456c3cdadb0012b840ad07c3a092e62dbc614ab8a66726ae4cd80553bbd7230c0c174d7755
7
- data.tar.gz: a7bb450068fd878991099b8a40ae4c093a2d0fd712b20b070c01eed0bba6e80cbb8e9fa04acebb30b751c074b243dbbbdf496864c57be64e9f736bb26982b639
6
+ metadata.gz: a24a64be8a258857e60925639dd0dd9419498df75a240d54efa0c3f636e280a58986122ae38269e42ec0c13e79c746e2081f278918a65e269408b12a4350f455
7
+ data.tar.gz: abd7d4cc77ef0fc3eac98fbc81c6a3bb092344635f0ae789e7e7f380fa9551006c8e319b8e127c9ca1cbbf31b25ef77b4d64f77fa5f62279b8387d1c97045a8c
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ contenttruck-rb (1.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (13.0.6)
10
+
11
+ DEPENDENCIES
12
+ contenttruck-rb!
13
+ rake (~> 13.0)
14
+
15
+ BUNDLED WITH
16
+ 2.4.8
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+ require 'net/http'
5
+ require 'json'
6
+
7
+ module Contenttruck
8
+ class UserError < StandardError
9
+ def initialize(code, message)
10
+ super(message)
11
+ @code = code
12
+ end
13
+
14
+ def code
15
+ @code
16
+ end
17
+ end
18
+
19
+ class Client
20
+ def initialize(base_url)
21
+ raise ArgumentError, 'base_url must be a String' unless base_url.is_a?(String)
22
+ @rpc_url = URI.parse(base_url)
23
+
24
+ # Set the path of the RPC URL to /_contenttruck.
25
+ @rpc_url.path = '/_contenttruck'
26
+ end
27
+
28
+ def upload(key, partition, relative_path, content_type, content_or_reader)
29
+ raise ArgumentError, 'key must be a String' unless key.is_a?(String)
30
+ raise ArgumentError, 'partition must be a String' unless partition.is_a?(String)
31
+ raise ArgumentError, 'relative_path must be a String' unless relative_path.is_a?(String)
32
+ raise ArgumentError, 'content_type must be a String' unless content_type.is_a?(String)
33
+ unless content_or_reader.is_a?(String) || content_or_reader.respond_to?(:read)
34
+ raise ArgumentError, 'content_or_reader must be a String or reader'
35
+ end
36
+
37
+ _do_rpc_request('Upload', {
38
+ 'key' => key,
39
+ 'partition' => partition,
40
+ 'relative_path' => relative_path,
41
+ }, content_type, content_or_reader)
42
+ end
43
+
44
+ def delete(key, partition, relative_path)
45
+ raise ArgumentError, 'key must be a String' unless key.is_a?(String)
46
+ raise ArgumentError, 'partition must be a String' unless partition.is_a?(String)
47
+ raise ArgumentError, 'relative_path must be a String' unless relative_path.is_a?(String)
48
+
49
+ _do_rpc_request('Delete', {
50
+ 'key' => key,
51
+ 'partition' => partition,
52
+ 'relative_path' => relative_path,
53
+ })
54
+ end
55
+
56
+ def create_key(sudo_key, partitions)
57
+ raise ArgumentError, 'sudo_key must be a String' unless sudo_key.is_a?(String)
58
+ raise ArgumentError, 'partitions must be an Array' unless partitions.is_a?(Array)
59
+ partitions.each do |partition|
60
+ raise ArgumentError, 'partitions must be an Array of Strings' unless partition.is_a?(String)
61
+ end
62
+
63
+ _do_rpc_request('CreateKey', {
64
+ 'sudo_key' => sudo_key,
65
+ 'partitions' => partitions,
66
+ })
67
+ end
68
+
69
+ def delete_key(sudo_key, key)
70
+ raise ArgumentError, 'sudo_key must be a String' unless sudo_key.is_a?(String)
71
+ raise ArgumentError, 'key must be a String' unless key.is_a?(String)
72
+
73
+ _do_rpc_request('DeleteKey', {
74
+ 'sudo_key' => sudo_key,
75
+ 'key' => key,
76
+ })
77
+ end
78
+
79
+ def create_partition(sudo_key, name, rule_set)
80
+ raise ArgumentError, 'sudo_key must be a String' unless sudo_key.is_a?(String)
81
+ raise ArgumentError, 'name must be a String' unless name.is_a?(String)
82
+ raise ArgumentError, 'rule_set must be a String' unless rule_set.is_a?(String)
83
+
84
+ _do_rpc_request('CreatePartition', {
85
+ 'sudo_key' => sudo_key,
86
+ 'name' => name,
87
+ 'rule_set' => rule_set,
88
+ })
89
+ end
90
+
91
+ def delete_partition(sudo_key, name)
92
+ raise ArgumentError, 'sudo_key must be a String' unless sudo_key.is_a?(String)
93
+ raise ArgumentError, 'name must be a String' unless name.is_a?(String)
94
+
95
+ _do_rpc_request('DeletePartition', {
96
+ 'sudo_key' => sudo_key,
97
+ 'name' => name,
98
+ })
99
+ end
100
+
101
+ private
102
+
103
+ def _to_json(value)
104
+ # Basically works around the fact that some people might not have ActiveSupport loaded.
105
+ return value.to_json if value.respond_to?(:to_json)
106
+ JSON.generate(value)
107
+ end
108
+
109
+ def _get_user_error(response)
110
+ begin
111
+ content = JSON.parse(response.body)
112
+ return UserError.new(content['code'], content['message']) if content['code'] && content['message']
113
+ rescue; end
114
+ end
115
+
116
+ def _do_rpc_request(type, body, content_type = nil, content_reader = nil)
117
+ http = Net::HTTP.new(@rpc_url.host, @rpc_url.port)
118
+ http.use_ssl = true if @rpc_url.scheme == 'https'
119
+
120
+ request = Net::HTTP::Post.new(@rpc_url)
121
+ request['X-Type'] = type
122
+ if content_type
123
+ # Include the JSON in a header and send the body.
124
+ request['X-Json-Body'] = _to_json(body)
125
+ request['Content-Type'] = content_type
126
+ content_length = 0
127
+ if content_reader.instance_of?(String)
128
+ request.body = content_reader
129
+ content_length = content_reader.bytesize
130
+ else
131
+ request.body_stream = content_reader
132
+ content_length = content_reader.size
133
+ end
134
+ request['Content-Length'] = content_length.to_s
135
+ else
136
+ # Send the body as JSON.
137
+ request['Content-Type'] = 'application/json'
138
+ json = _to_json(body)
139
+ request['Content-Length'] = json.bytesize.to_s
140
+ request.body = json
141
+ end
142
+
143
+ response = http.request(request)
144
+
145
+ # Handle ok responses.
146
+ return nil if response.code == '204'
147
+ return JSON.parse(response.body) if response.code == '200'
148
+
149
+ # Handle errors.
150
+ x = _get_user_error(response)
151
+ raise x if x
152
+ raise StandardError, "Unexpected response code #{response.code} (body: #{response.body})"
153
+ end
154
+ end
155
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Contenttruck
4
- VERSION = "1.0.0".freeze
4
+ VERSION = "1.0.1".freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contenttruck-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jake Gealer
@@ -18,11 +18,13 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - Gemfile
21
+ - Gemfile.lock
21
22
  - LICENSE.txt
22
23
  - README.md
23
24
  - Rakefile
24
25
  - contenttruck.gemspec
25
26
  - lib/contenttruck.rb
27
+ - lib/contenttruck/client.rb
26
28
  - lib/contenttruck/version.rb
27
29
  - sig/contenttruck.rbs
28
30
  homepage: https://github.com/webscalesoftwareltd/contenttruck-rb