lhs 2.0.0 → 2.0.1

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: 911fecf1a1b1e1d5a011b185b4d6ba8fb63f7445
4
- data.tar.gz: 24b82515d4cfe87227d949afe0cd23048962bce7
3
+ metadata.gz: 4429a4da605c0b11565e5e005771dd9be52411a3
4
+ data.tar.gz: 5071611c0b36c7162ad4aac13172dba6d4c86f67
5
5
  SHA512:
6
- metadata.gz: 43a4a6f1605313a18ce9f26e00aafa80e11530d99c2f52e3fab6bf26fd40a4e249af9ab87f854e76a4b335df26940deb27e1a5ad8bbdc39481bb9a0aa80ca46e
7
- data.tar.gz: 66e736a905f730e2216fe676051f16436437c45e7ca1f393c0169e13204d511b3f44f3d8c2b66ef66f5fbd9024cf7046fd13f0dbd7920213538667ee13948bd4
6
+ metadata.gz: 3c52dcb465ba7b0a881811fc3d71eb83140c5057872aa30c3c83004cce75e392c9bd47abffa201d8201e3116aa6dfa359c05e890a2d1b66c144b61787de45eb2
7
+ data.tar.gz: 7ff5aa130cfb6fffcf07a8651b07b6ea82bf48ff8927f1a9015c5ededed930dcf07a6e8372a035f339e549da6eca8551fa92bbfabf95825f483d905abfbc940a
@@ -5,24 +5,24 @@ require File.join(__dir__, 'proxy.rb')
5
5
  class LHS::Collection < LHS::Proxy
6
6
 
7
7
  def total
8
- _data._raw['total']
8
+ _data._raw[:total]
9
9
  end
10
10
 
11
11
  def limit
12
- _data._raw['limit']
12
+ _data._raw[:limit]
13
13
  end
14
14
 
15
15
  def offset
16
- _data._raw['offset']
16
+ _data._raw[:offset]
17
17
  end
18
18
 
19
19
  def href
20
- _data._raw['href']
20
+ _data._raw[:href]
21
21
  end
22
22
 
23
23
  def _collection
24
24
  raw = _data._raw if _data._raw.is_a?(Array)
25
- raw ||= _data._raw['items']
25
+ raw ||= _data._raw[:items]
26
26
  Collection.new(raw, _data, _data._service)
27
27
  end
28
28
 
@@ -9,11 +9,12 @@ class LHS::Item < LHS::Proxy
9
9
  self.errors = nil
10
10
  fail 'No validation endpoint found!' unless validation_endpoint
11
11
  service = LHS::Service.for_url(validation_endpoint.url)
12
+ params = validation_endpoint.options.fetch(:params, {}).merge(persist: false)
12
13
  begin
13
14
  service.request(
14
15
  url: validation_endpoint.url,
15
16
  method: :post,
16
- params: validation_endpoint.options.fetch(:params, {}).merge(persist: false),
17
+ params: params,
17
18
  body: _data.to_json,
18
19
  headers: {'Content-Type' => 'application/json'}
19
20
  )
@@ -16,14 +16,14 @@ class LHS::Service
16
16
  all = []
17
17
  default_max_limit = 100
18
18
  data = instance.request(params: params.merge(limit: default_max_limit))
19
- all.concat(data._raw['items'])
20
- total_left = data._raw['total'] - data.count
21
- limit = data._raw['limit'] || data.count
19
+ all.concat(data._raw[:items])
20
+ total_left = data._raw[:total] - data.count
21
+ limit = data._raw[:limit] || data.count
22
22
  if limit > 0
23
23
  requests = total_left / limit
24
24
  requests.times do |i|
25
25
  offset = limit * (i+1) + 1
26
- all.concat instance.request(params: params.merge(limit: limit, offset: offset))._raw['items']
26
+ all.concat instance.request(params: params.merge(limit: limit, offset: offset))._raw[:items]
27
27
  end
28
28
  end
29
29
  LHS::Data.new(all, nil, self)
@@ -25,8 +25,8 @@ class LHS::Service
25
25
  params = options[:params] || {}
26
26
  loop do # as suggested by Matz
27
27
  data = instance.request(params: params.merge(limit: batch_size, offset: start))
28
- batch_size = data._raw['limit']
29
- left = data._raw['total'].to_i - data._raw['offset'].to_i - data._raw['limit'].to_i
28
+ batch_size = data._raw[:limit]
29
+ left = data._raw[:total].to_i - data._raw[:offset].to_i - data._raw[:limit].to_i
30
30
  yield data
31
31
  break if left <= 0
32
32
  start += batch_size
@@ -38,10 +38,10 @@ class LHS::Service
38
38
  if data._proxy.is_a? LHS::Collection
39
39
  data.each_with_index do |item, i|
40
40
  item = item[i] if item.is_a? LHS::Collection
41
- item._raw[key.to_s].merge!(addition[i]._raw)
41
+ item._raw[key.to_sym].merge!(addition[i]._raw)
42
42
  end
43
43
  elsif data._proxy.is_a? LHS::Item
44
- data._raw[key.to_s].merge!(addition._raw)
44
+ data._raw[key.to_sym].merge!(addition._raw)
45
45
  end
46
46
  end
47
47
 
@@ -108,6 +108,7 @@ class LHS::Service
108
108
  def process_options(options)
109
109
  options ||= {}
110
110
  options = options.dup
111
+ options[:params].deep_symbolize_keys! if options[:params]
111
112
  endpoint = find_endpoint(options[:params])
112
113
  options = (endpoint.options || {}).merge(options)
113
114
  options[:url] = compute_url!(options[:params]) unless options.key?(:url)
@@ -33,6 +33,12 @@ class LHS::Data
33
33
  _root._service
34
34
  end
35
35
 
36
+ # enforce internal data structure to have deep symbolized keys
37
+ def _raw=(raw)
38
+ raw.to_hash.deep_symbolize_keys! if raw && raw.respond_to?(:to_hash)
39
+ @_raw = raw
40
+ end
41
+
36
42
  protected
37
43
 
38
44
  # Use existing mapping to provide data
@@ -53,13 +59,12 @@ class LHS::Data
53
59
  private
54
60
 
55
61
  def collection_proxy?(input)
56
- (_raw.is_a?(Hash) && _raw['items']) ||
57
- input.is_a?(Array) || _raw.is_a?(Array)
62
+ !! (input.is_a?(Hash) && input[:items]) || input.is_a?(Array) || _raw.is_a?(Array)
58
63
  end
59
64
 
60
65
  def mapping_for(name)
61
- service_instance = LHS::Service.for_url(_raw['href']) if _raw.is_a?(Hash)
62
- service_instance ||= _root._service.instance if root_item?
66
+ service_instance = LHS::Service.for_url(_raw[:href]) if _raw.is_a?(Hash)
67
+ service_instance ||= _root._service.instance if root_item? && _root._service
63
68
  return unless service_instance
64
69
  service_instance.mapping[name]
65
70
  end
@@ -89,7 +94,7 @@ class LHS::Data
89
94
  def proxy_from_input(input)
90
95
  if input.is_a? LHS::Proxy
91
96
  input
92
- elsif collection_proxy?(input)
97
+ elsif collection_proxy?(raw_from_input(input))
93
98
  LHS::Collection.new(self)
94
99
  else
95
100
  LHS::Item.new(self)
@@ -98,12 +103,13 @@ class LHS::Data
98
103
 
99
104
  def raw_from_input(input)
100
105
  if input.is_a?(String) && input.length > 0
101
- JSON.parse(input)
106
+ JSON.parse(input).deep_symbolize_keys
102
107
  elsif defined?(input._raw)
103
108
  input._raw
104
109
  elsif defined?(input._data)
105
110
  input._data._raw
106
111
  else
112
+ input.deep_symbolize_keys! if input.is_a?(Hash)
107
113
  input
108
114
  end
109
115
  end
@@ -63,7 +63,7 @@ class LHS::Item < LHS::Proxy
63
63
 
64
64
  def set(name, value)
65
65
  key = name.to_s.gsub(/=$/, '')
66
- _data._raw[key] = value
66
+ _data._raw[key.to_sym] = value
67
67
  end
68
68
 
69
69
  private
@@ -23,4 +23,5 @@ class LHS::Proxy
23
23
  self._loaded = true
24
24
  self
25
25
  end
26
+
26
27
  end
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.1"
3
3
  end
@@ -0,0 +1,31 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHS::Item do
4
+
5
+ before(:each) do
6
+ class Feedback < LHS::Service
7
+ endpoint ':datastore/v2/feedbacks'
8
+ endpoint ':datastore/v2/feedbacks/:id'
9
+ end
10
+ end
11
+
12
+ let(:hash) do
13
+ {'addresses' => [{'businesses' => {'identities' => [{'name' => 'Löwenzorn'}]}}]}
14
+ end
15
+
16
+ let(:data) do
17
+ LHS::Data.new(hash, nil, Feedback)
18
+ end
19
+
20
+ it 'deep symbolizes keys internaly when new data is initalized' do
21
+ expect(data._raw[:addresses].first[:businesses][:identities].first[:name]).to eq 'Löwenzorn'
22
+ data.id = 'YZ12'
23
+ expect(data._raw.keys).to include(:id)
24
+ end
25
+
26
+ it 'deep symbolizes internal data' do
27
+ feedback = Feedback.build(hash)
28
+ expect(feedback._raw.keys).to include(:addresses)
29
+ expect(feedback._raw[:addresses].first[:businesses][:identities].first[:name]).to eq 'Löwenzorn'
30
+ end
31
+ end
@@ -26,7 +26,7 @@ describe LHS::Item do
26
26
  it 'sets the value for an existing attribute' do
27
27
  expect(item.name = 'Steve').to eq 'Steve'
28
28
  expect(item.name).to eq 'Steve'
29
- expect(item._raw['name']).to eq 'Steve'
29
+ expect(item._raw[:name]).to eq 'Steve'
30
30
  end
31
31
 
32
32
  it 'sets things to nil' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - local.ch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-08 00:00:00.000000000 Z
11
+ date: 2015-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lhc
@@ -215,6 +215,7 @@ files:
215
215
  - spec/item/destroy_spec.rb
216
216
  - spec/item/errors_spec.rb
217
217
  - spec/item/getter_spec.rb
218
+ - spec/item/internal_data_structure_spec.rb
218
219
  - spec/item/respond_to_spec.rb
219
220
  - spec/item/save_spec.rb
220
221
  - spec/item/setter_spec.rb
@@ -327,6 +328,7 @@ test_files:
327
328
  - spec/item/destroy_spec.rb
328
329
  - spec/item/errors_spec.rb
329
330
  - spec/item/getter_spec.rb
331
+ - spec/item/internal_data_structure_spec.rb
330
332
  - spec/item/respond_to_spec.rb
331
333
  - spec/item/save_spec.rb
332
334
  - spec/item/setter_spec.rb