duracloud-client 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE +201 -0
- data/README.md +85 -0
- data/Rakefile +2 -0
- data/duracloud.gemspec +26 -0
- data/lib/duracloud-client.rb +1 -0
- data/lib/duracloud.rb +9 -0
- data/lib/duracloud/client.rb +47 -0
- data/lib/duracloud/configuration.rb +31 -0
- data/lib/duracloud/connection.rb +28 -0
- data/lib/duracloud/content.rb +127 -0
- data/lib/duracloud/content_properties.rb +117 -0
- data/lib/duracloud/content_request.rb +16 -0
- data/lib/duracloud/content_response.rb +11 -0
- data/lib/duracloud/error.rb +7 -0
- data/lib/duracloud/error_handler.rb +58 -0
- data/lib/duracloud/request.rb +39 -0
- data/lib/duracloud/request_options.rb +25 -0
- data/lib/duracloud/response.rb +57 -0
- data/lib/duracloud/version.rb +3 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c6733dd180dfccf317acf1938b3f95265ee321a0
|
4
|
+
data.tar.gz: a61cb47c8d1411efc8d031c1dea604d651b79a49
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fb3b785bbbcc959e96fa2b13a550688375e1f1dff06cf44d10bc8e4f6b1aa5b9f23c41066402e2b67bb16f804cee2b20e6b427f32d0d1b6bf6b2016af3a10f2a
|
7
|
+
data.tar.gz: 628e6c8c041862732675419e04bb0c404b5014097ba819c23f59dec17f3d208412e33e734edb657774d9853557e89aa4e59c7455e9fd11610d73860305e72836
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# duracloud-ruby-client
|
2
|
+
Ruby client for communicating with DuraCloud
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'duracloud'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install duracloud
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
### Configure the client
|
23
|
+
|
24
|
+
Option 1. Environment variables
|
25
|
+
|
26
|
+
DURACLOUD_HOST
|
27
|
+
DURACLOUD_PORT
|
28
|
+
DURACLOUD_USER
|
29
|
+
DURACLOUD_PASSWORD
|
30
|
+
|
31
|
+
Option 2. Manual configuration
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Duracloud::Client.configure do |config|
|
35
|
+
config.host = "foo.duracloud.org"
|
36
|
+
config.user = "bob@example.com"
|
37
|
+
config.password = "s3cret"
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
```
|
42
|
+
> c = Duracloud::Client.new
|
43
|
+
=> #<Duracloud::Client:0x007fe953a1c630 @config=#<Duracloud::Configuration host="foo.duracloud.org", port=nil, user="bob@example.com", password="******">>
|
44
|
+
```
|
45
|
+
|
46
|
+
### Create a new content item and store it in DuraCloud
|
47
|
+
|
48
|
+
If a relative URL is given (`:url` keyword option, or a combination of `:space_id` and `:content_id` options), the fully-qualified URL is built in the standard way from the base URL `https://{host}:{port}/durastore/`.
|
49
|
+
|
50
|
+
```
|
51
|
+
> new_content = Duracloud::Content.new(space_id: "rest-api-testing", content_id: "ark:/99999/fk4zzzz")
|
52
|
+
=> #<Duracloud::Content url="rest-api-testing/ark:/99999/fk4zzzz">
|
53
|
+
> new_content.body = "test"
|
54
|
+
=> "test"
|
55
|
+
> new_content.content_type = "text/plain"
|
56
|
+
=> "text/plain"
|
57
|
+
> new_content.store
|
58
|
+
=> #<Duracloud::Content url="rest-api-testing/ark:/99999/fk4zzzz">
|
59
|
+
```
|
60
|
+
|
61
|
+
### Retrieve an existing content item from DuraCloud
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
Duracloud::Content.find(**options) # :url, or :space_id and :content_id
|
65
|
+
```
|
66
|
+
|
67
|
+
### Update the properties for an item
|
68
|
+
|
69
|
+
TODO
|
70
|
+
|
71
|
+
### Delete a content item
|
72
|
+
|
73
|
+
TODO
|
74
|
+
|
75
|
+
## Versioning
|
76
|
+
|
77
|
+
We endeavor to follow semantic versioning. In particular, versions < 1.0 may introduce backward-incompatible changes without notice. Use at your own risk. Version 1.0 signals a stable API.
|
78
|
+
|
79
|
+
## Maintainers
|
80
|
+
|
81
|
+
* David Chandek-Stark (Duke University)
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
TODO
|
data/Rakefile
ADDED
data/duracloud.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'duracloud/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "duracloud-client"
|
8
|
+
spec.version = Duracloud::VERSION
|
9
|
+
spec.authors = ["David Chandek-Stark"]
|
10
|
+
spec.email = ["dchandekstark@gmail.com"]
|
11
|
+
spec.summary = "Ruby client for communicating with DuraCloud"
|
12
|
+
spec.description = "Ruby client for communicating with DuraCloud"
|
13
|
+
spec.homepage = "https://github.com/duracloud/duracloud-ruby-client"
|
14
|
+
spec.license = "APACHE2"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "hashie", "~> 3.4"
|
22
|
+
spec.add_dependency "httpclient", "~> 2.7"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "duracloud"
|
data/lib/duracloud.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "forwardable"
|
2
|
+
require_relative "configuration"
|
3
|
+
require_relative "connection"
|
4
|
+
require_relative "content_request"
|
5
|
+
|
6
|
+
module Duracloud
|
7
|
+
class Client
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def self.configure
|
11
|
+
yield Configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :config
|
15
|
+
|
16
|
+
delegate [:host, :port, :user, :password, :base_url] => :config
|
17
|
+
|
18
|
+
def initialize(**options)
|
19
|
+
@config = Configuration.new(**options)
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_content(url, **options)
|
23
|
+
execute ContentRequest, :get, url, **options
|
24
|
+
#ContentRequest.get(self, url, **options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_content_properties(url, **options)
|
28
|
+
execute ContentRequest, :head, url, **options
|
29
|
+
end
|
30
|
+
|
31
|
+
def set_content_properties(url, **options)
|
32
|
+
execute ContentRequest, :post, url, **options
|
33
|
+
end
|
34
|
+
|
35
|
+
def store_content(url, **options)
|
36
|
+
execute ContentRequest, :put, url, **options
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete_content(url, **options)
|
40
|
+
execute ContentRequest, :delete, url, **options
|
41
|
+
end
|
42
|
+
|
43
|
+
def execute(request_class, http_method, url, **options)
|
44
|
+
request_class.new(self, http_method, url, **options).execute
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Duracloud
|
2
|
+
class Configuration
|
3
|
+
class << self
|
4
|
+
attr_accessor :host, :port, :user, :password
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_reader :host, :port, :user, :password
|
8
|
+
|
9
|
+
def initialize(host: nil, port: nil, user: nil, password: nil)
|
10
|
+
@host = host || default(:host)
|
11
|
+
@port = port || default(:port)
|
12
|
+
@user = user || default(:user)
|
13
|
+
@password = password || default(:password)
|
14
|
+
freeze
|
15
|
+
end
|
16
|
+
|
17
|
+
def base_url
|
18
|
+
URI::HTTPS.build(host: host, port: port)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def default(attr)
|
24
|
+
self.class.send(attr) || ENV["DURACLOUD_#{attr.to_s.upcase}"]
|
25
|
+
end
|
26
|
+
|
27
|
+
def inspect
|
28
|
+
"#<#{self.class} host=#{host.inspect}, port=#{port.inspect}, user=#{user.inspect}, password=\"******\">"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'httpclient'
|
2
|
+
|
3
|
+
module Duracloud
|
4
|
+
#
|
5
|
+
# An HTTP connection to DuraCloud.
|
6
|
+
#
|
7
|
+
# @note We are using HTTPClient because Net::HTTP capitalizes
|
8
|
+
# request header names which is incompatible with DuraCloud's
|
9
|
+
# custom case-sensitive content property headers (x-dura-meta-*).
|
10
|
+
#
|
11
|
+
class Connection < HTTPClient
|
12
|
+
# class << self
|
13
|
+
# attr_accessor :base_path
|
14
|
+
# end
|
15
|
+
|
16
|
+
# self.base_path = '/'
|
17
|
+
|
18
|
+
def initialize(client, base_path = '/')
|
19
|
+
base_url = client.base_url + base_path
|
20
|
+
super(base_url: base_url, force_basic_auth: true)
|
21
|
+
set_auth(client.base_url, client.user, client.password)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# class DurastoreConnection < Connection
|
26
|
+
# self.base_path = '/durastore/'
|
27
|
+
# end
|
28
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'forwardable'
|
3
|
+
require_relative 'content_properties'
|
4
|
+
|
5
|
+
module Duracloud
|
6
|
+
#
|
7
|
+
# A piece of content in DuraCloud
|
8
|
+
#
|
9
|
+
class Content
|
10
|
+
extend Forwardable
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def from_response(response)
|
14
|
+
new(url: response.url) { |c| c.load_from_response(response) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def find(**options)
|
18
|
+
new(**options).get
|
19
|
+
end
|
20
|
+
|
21
|
+
def create(**options)
|
22
|
+
new(**options).store
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
attr_reader :url
|
27
|
+
attr_accessor :md5, :content_type, :body
|
28
|
+
|
29
|
+
delegate :empty? => :body
|
30
|
+
|
31
|
+
alias_method :checksum, :md5
|
32
|
+
|
33
|
+
def initialize(**options)
|
34
|
+
url = if options[:url]
|
35
|
+
options[:url]
|
36
|
+
else
|
37
|
+
unless options[:space_id] && options[:content_id]
|
38
|
+
raise Error, "Content requires either :url OR both :space_id AND :content_id."
|
39
|
+
end
|
40
|
+
options.values_at(:space_id, :content_id).join('/')
|
41
|
+
end
|
42
|
+
@url = URI(url).path # might not be exactly what we want, but it works
|
43
|
+
@body = options[:body] || options[:payload]
|
44
|
+
@md5 = options[:md5]
|
45
|
+
@content_type = options[:content_type]
|
46
|
+
self.properties = options[:properties].to_h
|
47
|
+
yield self if block_given?
|
48
|
+
end
|
49
|
+
|
50
|
+
def inspect
|
51
|
+
"#<#{self.class} url=#{url.inspect}>"
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_s
|
55
|
+
body
|
56
|
+
end
|
57
|
+
|
58
|
+
def get
|
59
|
+
response = client.get_content(url)
|
60
|
+
load_from_response(response)
|
61
|
+
self
|
62
|
+
end
|
63
|
+
alias_method :reload, :get
|
64
|
+
|
65
|
+
def get_properties
|
66
|
+
response = client.get_content_properties(url)
|
67
|
+
load_properties_from_response(response)
|
68
|
+
self
|
69
|
+
end
|
70
|
+
alias_method :reload_properties, :get_properties
|
71
|
+
|
72
|
+
def set_properties
|
73
|
+
self.content_type = "text/plain" unless content_type
|
74
|
+
client.set_content_properties(url, properties: properties)
|
75
|
+
self
|
76
|
+
end
|
77
|
+
|
78
|
+
def delete
|
79
|
+
client.delete_content(url)
|
80
|
+
reset
|
81
|
+
freeze
|
82
|
+
self
|
83
|
+
end
|
84
|
+
|
85
|
+
def store
|
86
|
+
raise Error, "Refusing to store empty content file!" unless body
|
87
|
+
self.md5 = Digest::MD5.hexdigest(body) unless md5
|
88
|
+
unless content_type
|
89
|
+
self.content_type = body ? "application/octet-stream" : "text/plain"
|
90
|
+
end
|
91
|
+
options = {
|
92
|
+
payload: body, md5: md5, content_type: content_type, properties: properties
|
93
|
+
}
|
94
|
+
client.store_content(url, **options)
|
95
|
+
reload_properties
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
def load_from_response(response)
|
100
|
+
self.body = response.body
|
101
|
+
self.md5 = response.md5
|
102
|
+
self.content_type = response.content_type
|
103
|
+
load_properties_from_response(response)
|
104
|
+
end
|
105
|
+
|
106
|
+
def load_properties_from_response(response)
|
107
|
+
self.properties = response.headers.select { |h, v| ContentProperties.property?(h) }
|
108
|
+
end
|
109
|
+
|
110
|
+
def properties
|
111
|
+
@properties ||= ContentProperties.new
|
112
|
+
end
|
113
|
+
|
114
|
+
def properties=(props)
|
115
|
+
properties.replace(props)
|
116
|
+
end
|
117
|
+
|
118
|
+
def client
|
119
|
+
@client ||= Client.new
|
120
|
+
end
|
121
|
+
|
122
|
+
def reset
|
123
|
+
properties.clear
|
124
|
+
@body, @md5, @content_type = nil, nil, nil
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
|
3
|
+
module Duracloud
|
4
|
+
#
|
5
|
+
# [See https://groups.google.com/d/msg/duracloud-users/67ONTkAqyCM/SGVOOHTvAAAJ]
|
6
|
+
#
|
7
|
+
# These are used as part of the REST call to perform a copy
|
8
|
+
#
|
9
|
+
# x-dura-meta-copy-source
|
10
|
+
# x-dura-meta-copy-source-store
|
11
|
+
#
|
12
|
+
# These are used to provide details about a space.
|
13
|
+
# You could probably set them on a content item, but that would be a little odd:
|
14
|
+
#
|
15
|
+
# x-dura-meta-space-count
|
16
|
+
# x-dura-meta-space-created
|
17
|
+
#
|
18
|
+
# These are use used as part of the REST call to set space access control
|
19
|
+
# (the * is replaced by the user or group name)
|
20
|
+
#
|
21
|
+
# x-dura-meta-acl-*
|
22
|
+
# x-dura-meta-acl-group-*
|
23
|
+
#
|
24
|
+
# These are used for some internal DuraCloud data wrangling,
|
25
|
+
# I'd recommend that you not use them in your code:
|
26
|
+
#
|
27
|
+
# x-dura-meta-content-mimetype
|
28
|
+
# x-dura-meta-content-size
|
29
|
+
# x-dura-meta-content-checksum
|
30
|
+
# x-dura-meta-content-modified
|
31
|
+
#
|
32
|
+
# That's it for the exceptions.
|
33
|
+
#
|
34
|
+
# One other set I'll call out is the values which are captured by the DuraCloud SyncTool.
|
35
|
+
# These are added automatically by the SyncTool, so you may have an interest in reading
|
36
|
+
# or updating them. Either way, it's good to be aware of them so you don't accidentally
|
37
|
+
# step on the values being captured for you.
|
38
|
+
#
|
39
|
+
# x-dura-meta-creator
|
40
|
+
# x-dura-meta-content-file-created
|
41
|
+
# x-dura-meta-content-file-modified
|
42
|
+
# x-dura-meta-content-file-last-accessed
|
43
|
+
# x-dura-meta-content-file-path
|
44
|
+
#
|
45
|
+
# A few other notes:
|
46
|
+
#
|
47
|
+
# Stick with US-ASCII characters for property names and values
|
48
|
+
# There is a 2 KB total size limit on all user-metadata (this includes the metadata
|
49
|
+
# we create for you and that you add yourself)
|
50
|
+
#
|
51
|
+
# Both of these restrictions are put in place by Amazon S3, so we don't have much say
|
52
|
+
# in the matter.
|
53
|
+
#
|
54
|
+
class ContentProperties < Hashie::Mash
|
55
|
+
include Hashie::Extensions::Coercion
|
56
|
+
|
57
|
+
PREFIX = "x-dura-meta-".freeze
|
58
|
+
|
59
|
+
ENCODING = Encoding::US_ASCII
|
60
|
+
|
61
|
+
RESERVED = [
|
62
|
+
/\Ax-dura-meta-acl-/,
|
63
|
+
/\Ax-dura-meta-space-(count|created)\z/,
|
64
|
+
/\Ax-dura-meta-copy-source(-store)\z/,
|
65
|
+
/\Ax-dura-meta-content-(mimetype|size|checksum|modified)\z/,
|
66
|
+
]
|
67
|
+
|
68
|
+
coerce_value Array, ->(v) { v.first }
|
69
|
+
|
70
|
+
class << self
|
71
|
+
attr_accessor :ignore_reserved
|
72
|
+
|
73
|
+
def property?(prop)
|
74
|
+
prop.start_with?(PREFIX)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
self.ignore_reserved = true
|
79
|
+
|
80
|
+
def ignore_reserved?
|
81
|
+
self.class.ignore_reserved
|
82
|
+
end
|
83
|
+
|
84
|
+
def regular_writer(key, value)
|
85
|
+
if reserved?(key)
|
86
|
+
if ignore_reserved?
|
87
|
+
warn "#{self.class}: Ignoring reserved content property \"#{key}\"."
|
88
|
+
else
|
89
|
+
raise Error, "#{self.class}: The content property \"#{key}\" is reserved."
|
90
|
+
end
|
91
|
+
else
|
92
|
+
super
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def convert_key(key)
|
97
|
+
converted = super.dup
|
98
|
+
converted.gsub!(/_/, '-')
|
99
|
+
converted.downcase!
|
100
|
+
converted.prepend(PREFIX) unless self.class.property?(converted)
|
101
|
+
converted.force_encoding(ENCODING)
|
102
|
+
end
|
103
|
+
|
104
|
+
def reserved?(key)
|
105
|
+
RESERVED.any? { |k| k.match(key) }
|
106
|
+
end
|
107
|
+
|
108
|
+
def convert_value(value, _ = nil)
|
109
|
+
if value.is_a?(Array)
|
110
|
+
value.map { |v| convert_value(v) }
|
111
|
+
else
|
112
|
+
value.force_encoding(ENCODING)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "digest"
|
2
|
+
require_relative "request"
|
3
|
+
require_relative "content_response"
|
4
|
+
require_relative "connection"
|
5
|
+
|
6
|
+
module Duracloud
|
7
|
+
class ContentRequest < Request
|
8
|
+
def base_path
|
9
|
+
'/durastore/'
|
10
|
+
end
|
11
|
+
|
12
|
+
def response_class
|
13
|
+
ContentResponse
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Duracloud
|
2
|
+
class ErrorHandler
|
3
|
+
def self.call(response)
|
4
|
+
new(response).call
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_reader :response
|
8
|
+
|
9
|
+
def initialize(response)
|
10
|
+
@response = response # XXX dup?
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
message = response_has_error_message? ? response.body : status_message
|
15
|
+
raise handle_status, message
|
16
|
+
end
|
17
|
+
|
18
|
+
def status_message
|
19
|
+
[response.status, response.reason].join(' ')
|
20
|
+
end
|
21
|
+
|
22
|
+
def server_error?
|
23
|
+
response.status >= 500
|
24
|
+
end
|
25
|
+
|
26
|
+
def handle_status
|
27
|
+
send("handle_#{response.status}")
|
28
|
+
rescue NoMethodError
|
29
|
+
server_error? ? handle_server_error : handle_default
|
30
|
+
end
|
31
|
+
|
32
|
+
def handle_server_error
|
33
|
+
ServerError
|
34
|
+
end
|
35
|
+
|
36
|
+
def handle_default
|
37
|
+
Error
|
38
|
+
end
|
39
|
+
|
40
|
+
def handle_404
|
41
|
+
NotFoundError
|
42
|
+
end
|
43
|
+
|
44
|
+
def response_has_error_message?
|
45
|
+
response.plain_text? && response.has_body?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# class StoreContentErrorHandler < ErrorHandler
|
50
|
+
# def handle_400
|
51
|
+
# InvalidContentIDError
|
52
|
+
# end
|
53
|
+
|
54
|
+
# def handle_409
|
55
|
+
# ChecksumError
|
56
|
+
# end
|
57
|
+
# end
|
58
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative "connection"
|
2
|
+
require_relative "response"
|
3
|
+
require_relative "request_options"
|
4
|
+
|
5
|
+
module Duracloud
|
6
|
+
class Request
|
7
|
+
attr_reader :client, :url, :http_method, :options
|
8
|
+
|
9
|
+
def initialize(client, http_method, url, **options)
|
10
|
+
@client = client
|
11
|
+
@http_method = http_method
|
12
|
+
@url = url
|
13
|
+
@options = RequestOptions.new(**options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def execute
|
17
|
+
original_response = connection.send(http_method,
|
18
|
+
url,
|
19
|
+
body: options.payload,
|
20
|
+
query: options.query,
|
21
|
+
header: options.headers)
|
22
|
+
response_class.new(original_response)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def base_path
|
28
|
+
'/'
|
29
|
+
end
|
30
|
+
|
31
|
+
def response_class
|
32
|
+
Response
|
33
|
+
end
|
34
|
+
|
35
|
+
def connection
|
36
|
+
@connection ||= Connection.new(client, base_path)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "digest"
|
2
|
+
|
3
|
+
module Duracloud
|
4
|
+
class RequestOptions
|
5
|
+
|
6
|
+
attr_reader :payload, :content_type, :md5, :properties, :query
|
7
|
+
|
8
|
+
def initialize(**options)
|
9
|
+
@payload = options.delete(:payload)
|
10
|
+
@content_type = options.delete(:content_type)
|
11
|
+
@md5 = options.delete(:md5)
|
12
|
+
@properties = options.delete(:properties)
|
13
|
+
@query = options.delete(:query) { |k| Hash.new }.merge(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def headers
|
17
|
+
Hash.new.tap do |h|
|
18
|
+
h["Content-MD5"] = md5 if md5
|
19
|
+
h["Content-Type"] = content_type if content_type
|
20
|
+
h.update(properties) if properties
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "forwardable"
|
2
|
+
require_relative "error_handler"
|
3
|
+
|
4
|
+
module Duracloud
|
5
|
+
class Response
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
# class << self
|
9
|
+
# def error_handler
|
10
|
+
# @error_handler ||=
|
11
|
+
# begin
|
12
|
+
# class_name = self.name.split(/::/).last.sub(/Response\z/, "ErrorHandler")
|
13
|
+
# Duracloud.const_get(class_name)
|
14
|
+
# rescue NameError
|
15
|
+
# superclass.error_handler
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
|
20
|
+
attr_reader :original_response
|
21
|
+
|
22
|
+
delegate [:header, :body, :code, :ok?, :redirect?, :status, :reason] => :original_response,
|
23
|
+
:content_type => :header,
|
24
|
+
[:size, :empty?] => :body
|
25
|
+
|
26
|
+
def_delegator :header, :request_uri, :url
|
27
|
+
|
28
|
+
def initialize(original_response)
|
29
|
+
@original_response = original_response
|
30
|
+
#self.class.error_handler.call(self) if error?
|
31
|
+
ErrorHandler.call(self) if error?
|
32
|
+
end
|
33
|
+
|
34
|
+
def error?
|
35
|
+
!(ok? || redirect?)
|
36
|
+
end
|
37
|
+
|
38
|
+
def plain_text?
|
39
|
+
content_type == "text/plain"
|
40
|
+
end
|
41
|
+
|
42
|
+
def has_body?
|
43
|
+
!empty?
|
44
|
+
end
|
45
|
+
|
46
|
+
def headers
|
47
|
+
header.all.each_with_object({}) do |(name, value), memo|
|
48
|
+
memo[name] ||= []
|
49
|
+
memo[name] << value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def md5
|
54
|
+
header["content-md5"].first
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: duracloud-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Chandek-Stark
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hashie
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: httpclient
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.7'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Ruby client for communicating with DuraCloud
|
70
|
+
email:
|
71
|
+
- dchandekstark@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- duracloud.gemspec
|
82
|
+
- lib/duracloud-client.rb
|
83
|
+
- lib/duracloud.rb
|
84
|
+
- lib/duracloud/client.rb
|
85
|
+
- lib/duracloud/configuration.rb
|
86
|
+
- lib/duracloud/connection.rb
|
87
|
+
- lib/duracloud/content.rb
|
88
|
+
- lib/duracloud/content_properties.rb
|
89
|
+
- lib/duracloud/content_request.rb
|
90
|
+
- lib/duracloud/content_response.rb
|
91
|
+
- lib/duracloud/error.rb
|
92
|
+
- lib/duracloud/error_handler.rb
|
93
|
+
- lib/duracloud/request.rb
|
94
|
+
- lib/duracloud/request_options.rb
|
95
|
+
- lib/duracloud/response.rb
|
96
|
+
- lib/duracloud/version.rb
|
97
|
+
homepage: https://github.com/duracloud/duracloud-ruby-client
|
98
|
+
licenses:
|
99
|
+
- APACHE2
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.4.3
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Ruby client for communicating with DuraCloud
|
121
|
+
test_files: []
|