query-interface-client 0.0.2

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: fd8b45c8627082cfb7c536191a610f9e30f6d48b
4
+ data.tar.gz: 0d2b822eb090b86f7a12139e0202a7f247ddb1ec
5
+ SHA512:
6
+ metadata.gz: f5400488b3a33e95912dcd7ff3883de87434e56e7ad6205328ca894d80be24c22336531fd0ca6d111a0eabc45f3dec5a44268dc58e734b96a52ba22432752650
7
+ data.tar.gz: b7ab3388fad70f3313d662e1a975cc9f8fda4894a41c7e7e2feccdd1586aff00f2b8bf28a37b0c93df87ebcb9a65dc01557ea476de59da00ce2f1069128c63c3
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'her'
4
+ gem 'will_paginate'
5
+
6
+ group :development do
7
+ gem 'rake'
8
+ gem 'rspec'
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ GEM
2
+ remote: http://rubygems.org/
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)
11
+ thread_safe (~> 0.1)
12
+ tzinfo (~> 0.3.37)
13
+ atomic (1.1.10)
14
+ builder (3.1.4)
15
+ diff-lcs (1.2.4)
16
+ faraday (0.8.7)
17
+ multipart-post (~> 1.1)
18
+ her (0.6.7)
19
+ activemodel (>= 3.0.0)
20
+ activesupport (>= 3.0.0)
21
+ faraday (~> 0.8)
22
+ multi_json (~> 1.7)
23
+ i18n (0.6.4)
24
+ minitest (4.7.5)
25
+ multi_json (1.7.7)
26
+ multipart-post (1.2.0)
27
+ rake (10.1.0)
28
+ rspec (2.14.0)
29
+ rspec-core (~> 2.14.0)
30
+ rspec-expectations (~> 2.14.0)
31
+ rspec-mocks (~> 2.14.0)
32
+ rspec-core (2.14.2)
33
+ rspec-expectations (2.14.0)
34
+ diff-lcs (>= 1.1.3, < 2.0)
35
+ rspec-mocks (2.14.1)
36
+ thread_safe (0.1.0)
37
+ atomic
38
+ tzinfo (0.3.37)
39
+ will_paginate (3.0.4)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ her
46
+ rake
47
+ rspec
48
+ will_paginate
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2013 RadarServices Smart IT-Security <gems@radarservices.com>
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any
4
+ purpose with or without fee is hereby granted, provided that the above
5
+ copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # Query Interface Client
2
+
3
+ TODO
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ $ gem install query-interface-client
9
+ ```
10
+
11
+ ```ruby
12
+ require 'query-interface-client'
13
+ ```
14
+
15
+ or use ``gem 'query-interface-client'`` in your Gemfile when using bundler.
16
+
17
+ ##Examples
18
+
19
+ TODO
20
+
21
+ ### Usage
22
+
23
+ TODO
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
7
+ task test: :spec
@@ -0,0 +1,109 @@
1
+ module QueryInterface
2
+ module Client
3
+ class LazyQuery
4
+
5
+ attr_accessor :model, :api_params, :result
6
+
7
+ def initialize(model, api_params = nil)
8
+ self.model = model
9
+ self.api_params = {
10
+ conditions: api_params ? api_params[:conditions].dup : {},
11
+ with: api_params ? api_params[:with].dup : [],
12
+ order: api_params ? api_params[:order].dup : [],
13
+ }
14
+ self.result = nil
15
+ end
16
+
17
+ def filter(conditions)
18
+ self.copy.tap do |dataset|
19
+ dataset.api_params[:conditions].merge!(conditions)
20
+ end
21
+ end
22
+
23
+ def with(*fields)
24
+ self.copy.tap do |dataset|
25
+ dataset.api_params[:with] += fields
26
+ end
27
+ end
28
+
29
+ def order(*fields)
30
+ self.copy.tap do |dataset|
31
+ dataset.api_params[:order] = fields
32
+ end
33
+ end
34
+
35
+ def copy(options = {})
36
+ self.class.new(self.model, self.api_params)
37
+ end
38
+
39
+ def do_collection_query(params={})
40
+ self.model.get_collection(:query, query_data: self.api_params.merge(params))
41
+ end
42
+
43
+ def do_resource_query(params = {})
44
+ self.model.get_resource(:query, query_data: self.api_params.merge(params))
45
+ end
46
+
47
+ def do_raw_query(params = {})
48
+ self.model.get_raw(:query, query_data: self.api_params.merge(params))
49
+ end
50
+
51
+ def first(*args)
52
+ one(:first, *args)
53
+ end
54
+
55
+ def last(*args)
56
+ one(:last, *args)
57
+ end
58
+
59
+ def evaluate
60
+ self.result ||= self.do_collection_query(mode: :evaluate)
61
+ end
62
+
63
+ def paginate(params = {})
64
+ params = {page: 1, per_page: 10, mode: :paginate}.merge(params)
65
+ raw = self.do_raw_query(params)[:parsed_data]
66
+ result = raw[:data]
67
+ objects = result[:objects].map { |h| self.model.new(h) }
68
+ WillPaginate::Collection.create(params[:page], params[:per_page], result[:total]) do |pager|
69
+ pager.replace objects
70
+ end
71
+ end
72
+
73
+ def ids
74
+ response = self.do_raw_query(mode: :ids)
75
+ response.try(:[], :data)
76
+ end
77
+
78
+ def count
79
+ if self.result
80
+ self.result.count
81
+ else
82
+ r = self.do_raw_query(mode: :count)
83
+ r[:parsed_data][:data][:count]
84
+ end
85
+ end
86
+
87
+ def to_json(*args)
88
+ evaluate.to_json(*args)
89
+ end
90
+
91
+ def method_missing(method_name, *args, &block)
92
+ evaluate.send(method_name, *args, &block)
93
+ end
94
+
95
+ protected
96
+
97
+ def one(which, params = {})
98
+ if self.result
99
+ self.result.send(which)
100
+ else
101
+ self.do_resource_query(params.merge(mode: which))
102
+ end
103
+ rescue Faraday::Error::ResourceNotFound
104
+ nil
105
+ end
106
+
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,23 @@
1
+ module QueryInterface::Client
2
+ module Resource
3
+
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
11
+ end
12
+
13
+ def last(*args)
14
+ self.query.order('id').last
15
+ end
16
+
17
+ def query
18
+ LazyQuery.new(self)
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module QueryInterface
2
+ module Client
3
+
4
+ VERSION = '0.0.2'
5
+
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ require 'will_paginate/array'
2
+
3
+ require 'query-interface-client/lazy_query'
4
+ require 'query-interface-client/resource'
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), 'lib', 'query-interface-client', 'version')
4
+
5
+ require 'date'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "query-interface-client"
9
+ s.version = QueryInterface::Client::VERSION
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Andreas Kopecky <andreas.kopecky@radarservices.com>", "Anton Bangratz <anton.bangratz@radarservices.com>", "Martin Natano <martin.natano@radarservices.com"]
13
+ s.date = Date.today.strftime
14
+ s.description = "Client for the radar query interface"
15
+ s.email = "gems [a] radarservices [d] com"
16
+ s.files = `git ls-files`.split("\n").reject { |file| file == '.gitignore' }
17
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
18
+ s.extra_rdoc_files = %w[LICENSE README.md]
19
+
20
+ s.homepage = "http://github.com/rs-dev/query-interface-client"
21
+ s.require_paths = ["lib"]
22
+ # s.rubygems_version = "1.8.24"
23
+ s.summary = "Client for the radar query interface"
24
+ s.license = "ISC"
25
+
26
+ s.add_runtime_dependency(%q<her>)
27
+ s.add_runtime_dependency(%q<will_paginate>)
28
+ s.add_development_dependency(%q<rake>)
29
+ s.add_development_dependency(%q<rspec>)
30
+ end
@@ -0,0 +1,172 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ def deep_copy_check(left, right)
5
+ left.should_not be(right)
6
+ [:conditions, :with, :order].each do |key|
7
+ left[key].should_not be(right[key])
8
+ end
9
+ end
10
+
11
+ describe QueryInterface::Client::LazyQuery do
12
+ subject {QueryInterface::Client::LazyQuery}
13
+ let(:model) {double("Dummy Model")}
14
+ let(:default_params) { {conditions: {}, with: [], order: []} }
15
+
16
+ context "construction" do
17
+ let(:api_params) do
18
+ {conditions: {field: 'value'}, with: [:inclusion], order: ["-something"]}
19
+ end
20
+
21
+ it "initializes itself with empty parameters and a supplied model" do
22
+ query = subject.new(model)
23
+ query.api_params.should == {conditions: {}, with: [], order: []}
24
+ query.model.should eq(model)
25
+ end
26
+
27
+ it "honors passed api params" do
28
+ query = subject.new(model, api_params)
29
+ query.api_params.should == api_params
30
+ end
31
+
32
+ it "does not alter the originally passed api_params" do
33
+ query = subject.new(model, api_params)
34
+ deep_copy_check(api_params, query.api_params)
35
+ end
36
+ end
37
+
38
+ context "copy" do
39
+ let(:api_params) do
40
+ {conditions: {field: 'value'}, with: [:inclusion], order: ["-something"]}
41
+ end
42
+ it "provides a copy method cloning api_params onto a new instance" do
43
+ query = subject.new(model, api_params)
44
+ query_copy = query.copy
45
+ query_copy.api_params.should eq(query.api_params)
46
+ deep_copy_check(query_copy.api_params, query.api_params)
47
+ end
48
+ end
49
+
50
+ context "filtering" do
51
+ it "should create a new instance with updated api_params" do
52
+ query = subject.new(model)
53
+ query_copy = subject.new(model)
54
+ query.should_receive(:copy).and_return(query_copy)
55
+ query.filter(a: :b)
56
+ query_copy.api_params[:conditions].should eq({a: :b})
57
+ end
58
+ end
59
+
60
+ context "with" do
61
+ it "should create a new instance including additional fields" do
62
+ query = subject.new(model)
63
+ query_copy = subject.new(model)
64
+ query.should_receive(:copy).and_return(query_copy)
65
+ query.with(:c)
66
+ query_copy.api_params[:with].should eq([:c])
67
+ end
68
+ end
69
+
70
+ context "order" do
71
+ it "should create a new instance includuing order fields" do
72
+ query = subject.new(model)
73
+ query_copy = subject.new(model)
74
+ query.should_receive(:copy).and_return(query_copy)
75
+ query.order("-something")
76
+ query_copy.api_params[:order].should eq(["-something"])
77
+ end
78
+ end
79
+
80
+ context "chaining" do
81
+ it "allows chaining of filter and with" do
82
+ query = subject.new(model)
83
+ query_copy = query.filter(a: :b).filter(c: :d).with(:e).with(:f, :g)
84
+ query_copy.api_params[:conditions].should eq({a: :b, c: :d})
85
+ query_copy.api_params[:with].should eq([:e, :f, :g])
86
+ end
87
+
88
+ it "calling order multiple times overwrites" do
89
+ query = subject.new(model)
90
+ query_copy = query.order("-something").order("now really")
91
+ query_copy.api_params[:order].should eq(["now really"])
92
+ end
93
+ end
94
+
95
+ context "first" do
96
+ it "gets the first object via do_query" do
97
+ query = subject.new(model)
98
+ model.should_receive(:get_resource)
99
+ .with(:query, query_data: default_params.merge(mode: :first))
100
+ .and_return("result object")
101
+ query.first.should eq("result object")
102
+ end
103
+
104
+ it "uses the cached result" do
105
+ query = subject.new(model)
106
+ query.result = ["a", "b", "c"]
107
+ query.should_not_receive(:do_query)
108
+ query.first.should eq("a")
109
+ end
110
+ end
111
+
112
+ context "evaluate" do
113
+ it "gets results via do_query and caches the result" do
114
+ query = subject.new(model)
115
+ model.should_receive(:get_collection)
116
+ .with(:query, query_data: default_params.merge(mode: :evaluate))
117
+ .and_return(["result object"])
118
+ query.evaluate.should eq(["result object"])
119
+ query.result.should eq(["result object"])
120
+ end
121
+
122
+ it "doesn't query the api twice" do
123
+ query = subject.new(model)
124
+ model.should_receive(:get_collection)
125
+ .with(:query, query_data: default_params.merge(mode: :evaluate))
126
+ .and_return(["result object"])
127
+ result = query.evaluate
128
+ result_second = query.evaluate
129
+ result.should be(result_second)
130
+ end
131
+ end
132
+
133
+ context "count" do
134
+ it "gets the count via do_query" do
135
+ query = subject.new(model)
136
+ model.should_receive(:get_raw).with(:query, query_data: default_params.merge(mode: :count))
137
+ .and_return({parsed_data: {data: {count: 42}}})
138
+ query.count.should eq(42)
139
+ end
140
+
141
+ it "uses cached result for counting" do
142
+ query = subject.new(model)
143
+ query.result = ["a", "b", "c"]
144
+ query.should_not_receive(:do_query)
145
+ query.count.should eq(3)
146
+ end
147
+ end
148
+
149
+ context "paginate" do
150
+ it "paginates results" do
151
+ query = subject.new(model)
152
+ objects = (1..10).to_a
153
+ model.should_receive(:get_raw).with(:query, query_data: default_params.merge({mode: :paginate, page: 1, per_page: 10})).and_return({parsed_data: {data: {objects: objects, total: 15}, errors: []}})
154
+ objects.should_receive(:map).and_return(objects)
155
+ result = query.paginate(page: 1, per_page: 10)
156
+ result.should eq((1..10).to_a)
157
+ result.is_a?(WillPaginate::Collection)
158
+ result.total_entries.should eq(15)
159
+ end
160
+ end
161
+
162
+ context "method_missing" do
163
+ it "delegates unknown methods to the result of evaluate" do
164
+ query = subject.new(model)
165
+ result = double("result")
166
+ query.should_receive(:evaluate).and_return(result)
167
+ result.should_receive(:frobnicate!).with("uargh!")
168
+ query.frobnicate!("uargh!")
169
+ end
170
+ end
171
+
172
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ class RMDummyClass
4
+ include QueryInterface::Client::Resource
5
+ end
6
+
7
+ describe QueryInterface::Client::Resource do
8
+
9
+ let(:lazy_query) { double("LazyQuery") }
10
+ before(:each) do
11
+ QueryInterface::Client::LazyQuery.stub(new: lazy_query)
12
+ end
13
+ context "class methods" do
14
+ context ".query" do
15
+ it "returns a LazyQuery object" do
16
+ RMDummyClass.query.should eq(lazy_query)
17
+ end
18
+ end
19
+ context ".first" do
20
+ it "queries the query object" do
21
+ args = double("Args")
22
+ lazy_query.should_receive(:order).with('id').and_return(lazy_query)
23
+ lazy_query.should_receive(:first)
24
+ RMDummyClass.first(args)
25
+ end
26
+ end
27
+ context ".last" do
28
+ it "queries the query object" do
29
+ args = double("Args")
30
+ lazy_query.should_receive(:order).with('id').and_return(lazy_query)
31
+ lazy_query.should_receive(:last)
32
+ RMDummyClass.last(args)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ require 'query-interface-client'
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query-interface-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Andreas Kopecky <andreas.kopecky@radarservices.com>
8
+ - Anton Bangratz <anton.bangratz@radarservices.com>
9
+ - Martin Natano <martin.natano@radarservices.com
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-07-10 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: her
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: will_paginate
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rake
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rspec
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ description: Client for the radar query interface
72
+ email: gems [a] radarservices [d] com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files:
76
+ - LICENSE
77
+ - README.md
78
+ files:
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - lib/query-interface-client.rb
85
+ - lib/query-interface-client/lazy_query.rb
86
+ - lib/query-interface-client/resource.rb
87
+ - lib/query-interface-client/version.rb
88
+ - query-interface-client.gemspec
89
+ - spec/lib/lazy_query_spec.rb
90
+ - spec/lib/resource_spec.rb
91
+ - spec/spec_helper.rb
92
+ homepage: http://github.com/rs-dev/query-interface-client
93
+ licenses:
94
+ - ISC
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.0.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Client for the radar query interface
116
+ test_files: []