cmis-ruby 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dc97717108219ab5cccfdc7ebbcebdae3e49459f
4
- data.tar.gz: 7f6f2d04fdf02fdd4e903024f90cf208a3b0fbc0
3
+ metadata.gz: 02a90b75f0be84cc302c4a1009af31ba8c5bda9b
4
+ data.tar.gz: 4c4fbdf04f4c5dee685f77a1ecfe43af08616431
5
5
  SHA512:
6
- metadata.gz: b85d9befb1c62a5f7077040094c7100d4dd14d1f09712d831b5e8f2362f46186923e03b236c7bfb3330c5c2795e26a17b73cfb34faa97b55da60d8756d6fb07b
7
- data.tar.gz: 8f82470e491ee50316dc856f5ef454749a5118ae542991f89ddbdee16e3b4726681ff892c3016f3b099e44e3c271d7c5b49e77a0d49e3b1b703fe720d07851b5
6
+ metadata.gz: d21dacc515544df7721021adc675a1a7492ec56d3067e0a9a7220b205274dbdbb06212cb2028b926867deef899aa8373c3112be0071ab2e1e46973633070aa21
7
+ data.tar.gz: 5b2ed9b877541bf0fb23f97ca15fa8f7bafa776eb803dab54c2d025fc30984023479f0c0a3b47c2b49c6cf51f452d35b56ed17cc3d3ed0170e4f95c2c86b50f2
data/cmis-ruby.gemspec CHANGED
@@ -16,9 +16,8 @@ Gem::Specification.new do |s|
16
16
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
17
  s.require_paths = ['lib']
18
18
 
19
- s.add_dependency 'typhoeus', '~> 0.6'
20
- s.add_dependency 'multipart-post', '~> 1.1'
21
- s.add_dependency 'activesupport', '>= 3.2'
19
+ s.add_dependency 'faraday', '~> 0.9.0'
20
+ s.add_dependency 'activesupport', '>= 3.2', '< 5.0'
22
21
 
23
22
  s.description = <<-DESC.gsub(/^ /, '')
24
23
  CMIS browser binding client library in ruby.
data/lib/cmis-ruby.rb CHANGED
@@ -1,16 +1,11 @@
1
- # ruby
2
1
  require 'bigdecimal'
3
2
  require 'json'
4
3
 
5
- # core extensions
6
4
  require 'active_support/core_ext'
7
5
  require 'core_ext'
8
6
 
9
- # HTTP
10
- require 'typhoeus'
11
- require 'net/http/post/multipart'
7
+ require 'faraday'
12
8
 
13
- # cmis-ruby
14
9
  require 'cmis/connection'
15
10
  require 'cmis/exceptions'
16
11
  require 'cmis/server'
@@ -29,3 +24,4 @@ require 'cmis/policy'
29
24
  require 'cmis/relationship'
30
25
  require 'cmis/property_definition'
31
26
  require 'cmis/type'
27
+ require 'cmis/version'
@@ -1,189 +1,145 @@
1
1
  module CMIS
2
2
  class Connection
3
- def initialize(service_url, username, password, headers)
4
- @service_url = service_url
5
- @username = username
6
- @password = password
7
- @headers = headers
3
+ def initialize(options)
4
+ options.symbolize_keys!
5
+
6
+ service_url = options[:service_url] || ENV['CMIS_BROWSER_URL']
7
+ @service_url = service_url or raise "\
8
+ option `:service_url` or ENV['CMIS_BROWSER_URL'] must be set"
9
+
10
+ adapter = (options[:adapter] || :net_http).to_sym
11
+
12
+ headers = {
13
+ user_agent: "cmis-ruby/#{VERSION} [#{adapter}]"
14
+ }.merge(options[:headers] || {})
15
+ conn_opts = { headers: headers, ssl: options[:ssl] }.compact
16
+
17
+ @http = Faraday.new(conn_opts) do |builder|
18
+ builder.use RequestModifier
19
+ builder.request :multipart
20
+ builder.request :url_encoded
21
+
22
+ if username = options[:username] || ENV['CMIS_USER']
23
+ password = options[:password] || ENV['CMIS_PASSWORD']
24
+ builder.basic_auth(username, password)
25
+ end
26
+
27
+ builder.adapter adapter
28
+ builder.response :logger if options[:log_requests]
29
+ builder.use ResponseParser
30
+ end
8
31
 
9
32
  @url_cache = {}
10
33
  end
11
34
 
12
35
  def execute!(params = {}, options = {})
13
- query, headers = parse_options(options)
14
-
15
- Request.new(service_url: @service_url,
16
- url_cache: @url_cache,
17
- params: params,
18
- query: query,
19
- headers: headers,
20
- username: @username,
21
- password: @password).run
22
- end
23
-
24
- private
25
-
26
- def parse_options(options)
27
36
  options.symbolize_keys!
28
- query = options[:query] || {}
29
- headers = @headers
30
- headers.merge!(options[:headers]) if options[:headers]
31
- [ query, headers ]
32
- end
33
37
 
34
- class Request
35
- def initialize(options)
36
- @service_url = options[:service_url]
37
- @url_cache = options[:url_cache]
38
- @params = massage(options[:params])
39
- @repository_id = @params.delete(:repositoryId)
40
- @query = options[:query]
41
- @headers = options[:headers]
42
- @username = options[:username]
43
- @password = options[:password]
44
- end
38
+ query = options[:query] || {}
39
+ headers = options[:headers]|| {}
40
+ url = url(params.delete(:repositoryId), params[:objectId])
45
41
 
46
- def run
47
- case method
48
- when 'get'
49
- typhoeus_request
50
- when 'post'
51
- typhoeus_request
52
- when 'multipart_post'
53
- multipart_post
54
- end
42
+ response = if params[:cmisaction]
43
+ @http.post(url, params, headers)
44
+ else
45
+ @http.get(url, params.merge(query), headers)
55
46
  end
56
47
 
57
- private
48
+ response.body
49
+ end
58
50
 
59
- def typhoeus_request
60
- options = {
61
- method: method,
62
- body: body,
63
- params: query,
64
- headers: @headers,
65
- followlocation: true
66
- }
67
- options[:userpwd] = "#{@username}:#{@password}" if @username
68
- response = Typhoeus::Request.new(url, options).run
69
- Response.new(response.headers['Content-Type'], response.body).parse!
70
- end
51
+ private
71
52
 
72
- def multipart_post
73
- uri = URI.parse(url)
74
- req = Net::HTTP::Post::Multipart.new(uri.path, body)
75
- @headers.each { |key, value| req[key] = value }
76
- req.basic_auth @username, @password if @username
77
- opts = if uri.scheme == 'https'
78
- { use_ssl: true , verify_mode: OpenSSL::SSL::VERIFY_NONE }
79
- else
80
- {}
81
- end
82
- Net::HTTP.start(uri.host, uri.port, opts) do |http|
83
- response = http.request(req)
84
- Response.new(response['Content-Type'], response.body).parse!
85
- end
53
+ def url(repository_id, object_id)
54
+ if repository_id.nil?
55
+ @service_url
56
+ else
57
+ urls = repository_urls(repository_id)
58
+ urls[object_id ? :root_folder_url : :repository_url]
86
59
  end
60
+ end
87
61
 
88
- def url
89
- if @repository_id.nil?
90
- @service_url
91
- else
92
- urls = repository_urls(@repository_id)
93
- if @params[:objectId]
94
- urls[:root_folder_url]
95
- else
96
- urls[:repository_url]
97
- end
98
- end
99
- end
62
+ def repository_urls(repository_id)
63
+ if @url_cache[repository_id].nil?
64
+ repository_infos = @http.get(@service_url).body
100
65
 
101
- def method
102
- if @params[:cmisaction]
103
- if @params[:content]
104
- 'multipart_post'
105
- else
106
- 'post'
107
- end
108
- else
109
- 'get'
66
+ unless repository_infos.has_key?(repository_id)
67
+ raise Exceptions::ObjectNotFound, "repositoryId: #{repository_id}"
110
68
  end
111
- end
112
69
 
113
- def body
114
- @params if @params[:cmisaction]
70
+ repository_info = repository_infos[repository_id]
71
+ @url_cache[repository_id] = {
72
+ repository_url: repository_info['repositoryUrl'],
73
+ root_folder_url: repository_info['rootFolderUrl']
74
+ }
115
75
  end
76
+ @url_cache[repository_id]
77
+ end
78
+ end
116
79
 
117
- def query
118
- if @params[:cmisaction]
119
- @query
120
- else
121
- @params.merge(@query)
122
- end
80
+ class RequestModifier < Faraday::Middleware
81
+ def call(env)
82
+ if env[:body]
83
+ env[:body].compact!
84
+ wrap_content(env)
85
+ massage_properties(env)
123
86
  end
87
+ @app.call(env)
88
+ end
124
89
 
125
- # TODO: Extract functionality
126
- def massage(hash)
127
- hash.compact
90
+ private
128
91
 
129
- if content_hash = hash[:content]
130
- hash[:content] = UploadIO.new(content_hash[:stream],
131
- content_hash[:mime_type],
132
- content_hash[:filename])
133
- end
92
+ def wrap_content(env)
93
+ if content_hash = env[:body][:content]
94
+ env[:body][:content] = Faraday::UploadIO.new(content_hash[:stream],
95
+ content_hash[:mime_type],
96
+ content_hash[:filename])
97
+ end
98
+ end
134
99
 
135
- if props = hash.delete(:properties)
136
- props.each_with_index do |(id, value), index|
137
- value = value.to_time if value.is_a?(Date) or value.is_a?(DateTime)
138
- value = (value.to_f * 1000).to_i if value.is_a?(Time)
139
- if value.is_a?(Array)
140
- hash.merge!("propertyId[#{index}]" => id)
141
- value.each_with_index do |v, idx|
142
- hash.merge!("propertyValue[#{index}][#{idx}]" => value[idx])
143
- end
144
- else
145
- hash.merge!("propertyId[#{index}]" => id,
146
- "propertyValue[#{index}]" => value)
100
+ def massage_properties(env)
101
+ if props = env[:body].delete(:properties)
102
+ props.each_with_index do |(id, value), index|
103
+ if value.is_a?(Array)
104
+ env[:body][id_key(index)] = id
105
+ value.each_with_index do |sub_value, sub_index|
106
+ env[:body][val_key(index, sub_index)] = normalize(sub_value)
147
107
  end
108
+ else
109
+ env[:body][id_key(index)] = id
110
+ env[:body][val_key(index)] = normalize(value)
148
111
  end
149
112
  end
150
- hash
151
113
  end
114
+ end
152
115
 
153
- def repository_urls(repository_id)
154
- if @url_cache[repository_id].nil?
155
-
156
- options = { method: 'get' }
157
- options[:userpwd] = "#{@username}:#{@password}" if @username
158
- response = Typhoeus::Request.new(@service_url, options).run
159
- repository_infos = JSON.parse(response.body)
160
-
161
- unless repository_infos.has_key?(repository_id)
162
- raise Exceptions::ObjectNotFound, "repositoryId: #{repository_id}"
163
- end
164
-
165
- repository_info = repository_infos[repository_id]
166
- @url_cache[repository_id] = { repository_url: repository_info['repositoryUrl'],
167
- root_folder_url: repository_info['rootFolderUrl'] }
168
- end
169
- @url_cache[repository_id]
170
- end
116
+ def normalize(value)
117
+ value = value.to_time if value.is_a?(Date)
118
+ value = (value.to_f * 1000).to_i if value.is_a?(Time)
119
+ value
171
120
  end
172
121
 
173
- class Response
174
- def initialize(content_type, body)
175
- @content_type = content_type
176
- @body = body
177
- end
122
+ def id_key(index)
123
+ "propertyId[#{index}]"
124
+ end
178
125
 
179
- def parse!
180
- return @body unless @content_type =~ /application\/json/
126
+ def val_key(i1, i2 = nil)
127
+ result = "propertyValue[#{i1}]"
128
+ result = "#{result}[#{i2}]" if i2
129
+ result
130
+ end
131
+ end
181
132
 
182
- result = JSON.parse(@body)
183
- if result.is_a?(Hash) && ex = result['exception']
184
- raise "CMIS::Exceptions::#{ex.camelize}".constantize, result['message']
133
+ class ResponseParser < Faraday::Middleware
134
+ def call(env)
135
+ @app.call(env).on_complete do |env|
136
+ if env[:response_headers][:content_type] =~ /\/(x-)?json(;.+?)?$/
137
+ env[:body] = JSON.parse(env[:body]).with_indifferent_access
138
+ if env[:body].is_a?(Hash) && ex = env[:body][:exception]
139
+ ruby_exception = "CMIS::Exceptions::#{ex.camelize}".constantize
140
+ raise ruby_exception, env[:body]['message']
141
+ end
185
142
  end
186
- result.with_indifferent_access
187
143
  end
188
144
  end
189
145
  end
data/lib/cmis/server.rb CHANGED
@@ -3,16 +3,7 @@ module CMIS
3
3
  attr_reader :connection
4
4
 
5
5
  def initialize(options = {})
6
- options.stringify_keys!
7
-
8
- service_url = options['service_url'] || ENV['CMIS_BROWSER_URL']
9
- username = options['username'] || ENV['CMIS_USER']
10
- password = options['password'] || ENV['CMIS_PASSWORD']
11
- headers = options['headers'] || {}
12
-
13
- raise "`service_url` must be set" unless service_url
14
-
15
- @connection = Connection.new(service_url, username, password, headers)
6
+ @connection = Connection.new(options)
16
7
  end
17
8
 
18
9
  def repositories(opts = {})
data/lib/cmis/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CMIS
2
- VERSION = '0.3.5'
2
+ VERSION = '0.3.6'
3
3
  end
data/readme.md CHANGED
@@ -10,7 +10,6 @@ Running the tests requires a running CMIS server.
10
10
 
11
11
  ## TODO
12
12
 
13
- * use Faraday for HTTP
14
13
  * facilitate copy between servers (make a flow)
15
14
  * caching
16
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmis-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenneth Geerts
@@ -9,36 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-04 00:00:00.000000000 Z
12
+ date: 2014-03-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: typhoeus
15
+ name: faraday
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '0.6'
20
+ version: 0.9.0
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '0.6'
28
- - !ruby/object:Gem::Dependency
29
- name: multipart-post
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - "~>"
33
- - !ruby/object:Gem::Version
34
- version: '1.1'
35
- type: :runtime
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - "~>"
40
- - !ruby/object:Gem::Version
41
- version: '1.1'
27
+ version: 0.9.0
42
28
  - !ruby/object:Gem::Dependency
43
29
  name: activesupport
44
30
  requirement: !ruby/object:Gem::Requirement
@@ -46,6 +32,9 @@ dependencies:
46
32
  - - ">="
47
33
  - !ruby/object:Gem::Version
48
34
  version: '3.2'
35
+ - - "<"
36
+ - !ruby/object:Gem::Version
37
+ version: '5.0'
49
38
  type: :runtime
50
39
  prerelease: false
51
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -53,6 +42,9 @@ dependencies:
53
42
  - - ">="
54
43
  - !ruby/object:Gem::Version
55
44
  version: '3.2'
45
+ - - "<"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
56
48
  description: |
57
49
  CMIS browser binding client library in ruby.
58
50
  email: