query-interface-client 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38358ae4407fb6eaec45f3618356ecc0a4ce6036
4
- data.tar.gz: 779c341a7401922afed15e0cc34506b6e52781fa
3
+ metadata.gz: 058935aef6e41bdd7e467dd59ec3f367b98d93fd
4
+ data.tar.gz: 30ecc627ea8f2b63a1afa8bd084bf921039e2bd3
5
5
  SHA512:
6
- metadata.gz: ea1de7c1e67b563d48baaac1c66fa82d1d800186d766a83423e1ac85a7e957283bcf08a6b94db6cb417e5205cad0238ae77da8aae38065c4ce5f7de2df5d1a10
7
- data.tar.gz: 2fa98440d3d0d96486b36b54c517f5cc35755bbf3b00efabd1f8ec6bd72072a11f4859268d8d3b45f3e9176b36a6c4ccc7dc461603af0ae72c883d96f9313486
6
+ metadata.gz: 50a8b92a71878b2a277ff1a3248750e72359c391abab5201b680c679a63a9e16015fa66b23f858435337a0406ec3668c6b75ed1206cb91531acd7a730a47e5b3
7
+ data.tar.gz: d14949f8023b16b781fc69a312406c129741021ad977247dba4e0008c67608fed5cec585df0a9b9d6d2d7379fd37021445bc452924e1d1d37453558920e0f342
data/Gemfile CHANGED
@@ -1,9 +1,8 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
- gem 'her'
4
- gem 'json'
5
3
  gem 'rs_paginator'
6
4
  gem 'activesupport', require: false
5
+ gem 'faraday'
7
6
 
8
7
  group :development do
9
8
  gem 'rake'
data/Gemfile.lock CHANGED
@@ -1,49 +1,52 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activemodel (4.0.0)
5
- activesupport (= 4.0.0)
6
- builder (~> 3.1.0)
7
- activesupport (4.0.0)
8
- i18n (~> 0.6, >= 0.6.4)
9
- minitest (~> 4.2)
10
- multi_json (~> 1.3)
4
+ actionview (4.1.6)
5
+ activesupport (= 4.1.6)
6
+ builder (~> 3.1)
7
+ erubis (~> 2.7.0)
8
+ activesupport (4.1.6)
9
+ i18n (~> 0.6, >= 0.6.9)
10
+ json (~> 1.7, >= 1.7.7)
11
+ minitest (~> 5.1)
11
12
  thread_safe (~> 0.1)
12
- tzinfo (~> 0.3.37)
13
- atomic (1.1.10)
13
+ tzinfo (~> 1.1)
14
14
  autotest-standalone (4.5.11)
15
- builder (3.1.4)
16
- diff-lcs (1.2.4)
17
- faraday (0.8.7)
18
- multipart-post (~> 1.1)
19
- her (0.6.7)
20
- activemodel (>= 3.0.0)
21
- activesupport (>= 3.0.0)
22
- faraday (~> 0.8)
23
- multi_json (~> 1.7)
15
+ builder (3.2.2)
16
+ diff-lcs (1.2.5)
17
+ docile (1.1.5)
18
+ erubis (2.7.0)
19
+ faraday (0.9.0)
20
+ multipart-post (>= 1.2, < 3)
24
21
  i18n (0.6.11)
25
22
  json (1.8.1)
26
- minitest (4.7.5)
27
- multi_json (1.7.7)
28
- multipart-post (1.2.0)
29
- rake (10.1.0)
30
- rs_paginator (0.0.1)
23
+ minitest (5.4.2)
24
+ multi_json (1.10.1)
25
+ multipart-post (2.0.0)
26
+ rake (10.3.2)
27
+ rs_paginator (0.1.1)
28
+ actionview
31
29
  i18n
32
- rspec (2.14.0)
33
- rspec-core (~> 2.14.0)
34
- rspec-expectations (~> 2.14.0)
35
- rspec-mocks (~> 2.14.0)
36
- rspec-core (2.14.2)
37
- rspec-expectations (2.14.0)
38
- diff-lcs (>= 1.1.3, < 2.0)
39
- rspec-mocks (2.14.1)
40
- simplecov (0.7.1)
30
+ rspec (3.1.0)
31
+ rspec-core (~> 3.1.0)
32
+ rspec-expectations (~> 3.1.0)
33
+ rspec-mocks (~> 3.1.0)
34
+ rspec-core (3.1.6)
35
+ rspec-support (~> 3.1.0)
36
+ rspec-expectations (3.1.2)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.1.0)
39
+ rspec-mocks (3.1.3)
40
+ rspec-support (~> 3.1.0)
41
+ rspec-support (3.1.2)
42
+ simplecov (0.9.1)
43
+ docile (~> 1.1.0)
41
44
  multi_json (~> 1.0)
42
- simplecov-html (~> 0.7.1)
43
- simplecov-html (0.7.1)
44
- thread_safe (0.1.0)
45
- atomic
46
- tzinfo (0.3.37)
45
+ simplecov-html (~> 0.8.0)
46
+ simplecov-html (0.8.0)
47
+ thread_safe (0.3.4)
48
+ tzinfo (1.2.2)
49
+ thread_safe (~> 0.1)
47
50
 
48
51
  PLATFORMS
49
52
  ruby
@@ -51,8 +54,7 @@ PLATFORMS
51
54
  DEPENDENCIES
52
55
  activesupport
53
56
  autotest-standalone
54
- her
55
- json
57
+ faraday
56
58
  rake
57
59
  rs_paginator
58
60
  rspec
@@ -0,0 +1,48 @@
1
+ require 'faraday'
2
+
3
+ module QueryInterface
4
+ module Client
5
+ class API
6
+ attr_accessor :connection, :url, :options
7
+
8
+ def self.setup(options={}, &faraday_block)
9
+ @default_api = self.new(options)
10
+ @default_api.setup_connection(&faraday_block)
11
+ end
12
+
13
+ def initialize(options)
14
+ if options.include?(:url)
15
+ self.url = options.delete(:url)
16
+ end
17
+ self.options = options
18
+ end
19
+
20
+ def setup_connection(&faraday_block)
21
+ Faraday.new do |connection|
22
+ self.connection = connection
23
+ yield connection if block_given?
24
+ end
25
+ self
26
+ end
27
+
28
+ def request(options={})
29
+ method = options.delete(:_method)
30
+ path = options.delete(:_path)
31
+ headers = options.delete(:_headers)
32
+ response = self.connection.send method do |request|
33
+ request.headers.merge!(headers) if headers
34
+ if method == :get
35
+ # For GET requests, treat additional parameters as querystring data
36
+ request.url path, options
37
+ else
38
+ # For POST, PUT and DELETE requests, treat additional parameters as request body
39
+ request.url path
40
+ request.body = options
41
+ end
42
+ end
43
+ {parsed_data: response.env[:body], response: response}
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,37 @@
1
+ require 'faraday'
2
+
3
+ module QueryInterface
4
+ module Client
5
+ class JsonMiddleware < Faraday::Response::Middleware
6
+
7
+ def parse_json(body)
8
+ body = '{}' if body.blank?
9
+ json = JSON.parse(body, symbolize_names: true)
10
+ unless json.is_a?(Hash) || json.is_a?(Array)
11
+ raise "Response must be Hash or Array, got: #{body.inspect}"
12
+ end
13
+ json
14
+ end
15
+
16
+ def parse(body)
17
+ json = parse_json(body)
18
+ errors = json.delete(:errors) || {}
19
+ metadata = json.delete(:metadata) || {}
20
+ {
21
+ :data => json,
22
+ :errors => errors,
23
+ :metadata => metadata
24
+ }
25
+ end
26
+
27
+ def on_complete(env)
28
+ env[:body] = case env[:status]
29
+ when 204
30
+ parse('{}')
31
+ else
32
+ parse(env[:body])
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -15,10 +15,6 @@ module QueryInterface
15
15
  self.result_model = result_model
16
16
  end
17
17
 
18
- def parse(data)
19
- (self.result_model ? self.result_model.parse(data) : self.model.parse(data))
20
- end
21
-
22
18
  def instantiate(data)
23
19
  (self.result_model ? self.result_model.new(data) : self.model.new(data))
24
20
  end
@@ -128,15 +124,12 @@ module QueryInterface
128
124
  if parsed_data[:data].is_a?(Array)
129
125
  self.instantiate_collection(parsed_data)
130
126
  else
131
- self.instantiate(
132
- self.parse(parsed_data[:data]).
133
- merge(_metadata: parsed_data[:metadata], _errors: parsed_data[:errors])
134
- )
127
+ self.instantiate(parsed_data[:data])
135
128
  end
136
129
  end
137
130
 
138
131
  def do_raw_query
139
- self.model.get_raw(:query, transformations: JSON.dump(self.transformations))
132
+ self.model.get_raw(path: :query, params: {transformations: JSON.dump(self.transformations)})
140
133
  end
141
134
 
142
135
  def first(*args)
@@ -0,0 +1,206 @@
1
+ module QueryInterface
2
+ module Client
3
+ class Property
4
+ attr_accessor :type, :name, :primary, :update, :array, :default
5
+
6
+ def initialize(name, type, options={})
7
+ unless conversion = QueryInterface::Client.types[type]
8
+ raise "Unknown Type #{type}"
9
+ end
10
+ self.type = conversion
11
+ self.primary = options.delete(:primary) {false}
12
+ self.update = options.delete(:update) {true}
13
+ self.array = options.delete(:array) {false}
14
+ self.default = options.delete(:default) {nil}
15
+ end
16
+
17
+ def run_get_conversion(value)
18
+ if self.array
19
+ value ||= []
20
+ value.map { |item| self.type.apply_get(item) }
21
+ else
22
+ self.type.apply_get(value) unless value.nil?
23
+ end
24
+ end
25
+
26
+ def run_set_conversion(value)
27
+ if self.array
28
+ value ||= []
29
+ value.map { |item| self.type.apply_set(item) }
30
+ else
31
+ self.type.apply_set(value) unless value.nil?
32
+ end
33
+ end
34
+ end
35
+
36
+ module Model
37
+ def self.included(base)
38
+ base.include(QueryInterface::Client::Request)
39
+ base.include(QueryInterface::Client::Resource)
40
+ base.include(ActiveModel::Validations)
41
+ base.include(ActiveModel::Validations::Callbacks)
42
+ base.include(ActiveModel::Conversion)
43
+ base.instance_variable_set(:@properties, {})
44
+ base.send(:attr_accessor, :_property_values, :_additional_values, :_property_changes)
45
+ base.extend(ClassMethods)
46
+ base.include(InstanceMethods)
47
+ end
48
+
49
+ module ClassMethods
50
+ def property(name, type, options={}, &definitions)
51
+ property = QueryInterface::Client::Property.new(name, type, options)
52
+
53
+ define_method name do
54
+ self.get_value(name)
55
+ end
56
+
57
+ unless property.primary || !property.update
58
+ define_method "#{name}=".to_sym do |value|
59
+ if self.get_value(name) != value
60
+ self.property_changes << name unless self.property_changes.include?(name)
61
+ self.set_value(name, value)
62
+ else
63
+ value
64
+ end
65
+ end
66
+ end
67
+
68
+ if block_given?
69
+ property.instance_eval &definitions
70
+ end
71
+
72
+ @properties[name] = property
73
+ end
74
+
75
+ def primary_keys
76
+ @properties.map do |name, property|
77
+ name if property.primary
78
+ end.compact
79
+ end
80
+
81
+ def new_collection(collection)
82
+ collection[:data].map do |data|
83
+ self.new(data)
84
+ end
85
+ end
86
+
87
+ def get(path, params={})
88
+ data = self.get_raw(path: path, params: params)[:parsed_data][:data]
89
+ self.new(data)
90
+ end
91
+ end
92
+
93
+ module InstanceMethods
94
+ def initialize(data={})
95
+ self.set_data(data)
96
+ end
97
+
98
+ def properties
99
+ self.class.instance_variable_get(:@properties)
100
+ end
101
+
102
+ def set_data(data)
103
+ data.each do |name, value|
104
+ name = name.to_sym
105
+ if self.properties.has_key?(name)
106
+ property = self.properties[name]
107
+ self.set_value(name, property.run_set_conversion(value))
108
+ else
109
+ self.additional_values[name] = value
110
+ end
111
+ end
112
+ # Initialize missing fields to whatever their default is
113
+ (self.properties.keys - data.keys.map(&:to_sym)).each do |name|
114
+ property = self.properties[name]
115
+ default = property.default
116
+ self.set_value(name, default.is_a?(Enumerable) ? default.dup : default)
117
+ end
118
+ end
119
+
120
+ def to_attributes(additional_data={})
121
+ data = {}
122
+ properties = self.properties.map do |name, property|
123
+ [name, property.run_get_conversion(self.get_value(name))] if (property.update && !property.primary)
124
+ end.compact.to_h
125
+ data[self.class.name.underscore.to_sym] = properties.merge(additional_data)
126
+ data
127
+ end
128
+
129
+ def save(additional_data={})
130
+ if self.persisted? && self.dirty?
131
+ result = self.put_raw(params: self.to_attributes(additional_data))
132
+ return result[:response].success?
133
+ elsif !self.persisted?
134
+ result = self.class.post_raw(params: self.to_attributes(additional_data))
135
+ if result[:response].success?
136
+ self.set_data(result[:parsed_data][:data])
137
+ return true
138
+ else
139
+ return false
140
+ end
141
+ else
142
+ return true
143
+ end
144
+ end
145
+
146
+ def update_attributes(attributes)
147
+ self.assign_attributes(attributes)
148
+ self.save
149
+ end
150
+
151
+ def assign_attributes(attributes)
152
+ attributes.each do |name, value|
153
+ method = "#{name}=".to_sym
154
+ if self.respond_to?(method)
155
+ self.send(method, value)
156
+ end
157
+ end
158
+ end
159
+
160
+ def property_values
161
+ self._property_values ||= {}
162
+ end
163
+
164
+ def additional_values
165
+ self._additional_values ||= {}
166
+ end
167
+
168
+ def property_changes
169
+ self._property_changes ||= []
170
+ end
171
+
172
+ def dirty?
173
+ self.property_changes.size > 0
174
+ end
175
+
176
+ def set_value(name, value)
177
+ self.property_values[name] = value
178
+ end
179
+
180
+ def get_value(name)
181
+ self.property_values[name]
182
+ end
183
+
184
+ def persisted?
185
+ !!self.id
186
+ end
187
+
188
+ def as_json(options={})
189
+ prop_vals = self.properties.map do |name, property|
190
+ [name, property.run_get_conversion(self.get_value(name))]
191
+ end.to_h
192
+ prop_vals.merge(self.additional_values).as_json(options)
193
+ end
194
+
195
+ def get(path, params={})
196
+ self.get_raw(path: path, params: params)[:parsed_data]
197
+ end
198
+
199
+ def destroy
200
+ result = self.delete_raw()[:parsed_data]
201
+ # TODO: handle errors and destroyed flag
202
+ end
203
+ end
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,98 @@
1
+ module QueryInterface
2
+ module Client
3
+ module Request
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ base.include(InstanceMethods)
7
+ base.instance_variable_set(:@_qi_collection_path_fragments, nil)
8
+ base.instance_variable_set(:@_qi_resource_path_fragments, nil)
9
+ end
10
+
11
+ module ClassMethods
12
+ def resource_name
13
+ self.name.underscore.pluralize
14
+ end
15
+
16
+ def collection_path_fragments(*fragments)
17
+ unless fragments.empty?
18
+ @_qi_collection_path_fragments = [self.default_api.url] + fragments
19
+ end
20
+ @_qi_collection_path_fragments || [self.default_api.url, self.resource_name]
21
+ end
22
+
23
+ def resource_path_fragments(*fragments)
24
+ unless fragments.empty?
25
+ @_qi_resource_path_fragments = [self.default_api.url] + fragments
26
+ end
27
+ @_qi_resource_path_fragments || self.collection_path_fragments + [:id]
28
+ end
29
+
30
+ def default_api
31
+ QueryInterface::Client::API.instance_variable_get(:@default_api)
32
+ end
33
+
34
+ def collection_path(path=nil)
35
+ fragments = self.collection_path_fragments.dup
36
+ fragments << path unless path.blank?
37
+ fragments.join("/")
38
+ end
39
+
40
+ # TODO: Discern by "am i an instance or a class" what collection and member routes do
41
+ def member_path(path=nil, keys={})
42
+ fragments = self.resource_path_fragments.map do |fragment|
43
+ if fragment.is_a?(Symbol)
44
+ if keys.has_key?(fragment)
45
+ keys[fragment]
46
+ else
47
+ nil
48
+ end
49
+ else
50
+ fragment
51
+ end
52
+ end.compact
53
+ fragments << path unless path.blank?
54
+ fragments.join("/")
55
+ end
56
+
57
+ def raw_request(method, params, keys, path)
58
+ if keys
59
+ params[:_path] = self.member_path(path, keys)
60
+ else
61
+ params[:_path] = self.collection_path(path)
62
+ end
63
+ params[:_method] = method
64
+ if params.include?(:_headers)
65
+ params[:_headers] = params[:_headers].merge(self.default_api.options[:headers])
66
+ else
67
+ params[:_headers] = self.default_api.options[:headers]
68
+ end
69
+ result = self.default_api.request(params)
70
+ {parsed_data: result[:parsed_data], response: result[:response]}
71
+ end
72
+
73
+ [:get, :post, :put, :delete].each do |method|
74
+ define_method "#{method}_raw" do |options={}|
75
+ params = options.delete(:params) { {} }
76
+ keys = options.delete(:keys) { {} }
77
+ path = options.delete(:path)
78
+ self.raw_request(method, params, keys, path)
79
+ end
80
+ end
81
+ end
82
+
83
+ module InstanceMethods
84
+ def keys
85
+ Hash[self.class.primary_keys.map {|primary_key| [primary_key, self.send(primary_key)]}]
86
+ end
87
+
88
+ [:get, :post, :put, :delete, :patch].each do |method|
89
+ define_method "#{method}_raw" do |options={}|
90
+ params = options.delete(:params) { {} }
91
+ path = options.delete(:path)
92
+ self.class.raw_request(method, params, self.keys, path)
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -1,32 +1,72 @@
1
- module QueryInterface::Client
2
- module Resource
1
+ module QueryInterface
2
+ module Client
3
+ class Association
4
+ attr_accessor :basemodel, :klass, :autoresolve, :dataset
3
5
 
4
- def self.included(base)
5
- base.extend(ClassMethods)
6
- end
7
-
8
- module ClassMethods
9
- def first(*args)
10
- self.query.order('id').first
6
+ def initialize(basemodel, name, options={})
7
+ self.basemodel = basemodel
8
+ self.klass = (options.delete(:class_name) {name.to_s.singularize.camelize}).constantize
9
+ self.autoresolve = options.delete(:autoresolve) {true}
10
+ self.dataset = options.delete(:dataset) {name}
11
11
  end
12
12
 
13
- def last(*args)
14
- self.query.order('id').last
13
+ def query(id)
14
+ self.basemodel.query.instance(id).context(self.dataset, self.klass)
15
15
  end
16
+ end
16
17
 
17
- def query
18
- LazyQuery.new(self)
18
+ module Resource
19
+ def self.included(base)
20
+ base.instance_variable_set(:@associations, {})
21
+ base.extend(ClassMethods)
22
+ base.include(InstanceMethods)
19
23
  end
20
24
 
21
- def association(name, class_name: nil, dataset: nil)
22
- dataset ||= name
23
- define_method(name) do
24
- klass = (class_name || name.to_s.singularize.camelize).constantize
25
- self.class.query.instance(self.id).context(dataset, klass)
25
+ module ClassMethods
26
+ def first(*args)
27
+ self.query.order('id').first
28
+ end
29
+
30
+ def last(*args)
31
+ self.query.order('id').last
32
+ end
33
+
34
+ def query
35
+ LazyQuery.new(self)
36
+ end
37
+
38
+ def association(name, options={})
39
+ association = QueryInterface::Client::Association.new(self, name, options)
40
+ define_method name do
41
+ association.query(self.id)
42
+ end
43
+ @associations[name] = association
44
+ end
45
+
46
+ def [](id)
47
+ self.query.instance(id).evaluate
48
+ end
49
+
50
+ def all
51
+ self.query.evaluate
26
52
  end
27
53
  end
28
54
 
29
- end
55
+ module InstanceMethods
56
+ def associations
57
+ self.class.instance_variable_get(:@associations)
58
+ end
30
59
 
60
+ def associations_resolved?
61
+ self.associations.map do |name, association|
62
+ [name, (association.autoresolve || self.send(name).count == 0)]
63
+ end.to_h
64
+ end
65
+
66
+ def has_unresolved_dependencies?
67
+ !self.associations_resolved?.values.all?
68
+ end
69
+ end
70
+ end
31
71
  end
32
72
  end
@@ -0,0 +1,77 @@
1
+ module QueryInterface
2
+ module Client
3
+ class Conversion
4
+ attr_accessor :set_block, :get_block
5
+
6
+ def initialize
7
+ self.get_block = self.set_block = Proc.new { |value| value }
8
+ end
9
+
10
+ def set(&set_block)
11
+ self.set_block = set_block
12
+ end
13
+
14
+ def get(&get_block)
15
+ self.get_block = get_block
16
+ end
17
+
18
+ def apply_set(value)
19
+ self.set_block.call(value)
20
+ end
21
+
22
+ def apply_get(value)
23
+ self.get_block.call(value)
24
+ end
25
+ end
26
+
27
+ def self.types
28
+ @_types ||= {}
29
+ end
30
+
31
+ def self.register_type(type, &registry_block)
32
+ conversion = Conversion.new
33
+ if block_given?
34
+ conversion.instance_eval &registry_block
35
+ end
36
+ self.types[type] = conversion
37
+ end
38
+
39
+ register_type :string do
40
+ set { |value| value.to_s }
41
+ end
42
+
43
+ register_type :integer do
44
+ set { |value| value.to_i }
45
+ end
46
+
47
+ register_type :float do
48
+ set { |value| value.to_f }
49
+ end
50
+
51
+ register_type :ip do
52
+ set { |value| IPAddr.new(value) }
53
+ get { |value| value.to_s }
54
+ end
55
+
56
+ register_type :date_time do
57
+ set { |value| value.to_datetime }
58
+ get { |value| value.to_s }
59
+ end
60
+
61
+ register_type :date do
62
+ set { |value| value.to_date }
63
+ get { |value| value.to_s }
64
+ end
65
+
66
+ register_type :mac do
67
+ set { |value| NetAddr::EUI.create(value) }
68
+ get { |value| value.address(Delimiter: ':') }
69
+ end
70
+
71
+ register_type :boolean do
72
+ set { |value| !!value }
73
+ end
74
+
75
+ register_type :raw
76
+ end
77
+ end
@@ -1,7 +1,7 @@
1
1
  module QueryInterface
2
2
  module Client
3
3
 
4
- VERSION = '1.2.1'
4
+ VERSION = '1.3.0'
5
5
 
6
6
  end
7
7
  end
@@ -4,3 +4,8 @@ require 'active_support/core_ext/string'
4
4
 
5
5
  require 'query-interface-client/lazy_query'
6
6
  require 'query-interface-client/resource'
7
+ require 'query-interface-client/model'
8
+ require 'query-interface-client/api'
9
+ require 'query-interface-client/request'
10
+ require 'query-interface-client/types'
11
+ require 'query-interface-client/json_middleware'
@@ -23,9 +23,9 @@ Gem::Specification.new do |s|
23
23
  s.summary = "Client for the radar query interface"
24
24
  s.license = "ISC"
25
25
 
26
- s.add_runtime_dependency(%q<her>)
27
- s.add_runtime_dependency(%q<activesupport>)
28
26
  s.add_runtime_dependency(%q<rs_paginator>)
27
+ s.add_runtime_dependency(%q<activesupport>)
28
+ s.add_runtime_dependency(%q<faraday>)
29
29
  s.add_development_dependency(%q<rake>)
30
30
  s.add_development_dependency(%q<rspec>)
31
31
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
 
4
4
  def deep_copy_check(left, right)
5
5
  left.each_with_index do |item, idx|
6
- right[idx].should_not be(item)
6
+ expect(right[idx]).not_to be(item)
7
7
  end
8
8
  end
9
9
 
@@ -19,13 +19,13 @@ describe QueryInterface::Client::LazyQuery do
19
19
 
20
20
  it "initializes itself with empty parameters and a supplied model" do
21
21
  query = subject.new(model)
22
- query.transformations.should == []
23
- query.model.should eq(model)
22
+ expect(query.transformations).to eq([])
23
+ expect(query.model).to eq(model)
24
24
  end
25
25
 
26
26
  it "honors passed transformations params" do
27
27
  query = subject.new(model, transformations)
28
- query.transformations.should eq(transformations)
28
+ expect(query.transformations).to eq(transformations)
29
29
  end
30
30
 
31
31
  it "does not alter the originally passed transformations" do
@@ -38,7 +38,7 @@ describe QueryInterface::Client::LazyQuery do
38
38
  it "provides a copy method cloning transformations onto a new instance" do
39
39
  query = subject.new(model, transformations)
40
40
  query_copy = query.copy
41
- query_copy.transformations.should eq(query.transformations)
41
+ expect(query_copy.transformations).to eq(query.transformations)
42
42
  deep_copy_check(query_copy.transformations, query.transformations)
43
43
  end
44
44
  end
@@ -47,32 +47,32 @@ describe QueryInterface::Client::LazyQuery do
47
47
  it "should create a new instance with updated transformations" do
48
48
  query = subject.new(model)
49
49
  query_copy = subject.new(model)
50
- query.should_receive(:copy).and_return(query_copy)
50
+ expect(query).to receive(:copy).and_return(query_copy)
51
51
  query.filter(a: :b)
52
- query_copy.transformations.should eq([{transformation: :filter, parameter: {field: :a, value: :b}}])
52
+ expect(query_copy.transformations).to eq([{transformation: :filter, parameter: {field: :a, value: :b}}])
53
53
  end
54
54
  end
55
55
 
56
56
  context "instancing" do
57
57
  it "sets the instance parameter" do
58
58
  query = subject.new(model)
59
- query.should_receive(:copy).and_return(query)
59
+ expect(query).to receive(:copy).and_return(query)
60
60
  query.instance(5)
61
- query.transformations.should eq([{transformation: :instance, parameter: 5}])
61
+ expect(query.transformations).to eq([{transformation: :instance, parameter: 5}])
62
62
  end
63
63
  end
64
64
 
65
65
  context "context" do
66
66
  let(:query) {subject.new(model)}
67
67
  before do
68
- query.should_receive(:copy).and_return(query)
68
+ expect(query).to receive(:copy).and_return(query)
69
69
  query.context(:context)
70
70
  end
71
71
  it "should set the correct result model class" do
72
- query.result_model.should == Context
72
+ expect(query.result_model).to eq(Context)
73
73
  end
74
74
  it "sets the context parameter" do
75
- query.transformations.should eq([{transformation: :context, parameter: :context}])
75
+ expect(query.transformations).to eq([{transformation: :context, parameter: :context}])
76
76
  end
77
77
  end
78
78
 
@@ -80,9 +80,9 @@ describe QueryInterface::Client::LazyQuery do
80
80
  it "should create a new instance including additional fields" do
81
81
  query = subject.new(model)
82
82
  query_copy = subject.new(model)
83
- query.should_receive(:copy).and_return(query_copy)
83
+ expect(query).to receive(:copy).and_return(query_copy)
84
84
  query.with(:c)
85
- query_copy.transformations.should eq([{transformation: :with, parameter: {field: :c}}])
85
+ expect(query_copy.transformations).to eq([{transformation: :with, parameter: {field: :c}}])
86
86
  end
87
87
  end
88
88
 
@@ -90,9 +90,9 @@ describe QueryInterface::Client::LazyQuery do
90
90
  it "should create a new instance includuing order fields" do
91
91
  query = subject.new(model)
92
92
  query_copy = subject.new(model)
93
- query.should_receive(:copy).and_return(query_copy)
93
+ expect(query).to receive(:copy).and_return(query_copy)
94
94
  query.order("-something")
95
- query_copy.transformations.should eq([{transformation: :order, parameter: "-something"}])
95
+ expect(query_copy.transformations).to eq([{transformation: :order, parameter: "-something"}])
96
96
  end
97
97
  end
98
98
 
@@ -100,7 +100,7 @@ describe QueryInterface::Client::LazyQuery do
100
100
  it "allows chaining transformations in order of appearance" do
101
101
  query = subject.new(model)
102
102
  query_copy = query.filter(a: :b, c: :d).filter(e: :f).with(:x, :y).instance(12).context(:context)
103
- query_copy.transformations.should eq(
103
+ expect(query_copy.transformations).to eq(
104
104
  [
105
105
  {transformation: :filter, parameter: {field: :a, value: :b}},
106
106
  {transformation: :filter, parameter: {field: :c, value: :d}},
@@ -118,8 +118,8 @@ describe QueryInterface::Client::LazyQuery do
118
118
  it 'gets the first object' do
119
119
  query = subject.new(model)
120
120
  result = double('result')
121
- query.should_receive(:one).with(:first).and_return(result)
122
- query.first.should be(result)
121
+ expect(query).to receive(:one).with(:first).and_return(result)
122
+ expect(query.first).to be(result)
123
123
  end
124
124
  end
125
125
 
@@ -127,8 +127,8 @@ describe QueryInterface::Client::LazyQuery do
127
127
  it 'gets the last object' do
128
128
  query = subject.new(model)
129
129
  result = double('result')
130
- query.should_receive(:one).with(:last).and_return(result)
131
- query.last.should be(result)
130
+ expect(query).to receive(:one).with(:last).and_return(result)
131
+ expect(query.last).to be(result)
132
132
  end
133
133
  end
134
134
 
@@ -136,17 +136,17 @@ describe QueryInterface::Client::LazyQuery do
136
136
  it 'gets one object' do
137
137
  query = subject.new(model)
138
138
  result = double('result')
139
- query.should_receive(:copy).and_return(query)
140
- query.should_receive(:do_query).and_return(result)
141
- query.result.should be_nil
142
- query.send(:one, :anyone).should be(result)
143
- query.transformations.should eq([{transformation: :anyone, parameter: nil}])
139
+ expect(query).to receive(:copy).and_return(query)
140
+ expect(query).to receive(:do_query).and_return(result)
141
+ expect(query.result).to be_nil
142
+ expect(query.send(:one, :anyone)).to be(result)
143
+ expect(query.transformations).to eq([{transformation: :anyone, parameter: nil}])
144
144
  end
145
145
  it "uses the cached result" do
146
146
  query = subject.new(model)
147
147
  query.result = ["a", "b", "c"]
148
- query.should_not_receive(:do_query)
149
- query.first.should eq("a")
148
+ expect(query).not_to receive(:do_query)
149
+ expect(query.first).to eq("a")
150
150
  end
151
151
  end
152
152
 
@@ -154,29 +154,29 @@ describe QueryInterface::Client::LazyQuery do
154
154
  let(:transformations) {[]}
155
155
  context "without instance set" do
156
156
  before do
157
- model.should_receive(:get_raw)
158
- .with(:query, transformations: JSON.dump(self.transformations))
157
+ expect(model).to receive(:get_raw)
158
+ .with(path: :query, params: {transformations: JSON.dump(self.transformations)})
159
159
  .and_return({parsed_data: {data: ["result object"]}})
160
- model.stub(:new_collection).and_return(["result object"])
160
+ allow(model).to receive(:new_collection).and_return(["result object"])
161
161
  end
162
162
 
163
163
  it "add an evaluation transformation" do
164
164
  query = subject.new(model)
165
165
  query.evaluate
166
- query.transformations.should eq(transformations)
166
+ expect(query.transformations).to eq(transformations)
167
167
  end
168
168
 
169
169
  it "gets results via do_query and caches the result" do
170
170
  query = subject.new(model)
171
- query.evaluate.should eq(["result object"])
172
- query.result.should eq(["result object"])
171
+ expect(query.evaluate).to eq(["result object"])
172
+ expect(query.result).to eq(["result object"])
173
173
  end
174
174
 
175
175
  it "doesn't query the api twice" do
176
176
  query = subject.new(model)
177
177
  result = query.evaluate
178
178
  result_second = query.evaluate
179
- result.should be(result_second)
179
+ expect(result).to be(result_second)
180
180
  end
181
181
  end
182
182
  end
@@ -186,17 +186,17 @@ describe QueryInterface::Client::LazyQuery do
186
186
 
187
187
  it "adds a map_ids transformation" do
188
188
  query = subject.new(model)
189
- query.should_receive(:copy).and_return(query)
190
- query.stub(:do_raw_query).and_return({parsed_data: {data: [1,2,3]}})
189
+ expect(query).to receive(:copy).and_return(query)
190
+ allow(query).to receive(:do_raw_query).and_return({parsed_data: {data: [1,2,3]}})
191
191
  query.ids
192
- query.transformations.should eq(self.transformations)
192
+ expect(query.transformations).to eq(self.transformations)
193
193
  end
194
194
 
195
195
  it "returns the data of the parsed query" do
196
196
  query = subject.new(model)
197
- query.should_receive(:copy).and_return(query)
198
- query.should_receive(:do_raw_query).and_return({parsed_data: {data: [1,2,3]}})
199
- query.ids.should eq([1, 2, 3])
197
+ expect(query).to receive(:copy).and_return(query)
198
+ expect(query).to receive(:do_raw_query).and_return({parsed_data: {data: [1,2,3]}})
199
+ expect(query.ids).to eq([1, 2, 3])
200
200
  end
201
201
  end
202
202
 
@@ -205,17 +205,17 @@ describe QueryInterface::Client::LazyQuery do
205
205
 
206
206
  it "adds a count transformation" do
207
207
  query = subject.new(model)
208
- query.should_receive(:copy).and_return(query)
209
- query.should_receive(:do_raw_query).and_return({parsed_data: {data: {count: 42}}})
210
- query.count.should eq(42)
211
- query.transformations.should eq(self.transformations)
208
+ expect(query).to receive(:copy).and_return(query)
209
+ expect(query).to receive(:do_raw_query).and_return({parsed_data: {data: {count: 42}}})
210
+ expect(query.count).to eq(42)
211
+ expect(query.transformations).to eq(self.transformations)
212
212
  end
213
213
 
214
214
  it "uses cached result for counting" do
215
215
  query = subject.new(model)
216
216
  query.result = ["a", "b", "c"]
217
- query.should_not_receive(:do_raw_query)
218
- query.count.should eq(3)
217
+ expect(query).not_to receive(:do_raw_query)
218
+ expect(query.count).to eq(3)
219
219
  end
220
220
  end
221
221
 
@@ -225,15 +225,15 @@ describe QueryInterface::Client::LazyQuery do
225
225
 
226
226
  it "adds a paginate transformation" do
227
227
  query = subject.new(model)
228
- query.should_receive(:copy).and_return(query)
228
+ expect(query).to receive(:copy).and_return(query)
229
229
  objects = (1..10).to_a
230
- model.stub(:new).and_return(*objects)
231
- query.should_receive(:do_raw_query).and_return({parsed_data: {data: {objects: objects, total: 15}, errors: []}})
230
+ allow(model).to receive(:new).and_return(*objects)
231
+ expect(query).to receive(:do_raw_query).and_return({parsed_data: {data: {objects: objects, total: 15}, errors: []}})
232
232
  result = query.paginate
233
- query.transformations.should eq(self.transformations)
234
- result.should eq(objects)
235
- result.is_a?(WillPaginate::Collection)
236
- result.total_entries.should eq(15)
233
+ expect(query.transformations).to eq(self.transformations)
234
+ expect(result).to eq(objects)
235
+ result.is_a?(RsPaginator::Collection)
236
+ expect(result.total).to eq(15)
237
237
  end
238
238
  end
239
239
 
@@ -241,8 +241,8 @@ describe QueryInterface::Client::LazyQuery do
241
241
  it "delegates unknown methods to the result of evaluate" do
242
242
  query = subject.new(model)
243
243
  result = double("result")
244
- query.should_receive(:evaluate).and_return(result)
245
- result.should_receive(:frobnicate!).with("uargh!")
244
+ expect(query).to receive(:evaluate).and_return(result)
245
+ expect(result).to receive(:frobnicate!).with("uargh!")
246
246
  query.frobnicate!("uargh!")
247
247
  end
248
248
  end
@@ -250,24 +250,23 @@ describe QueryInterface::Client::LazyQuery do
250
250
  context "do queries" do
251
251
  it "queries the api raw" do
252
252
  query = subject.new(model)
253
- model.should_receive(:get_raw).with(:query, transformations: "[]")
253
+ expect(model).to receive(:get_raw).with(path: :query, params: {transformations: "[]"})
254
254
  query.do_raw_query
255
255
  end
256
256
 
257
257
  it "executes the actual query and creates a collection where appropriate" do
258
258
  query = subject.new(model)
259
259
  data = {parsed_data: {data: [1,2,3]}}
260
- query.should_receive(:do_raw_query).and_return(data)
261
- query.should_receive(:instantiate_collection).with(data[:parsed_data])
260
+ expect(query).to receive(:do_raw_query).and_return(data)
261
+ expect(query).to receive(:instantiate_collection).with(data[:parsed_data])
262
262
  query.do_query
263
263
  end
264
264
 
265
265
  it "executes the actual query and creates an object where appropriate" do
266
266
  query = subject.new(model)
267
267
  data = {parsed_data: {data: {id: 1, bunny: 'wuschel'}}}
268
- query.should_receive(:do_raw_query).and_return(data)
269
- query.should_receive(:parse).and_return(data[:parsed_data])
270
- query.should_receive(:instantiate).with(data[:parsed_data].merge(_metadata: nil, _errors: nil))
268
+ expect(query).to receive(:do_raw_query).and_return(data)
269
+ expect(query).to receive(:instantiate).with(data[:parsed_data][:data])
271
270
  query.do_query
272
271
  end
273
272
 
@@ -280,13 +279,13 @@ describe QueryInterface::Client::LazyQuery do
280
279
  it "parses the data via result model if set" do
281
280
  query = subject.new(model)
282
281
  query.result_model = result_model
283
- result_model.should_receive(:parse).with(data)
282
+ expect(result_model).to receive(:parse).with(data)
284
283
  query.parse(data)
285
284
  end
286
285
 
287
286
  it "parses the data via model if no result model set" do
288
287
  query = subject.new(model)
289
- model.should_receive(:parse).with(data)
288
+ expect(model).to receive(:parse).with(data)
290
289
  query.parse(data)
291
290
  end
292
291
  end
@@ -295,14 +294,14 @@ describe QueryInterface::Client::LazyQuery do
295
294
  let(:transformations) {[]}
296
295
  let(:result) { double("result") }
297
296
  before do
298
- model.should_receive(:get_raw)
299
- .with(:query, transformations: JSON.dump(self.transformations))
297
+ expect(model).to receive(:get_raw)
298
+ .with(path: :query, params: {transformations: JSON.dump(self.transformations)})
300
299
  .and_return({parsed_data: {data: ["result object"]}})
301
- model.stub(:new_collection).and_return(result)
300
+ allow(model).to receive(:new_collection).and_return(result)
302
301
  end
303
302
  it "calls to_json on the evaluated result" do
304
303
  query = subject.new(model)
305
- result.should_receive(:to_json)
304
+ expect(result).to receive(:to_json)
306
305
  query.to_json
307
306
  end
308
307
  end
@@ -311,16 +310,16 @@ describe QueryInterface::Client::LazyQuery do
311
310
  let(:transformations) {[]}
312
311
  let(:result) { double("result") }
313
312
  before do
314
- model.should_receive(:get_raw)
315
- .with(:query, transformations: JSON.dump(self.transformations))
313
+ expect(model).to receive(:get_raw)
314
+ .with(path: :query, params: {transformations: JSON.dump(self.transformations)})
316
315
  .and_return({parsed_data: {data: ["result object"]}})
317
- model.stub(:new_collection).and_return(result)
316
+ allow(model).to receive(:new_collection).and_return(result)
318
317
  end
319
318
  it "calls to_key on the evaluated result if persisted" do
320
319
  query = subject.new(model)
321
- result.should_receive(:persisted?).and_return(true)
322
- result.should_receive(:to_key).and_return([5])
323
- query.to_param.should eq("5")
320
+ expect(result).to receive(:persisted?).and_return(true)
321
+ expect(result).to receive(:to_key).and_return([5])
322
+ expect(query.to_param).to eq("5")
324
323
  end
325
324
 
326
325
  end
@@ -8,27 +8,27 @@ describe QueryInterface::Client::Resource do
8
8
 
9
9
  let(:lazy_query) { double("LazyQuery") }
10
10
  before(:each) do
11
- QueryInterface::Client::LazyQuery.stub(new: lazy_query)
11
+ allow(QueryInterface::Client::LazyQuery).to receive_messages(new: lazy_query)
12
12
  end
13
13
  context "class methods" do
14
14
  context ".query" do
15
15
  it "returns a LazyQuery object" do
16
- RMDummyClass.query.should eq(lazy_query)
16
+ expect(RMDummyClass.query).to eq(lazy_query)
17
17
  end
18
18
  end
19
19
  context ".first" do
20
20
  it "queries the query object" do
21
21
  args = double("Args")
22
- lazy_query.should_receive(:order).with('id').and_return(lazy_query)
23
- lazy_query.should_receive(:first)
22
+ expect(lazy_query).to receive(:order).with('id').and_return(lazy_query)
23
+ expect(lazy_query).to receive(:first)
24
24
  RMDummyClass.first(args)
25
25
  end
26
26
  end
27
27
  context ".last" do
28
28
  it "queries the query object" do
29
29
  args = double("Args")
30
- lazy_query.should_receive(:order).with('id').and_return(lazy_query)
31
- lazy_query.should_receive(:last)
30
+ expect(lazy_query).to receive(:order).with('id').and_return(lazy_query)
31
+ expect(lazy_query).to receive(:last)
32
32
  RMDummyClass.last(args)
33
33
  end
34
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query-interface-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Kopecky <andreas.kopecky@radarservices.com>
@@ -10,10 +10,10 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-09-18 00:00:00.000000000 Z
13
+ date: 2014-10-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: her
16
+ name: rs_paginator
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
19
  - - ">="
@@ -41,7 +41,7 @@ dependencies:
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
43
  - !ruby/object:Gem::Dependency
44
- name: rs_paginator
44
+ name: faraday
45
45
  requirement: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
@@ -96,8 +96,13 @@ files:
96
96
  - README.md
97
97
  - Rakefile
98
98
  - lib/query-interface-client.rb
99
+ - lib/query-interface-client/api.rb
100
+ - lib/query-interface-client/json_middleware.rb
99
101
  - lib/query-interface-client/lazy_query.rb
102
+ - lib/query-interface-client/model.rb
103
+ - lib/query-interface-client/request.rb
100
104
  - lib/query-interface-client/resource.rb
105
+ - lib/query-interface-client/types.rb
101
106
  - lib/query-interface-client/version.rb
102
107
  - query-interface-client.gemspec
103
108
  - spec/lib/lazy_query_spec.rb