miasma 0.2.4 → 0.2.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: d32bced5ab7a65f5cec7aae1a717904e71dc8a94
4
- data.tar.gz: 53581473453536b82a3afd67a9bd754f1ee43446
3
+ metadata.gz: aebc0393d265e3f5dfa814404af11e3668e2bb82
4
+ data.tar.gz: d72c1b1bd1d3727a0693c9e81c50d14ac502bd0d
5
5
  SHA512:
6
- metadata.gz: 4b02c183a44ba32b24e20f72260f6a89ed84c88efab319955326fc0fef4a0543fea459262ceec9ac32da60a05a52dbd7f508270bb557dda8e59ebb0e33f77651
7
- data.tar.gz: 6b72a033e7ef5293dd764c92da47abe2faaaf1024359d88b1781d2682e541de304015d7c62948f621195c82c90f24f8f6e265b6db6295cc152920520bfc5d288
6
+ metadata.gz: b09ca540d448dfbb34d93abfc431bf96165dc44e237dd96e7eb3f6a2899c54ca5c537edba9be292e9f34c5e05dc725d024055de4e5bcff172dea33cffb0f8afd
7
+ data.tar.gz: 00b056a517c7dfb837c2c68874ed343ca3c9c25604d7a75d7a03101105a4dbfbd665891c84dfa46162a389ce18dafc3c5b72c78c167cff15d43dae3fbb41f122
@@ -1,3 +1,8 @@
1
+ # v0.2.6
2
+ * Add default filter implementation on collection
3
+ * Allow disable of automatic body extraction
4
+ * Always return streamish object store file body
5
+
1
6
  # v0.2.4
2
7
  * Make `Storage::File#body` setup provider responsibility
3
8
  * Fix module usage within RS to properly squash existing methods
@@ -128,6 +128,33 @@ module Miasma
128
128
  end
129
129
  end
130
130
 
131
+ # Return filtered files
132
+ #
133
+ # @param args [Hash] filter options
134
+ # @return [Array<Models::Storage::File>]
135
+ def file_filter(bucket, args)
136
+ if(args[:prefix])
137
+ result = request(
138
+ :path => '/',
139
+ :endpoint => bucket_endpoint(bucket),
140
+ :params => Smash.new(
141
+ :prefix => args[:prefix]
142
+ )
143
+ )
144
+ [result.get(:body, 'ListBucketResult', 'Contents')].flatten.compact.map do |file|
145
+ File.new(
146
+ bucket,
147
+ :id => ::File.join(bucket.name, file['Key']),
148
+ :name => file['Key'],
149
+ :updated => file['LastModified'],
150
+ :size => file['Size'].to_i
151
+ ).valid_state
152
+ end
153
+ else
154
+ bucket_all
155
+ end
156
+ end
157
+
131
158
  # Return all files within bucket
132
159
  #
133
160
  # @param bucket [Bucket]
@@ -323,14 +350,17 @@ module Miasma
323
350
  if(file.persisted?)
324
351
  result = request(
325
352
  :path => file_path(file),
326
- :endpoint => bucket_endpoint(file.bucket)
353
+ :endpoint => bucket_endpoint(file.bucket),
354
+ :disable_body_extraction => true
327
355
  )
328
356
  content = result[:body]
329
357
  begin
330
358
  if(content.is_a?(String))
331
359
  StringIO.new(content)
332
360
  else
333
- content.stream!
361
+ if(content.respond_to?(:stream!))
362
+ content.stream!
363
+ end
334
364
  content
335
365
  end
336
366
  rescue HTTP::StateError
@@ -46,12 +46,11 @@ module Miasma
46
46
  begin
47
47
  begin
48
48
  content = MultiJson.load(response.body.to_s).to_smash
49
- msgs = content.values.map do |arg|
50
- arg[:message]
51
- end.compact
52
- unless(msgs.empty?)
53
- @response_error_msg = msgs.join(' - ')
54
- end
49
+ @response_error_msg = [[:error, :message]].map do |path|
50
+ if(result = content.get(*path))
51
+ "#{content[:code]}: #{result}"
52
+ end
53
+ end.flatten.compact.first
55
54
  rescue MultiJson::ParseError
56
55
  begin
57
56
  content = MultiXml.parse(response.body.to_s).to_smash
@@ -8,15 +8,6 @@ module Miasma
8
8
  # Abstract bucket collection
9
9
  class Buckets < Types::Collection
10
10
 
11
- # Return buckets matching given filter
12
- #
13
- # @param options [Hash] filter options
14
- # @return [Array<Bucket>]
15
- # @option options [String] :prefix key prefix
16
- def filter(options={})
17
- raise NotImplementedError
18
- end
19
-
20
11
  # @return [Bucket] new unsaved instance
21
12
  def build(args={})
22
13
  Bucket.new(api, args.to_smash)
@@ -25,7 +25,7 @@ module Miasma
25
25
  # @return [Array<File>]
26
26
  # @option options [String] :prefix key prefix
27
27
  def filter(options={})
28
- raise NotImplementedError
28
+ super
29
29
  end
30
30
 
31
31
  # @return [File] new unsaved instance
@@ -50,6 +50,11 @@ module Miasma
50
50
  api.file_all(bucket)
51
51
  end
52
52
 
53
+ # @return [Array<File>]
54
+ def perform_filter(args)
55
+ api.file_filter(bucket, args)
56
+ end
57
+
53
58
  end
54
59
 
55
60
  end
@@ -66,6 +66,7 @@ module Miasma
66
66
  # @option args [String, Symbol] :method HTTP request method
67
67
  # @option args [String] :path request path
68
68
  # @option args [Integer] :expects expected response status code
69
+ # @option args [TrueClass, FalseClass] :disable_body_extraction do not auto-parse response body
69
70
  # @return [Smash] {:result => HTTP::Response, :headers => Smash, :body => Object}
70
71
  # @raises [Error::ApiError::RequestError]
71
72
  def request(args)
@@ -96,7 +97,7 @@ module Miasma
96
97
  unless(result.code == args.fetch(:expects, 200).to_i)
97
98
  raise Error::ApiError::RequestError.new(result.reason, :response => result)
98
99
  end
99
- format_response(result)
100
+ format_response(result, !args[:disable_body_extraction])
100
101
  end
101
102
 
102
103
  # Perform request
@@ -115,22 +116,26 @@ module Miasma
115
116
  # Makes best attempt at formatting response
116
117
  #
117
118
  # @param result [HTTP::Response]
119
+ # @param extract_body [TrueClass, FalseClass] automatically extract body
118
120
  # @return [Smash]
119
- def format_response(result)
121
+ def format_response(result, extract_body=true)
120
122
  extracted_headers = Smash[result.headers.map{|k,v| [Utils.snake(k), v]}]
121
- if(extracted_headers[:content_type].to_s.include?('json'))
122
- begin
123
- extracted_body = MultiJson.load(result.body.to_s).to_smash
124
- rescue MultiJson::ParseError
125
- extracted_body = result.body.to_s
126
- end
127
- elsif(extracted_headers[:content_type].to_s.include?('xml'))
128
- begin
129
- extracted_body = MultiXml.parse(result.body.to_s).to_smash
130
- rescue MultiXml::ParseError
131
- extracted_body = result.body.to_s
123
+ if(extract_body)
124
+ if(extracted_headers[:content_type].to_s.include?('json'))
125
+ begin
126
+ extracted_body = MultiJson.load(result.body.to_s).to_smash
127
+ rescue MultiJson::ParseError
128
+ extracted_body = result.body.to_s
129
+ end
130
+ elsif(extracted_headers[:content_type].to_s.include?('xml'))
131
+ begin
132
+ extracted_body = MultiXml.parse(result.body.to_s).to_smash
133
+ rescue MultiXml::ParseError
134
+ extracted_body = result.body.to_s
135
+ end
132
136
  end
133
- else
137
+ end
138
+ unless(extracted_body)
134
139
  # @note if body is over 100KB, do not extract
135
140
  if(extracted_headers[:content_length].to_i < 102400)
136
141
  extracted_body = result.body.to_s
@@ -48,8 +48,10 @@ module Miasma
48
48
  # @todo need to add helper to deep sort args, convert to string
49
49
  # and hash to use as memoization key
50
50
  def filter(args={})
51
- memoize(args.to_s)
52
- raise NotImplementedError
51
+ key = "filter_#{args.to_smash.checksum}"
52
+ memoize(key) do
53
+ perform_filter(args)
54
+ end
53
55
  end
54
56
 
55
57
  # Build a new model
@@ -111,6 +113,17 @@ module Miasma
111
113
  raise NotImplementedError
112
114
  end
113
115
 
116
+ # @return [Array<Model>]
117
+ def perform_filter(args)
118
+ if(args[:prefix])
119
+ all.find_all do |item|
120
+ item.name.start_with?(args[:prefix])
121
+ end
122
+ else
123
+ all
124
+ end
125
+ end
126
+
114
127
  end
115
128
  end
116
129
  end
@@ -1,4 +1,4 @@
1
1
  module Miasma
2
2
  # current library version
3
- VERSION = Gem::Version.new('0.2.4')
3
+ VERSION = Gem::Version.new('0.2.6')
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miasma
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Roberts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-25 00:00:00.000000000 Z
11
+ date: 2014-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie