easy-exist 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 737047bef920cbfe0d8eea570262ffb60373384f
4
+ data.tar.gz: 7961d81d7d2acd4040ebc52c3f21e4697d3bad46
5
+ SHA512:
6
+ metadata.gz: 87a2179c43a6c3e4813a76b0411687b36d46581a1ce02ec3369a3f2cbb9cdeb6264eaa6ef25ae931a3964f9e5ea2b1dead020141e13e2936ed9bd6ff9f7da747
7
+ data.tar.gz: f1d0167739ae35fdef4991c9aa4711fafe09104d43cc25a56478edfa6f45354b1cc6dfba4baf4680bcc162fb54beb1a93836999a115fd63fa3c6378691c4e114
@@ -0,0 +1,109 @@
1
+ module EasyExist
2
+
3
+ # Responsible for communicating with the eXist-db server
4
+ class DB
5
+
6
+ # Initializes an EasyExist::DB.
7
+ #
8
+ # @param url [String] the url to the eXist-db server, include protocol.
9
+ # @param opts [Hash] options to initialize the DB with.
10
+ # @option opts :collection [String] The collection for which all GET, PUT and DELETE requests will be relative to.
11
+ # @option opts :username [String] Username for Basic HTTP Authentication.
12
+ # @option opts :password [String] Password for Basic HTTP Authentication.
13
+ # @return [EasyExist::DB] an instance of an EasyExist::DB.
14
+ def initialize(url, opts = {})
15
+ validate_opts(opts)
16
+ @default_opts = { base_uri: url + "/exist/rest/db#{opts[:collection] ||= ''}" }
17
+ if(opts[:username] && opts[:password])
18
+ @default_opts[:basic_auth] = { username: opts[:username], password: opts[:password] }
19
+ end
20
+ end
21
+
22
+ # Retrieves the document at the specified document_uri from the store.
23
+ #
24
+ # @param document_uri [String] the uri of the document to retrieve.
25
+ # relative to the collection specified on initialization otherwise '/db'.
26
+ # @return [String] the contents of the document at 'document_uri'
27
+ def get(document_uri)
28
+ validate_uri(document_uri)
29
+ res = HTTParty.get(document_uri, @default_opts)
30
+ res.success? ? res.body : handle_error(res)
31
+ end
32
+
33
+ # Puts the given document content at the specified document_uri
34
+ #
35
+ # @param document_uri [String] the uri of the document to store.
36
+ # relative to the collection specified on initialization otherwise '/db'.
37
+ # @return [HTTParty::Response] the response object
38
+ def put(document_uri, document)
39
+ validate_uri(document_uri)
40
+ res = HTTParty.put(document_uri, @default_opts.merge({
41
+ body: document,
42
+ headers: { "Content-Type" => "application/xml"},
43
+ }))
44
+ res.success? ? res : handle_error(res)
45
+ end
46
+
47
+ # Deletes the document at the specified document_uri from the store
48
+ #
49
+ # @param document_uri [String] the uri of the document to delete.
50
+ # relative to the collection specified on initialization otherwise '/db'.
51
+ # @return [HTTParty::Response] the response object
52
+ def delete(document_uri)
53
+ validate_uri(document_uri)
54
+ res = HTTParty.delete(document_uri, @default_opts)
55
+ res.success? ? res : handle_error(res)
56
+ end
57
+
58
+ # Determines if the document at the specified document_uri exists in the store
59
+ #
60
+ # @param document_uri [String] the uri of the document to check.
61
+ # relative to the collection specified on initialization otherwise '/db'.
62
+ # @return [TrueClass | FalseClass]
63
+ def exists?(document_uri)
64
+ validate_uri(document_uri)
65
+ HTTParty.get(document_uri, @default_opts).success?
66
+ end
67
+
68
+ # Runs the given XQuery against the store and returns the results
69
+ #
70
+ # @param query [String] XQuery to run against the store
71
+ # @param opts [Hash] options for the query request.
72
+ # @option opts :start [Integer] Index of first item to be returned.
73
+ # @option opts :max [Integer] The maximum number of items to be returned.
74
+ # @return [String] the query results
75
+ def query(query, opts = {})
76
+ body = EasyExist::QueryRequest.new(query, opts).body
77
+ res = HTTParty.post("", @default_opts.merge({
78
+ body: body,
79
+ headers: { 'Content-Type' => 'application/xml', 'Content-Length' => body.length.to_s }
80
+ }))
81
+ res.success? ? res.body : handle_error(res)
82
+ end
83
+
84
+ private
85
+ # Raises an error based on a HTTParty::Response.
86
+ # HTTParty:Response objects contain a reference to the Net::HTTResponse object.
87
+ # Where a request is unsuccessful, the reference can be raised as an exception for clients to rescue
88
+ # and decide on next steps.
89
+ #
90
+ # @param res [HTTParty::Response] the response
91
+ def handle_error(res)
92
+ res.response.value
93
+ end
94
+
95
+ # Raises an error if the given uri does not start with a '/'
96
+ #
97
+ # @param uri [String] the uri to validate
98
+ def validate_uri(uri)
99
+ raise ArgumentError, 'URI must contain preceding "/"' if uri[0] != '/';
100
+ end
101
+
102
+ # Raises an error if any of the given opts are invalid
103
+ #
104
+ # @param opts [Hash] options to validate.
105
+ def validate_opts(opts)
106
+ validate_uri(opts[:collection]) unless opts[:collection].nil? || opts[:collection].empty?
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,45 @@
1
+ module EasyExist
2
+
3
+ # Representation of an eXist-db query request.
4
+ # Responsible for encapsulating the XQuery text ready to POST to server.
5
+ class QueryRequest
6
+
7
+ using EasyExist::RefinedTrue
8
+ using EasyExist::RefinedFalse
9
+ using EasyExist::RefinedObject
10
+
11
+ # Initializes an EasyExist::QueryRequest.
12
+ # Takes the given XQuery text and builds an Nokogiri::XML object around it.
13
+ #
14
+ # @param text [String] the XQuery text
15
+ # @param opts [Hash] options for the query request.
16
+ # @option opts :start [Integer] Index of first item to be returned.
17
+ # @option opts :max [Integer] The maximum number of items to be returned.
18
+ # @option opts :wrap [Boolean] Wrap results in exist:result element.
19
+ # @return [EasyExist::QueryRequest] an instance of an EasyExist::QueryRequest.
20
+ def initialize(text, opts = {})
21
+ @body = Nokogiri::XML::Builder.new do |xml|
22
+ xml.query({ xmlns: EXIST_NAMESPACE }.merge(parse_opts(opts))) {
23
+ xml.text_ text
24
+ }
25
+ end
26
+ end
27
+
28
+ # Returns the request body as xml
29
+ #
30
+ # @return [String] the request body
31
+ def body
32
+ @body.to_xml
33
+ end
34
+
35
+ private
36
+ def parse_opts(opts)
37
+ if(opts.key?(:wrap)) then
38
+ raise(ArgumentError, ":wrap must be a TrueClass or FalseClass") unless opts[:wrap].is_a_boolean?
39
+ opts[:wrap] = opts[:wrap].to_yes_no
40
+ end
41
+ opts
42
+ end
43
+ end
44
+
45
+ end
data/lib/easy-exist.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'httparty'
2
+ require 'nokogiri'
3
+ require_relative "./easy-exist/refinements"
4
+ require_relative "./easy-exist/query_request"
5
+ require_relative "./easy-exist/db"
6
+
7
+ # The EasyExist module
8
+ module EasyExist
9
+
10
+ # Defines the eXist-db xml namespace
11
+ EXIST_NAMESPACE = "http://exist.sourceforge.net/NS/exist"
12
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy-exist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Cass
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.13.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.13'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.13.3
33
+ - !ruby/object:Gem::Dependency
34
+ name: nokogiri
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.6'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.6.6.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.6'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.6.6.2
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '3.1'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 3.1.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.1'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.1.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: rack-test
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '0.6'
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.6.3
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.6'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 0.6.3
93
+ - !ruby/object:Gem::Dependency
94
+ name: shoulda-matchers
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '2.7'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 2.7.0
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '2.7'
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 2.7.0
113
+ - !ruby/object:Gem::Dependency
114
+ name: pry-debugger
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '0.2'
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 0.2.3
123
+ type: :development
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '0.2'
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 0.2.3
133
+ description: An easy to use gem to interact with eXist-db via its REST API.
134
+ email: easy.exist.gem@gmail.com
135
+ executables: []
136
+ extensions: []
137
+ extra_rdoc_files: []
138
+ files:
139
+ - lib/easy-exist.rb
140
+ - lib/easy-exist/db.rb
141
+ - lib/easy-exist/query_request.rb
142
+ homepage: http://rubygems.org/gems/easy-exist
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.2.2
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: eXist-db made easy!
166
+ test_files: []
167
+ has_rdoc: