rubyql 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +103 -43
- data/lib/init_error.rb +2 -0
- data/lib/rubyql.rb +5 -6
- data/test/user_query_spectr.rb +39 -14
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26b1e539c01c2707d553beb0c1fd10bfc271a49b
|
4
|
+
data.tar.gz: 47c5dac8de78ad8c455db1e40ae627165f321ab3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2945940779830138b04183096903ae176ac8cd6aad33b11a2d6e5430a6aa434f74fedc1641c7ddace655202418cf598d653cbf530b097528bd59e72c76bc44e
|
7
|
+
data.tar.gz: ce16ed31a0e1c905103a0e74fa7da714bdfa01a41205e6fa02c1178c9f6c56ab060f4e47b1027b25a3ba99f4a6d51e7f51835f3104f7298259097608e0472b2c
|
data/README.md
CHANGED
@@ -9,13 +9,14 @@ RubyQL loves it when its used together with ActiveRecord 🤤
|
|
9
9
|
|
10
10
|
Example:
|
11
11
|
``` ruby
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
require 'rubyql'
|
13
|
+
|
14
|
+
class UserQuery < RubyQL
|
15
|
+
def query
|
16
|
+
result = User.find_by(query_params)
|
17
|
+
result.attributes unless result.nil?
|
18
18
|
end
|
19
|
+
end
|
19
20
|
```
|
20
21
|
|
21
22
|
The class we created overwrites the query method, to provide a custom mechanism which should return a hash. `query_params` is
|
@@ -23,16 +24,16 @@ the hash which holds the provided parameter, like `WHERE` in SQL.
|
|
23
24
|
|
24
25
|
**IMPORTANT**: `.attributes` is necessary for ActiveRecord, only returning the actual hash, not the ActiveModel object.
|
25
26
|
|
26
|
-
Usage:
|
27
|
-
```
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
27
|
+
## Usage:
|
28
|
+
``` ruby
|
29
|
+
UserQuery.new({"firstname"=>"", "lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute
|
30
|
+
=> {"firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}
|
31
|
+
|
32
|
+
UserQuery.new({"lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute
|
33
|
+
=> {"lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}
|
34
|
+
|
35
|
+
UserQuery.new({"lastname"=>"", "email"=>"", "id"=>1}).execute
|
36
|
+
=> {"lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com", "id"=>1}
|
36
37
|
```
|
37
38
|
|
38
39
|
As you can see we left the wanted attributes blank, so rubyql will fill them out, if they exist.
|
@@ -40,59 +41,118 @@ As you can see we left the wanted attributes blank, so rubyql will fill them out
|
|
40
41
|
For a better understanding another plain ruby example:
|
41
42
|
|
42
43
|
``` ruby
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
44
|
+
require 'rubyql
|
45
|
+
|
46
|
+
class PlainQuery < RubyQL
|
47
|
+
def query
|
48
|
+
{"firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com", "id"=>1337, "another_attribute"=>"Hello World"}
|
49
49
|
end
|
50
|
+
end
|
50
51
|
```
|
51
52
|
|
52
53
|
The method is only returning a simple hash. Now we can execute queries:
|
53
54
|
|
54
|
-
```
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
``` ruby
|
56
|
+
PlainQuery.new({"firstname"=>"", "lastname"=>"", "email"=>"niklas.hanft@outlook.com", "id"=>""}).execute
|
57
|
+
=> {"firstname"=>"Hanft", "lastname"=>"Niklas", "email"=>"niklas.hanft@outlook.com", "id"=>1337}
|
58
|
+
|
59
|
+
PlainQuery.new({"email"=>"niklas.hanft@outlook.com", "id"=>"", "another_attribute"=>""}).execute
|
60
|
+
=> {"email"=>"niklas.hanft@outlook.com", "id"=>1337, "another_attribute"=>"Hello World"}
|
60
61
|
```
|
61
62
|
|
62
63
|
As this is static, it doesn't really make sense, but for understanding it might be ok to use it this way. But mainly it
|
63
64
|
should be used with a database orm or some dynamic functions which return hashes.
|
64
65
|
|
65
|
-
Advanced usage:
|
66
|
+
## Advanced usage:
|
67
|
+
|
68
|
+
A typical API in Rails
|
69
|
+
|
70
|
+
``` ruby
|
71
|
+
module Api
|
72
|
+
module V1
|
73
|
+
class UserQueryController < ApiController
|
74
|
+
before_action :user_params
|
75
|
+
|
76
|
+
def execute
|
77
|
+
render json: UserQuery.new(user_params).execute.to_json
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
66
81
|
|
82
|
+
def user_params
|
83
|
+
params.permit(:username, :firstname, :email, :id, :lastname, :posts, :created_at).to_h
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
67
89
|
```
|
90
|
+
|
91
|
+
The Query class
|
92
|
+
|
93
|
+
``` ruby
|
68
94
|
require 'rubyql'
|
69
95
|
|
70
96
|
class UserQuery < RubyQL
|
71
97
|
def query
|
72
|
-
User.find_by(query_params)
|
98
|
+
result = User.find_by(query_params)
|
99
|
+
result.attributes unless result.nil?
|
73
100
|
end
|
74
101
|
end
|
75
102
|
```
|
76
103
|
|
104
|
+
Routing:
|
105
|
+
|
106
|
+
``` ruby
|
107
|
+
namespace :api, defaults: { format: :json } do
|
108
|
+
namespace :v1 do
|
109
|
+
post '/user-query', to: 'user_query#execute'
|
110
|
+
end
|
111
|
+
end
|
77
112
|
```
|
78
|
-
irb(main):007:0> UserQuery.new({ "firstname"=>"", "id"=>1}).execute
|
79
|
-
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
80
|
-
=> {"firstname"=>"Niklas", "id"=>1}
|
81
|
-
irb(main):008:0> UserQuery.new({ "lastname"=>"", "id"=>1}).execute
|
82
|
-
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
83
|
-
=> {"lastname"=>"Hanft", "id"=>1}
|
84
|
-
irb(main):009:0> UserQuery.new({ "lastname"=>"", "id"=>1, "email"=>""}).execute
|
85
|
-
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
86
|
-
=> {"lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com", "id"=>1}
|
87
|
-
irb(main):010:0> UserQuery.new({ "lastname"=>"", "id"=>1, "email"=>"", "firstname"=>""}).execute
|
88
|
-
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
89
|
-
=> {"firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com", "id"=>1}
|
90
113
|
|
114
|
+
A request:
|
115
|
+
|
116
|
+
``` json
|
117
|
+
POST /api/v1/user-query
|
118
|
+
{
|
119
|
+
"email": "niklas.hanft@outlook.com",
|
120
|
+
"firstname":"",
|
121
|
+
"id":"",
|
122
|
+
"created_at":"",
|
123
|
+
"lastname":""
|
124
|
+
}
|
91
125
|
```
|
92
|
-
*Coming soon*
|
93
126
|
|
127
|
+
The response:
|
94
128
|
|
129
|
+
``` json
|
130
|
+
{
|
131
|
+
"id": 1,
|
132
|
+
"firstname": "Niklas",
|
133
|
+
"lastname": "Hanft",
|
134
|
+
"created_at": "2017-11-21T07:10:46.953Z",
|
135
|
+
"email": "niklas.hanft@outlook.com"
|
136
|
+
}
|
137
|
+
```
|
138
|
+
|
139
|
+
A second request:
|
95
140
|
|
141
|
+
``` json
|
142
|
+
POST /api/v1/user-query
|
143
|
+
{
|
144
|
+
"email": "niklas.hanft@outlook.com",
|
145
|
+
"firstname":""
|
146
|
+
}
|
147
|
+
```
|
96
148
|
|
149
|
+
The response:
|
150
|
+
|
151
|
+
``` json
|
152
|
+
{
|
153
|
+
"email": "niklas.hanft@outlook.com",
|
154
|
+
"firstname": "Niklas"
|
155
|
+
}
|
156
|
+
```
|
97
157
|
|
98
158
|
|
data/lib/init_error.rb
ADDED
data/lib/rubyql.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'json'
|
2
|
+
require_relative './init_error'
|
2
3
|
|
3
4
|
class RubyQL
|
4
5
|
attr_accessor :params
|
@@ -9,30 +10,28 @@ class RubyQL
|
|
9
10
|
elsif params.is_a?(Hash)
|
10
11
|
@params = params
|
11
12
|
else
|
12
|
-
raise
|
13
|
+
raise InitError, "Cant initialize with a #{params.class} please provide a valid JSON or Hash"
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
16
17
|
def execute
|
17
18
|
return {} if query.nil?
|
18
|
-
query.select do |key,
|
19
|
+
query.select do |key, _value|
|
19
20
|
response_attr.key? key
|
20
21
|
end.merge query_params
|
21
22
|
end
|
22
23
|
|
23
24
|
def query_params
|
24
|
-
@params.reject do |
|
25
|
+
@params.reject do |_key, value|
|
25
26
|
value == '' || value.nil?
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
30
|
def response_attr
|
30
|
-
@params.select do |
|
31
|
+
@params.select do |_key, value|
|
31
32
|
value == '' || value.nil?
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
36
|
def query; end
|
36
|
-
|
37
37
|
end
|
38
|
-
|
data/test/user_query_spectr.rb
CHANGED
@@ -1,27 +1,52 @@
|
|
1
1
|
require 'spectr'
|
2
2
|
require_relative '../lib/rubyql'
|
3
|
+
require_relative '../lib/init_error'
|
3
4
|
|
4
|
-
|
5
|
+
class UserQuery < RubyQL
|
6
|
+
def query
|
7
|
+
{ 'username' => 'paradoxxger',
|
8
|
+
'firstname' => 'Niklas',
|
9
|
+
'lastname' => 'Hanft',
|
10
|
+
'email' => 'niklas.hanft@outlook.com' }
|
11
|
+
end
|
12
|
+
end
|
5
13
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
14
|
+
Spectr.new.test 'Test the UserQuery class' do |test|
|
15
|
+
test.assume('The response is a hash', Hash) do
|
16
|
+
UserQuery.new('lastname' => '', 'email' => 'niklas.hanft@outlook.com').execute.class
|
17
|
+
end
|
18
|
+
|
19
|
+
assumed_result = { 'firstname' => 'Niklas', 'lastname' => 'Hanft', 'email' => 'niklas.hanft@outlook.com' }
|
20
|
+
|
21
|
+
test.assume('The response equals the given hash', assumed_result) do
|
22
|
+
UserQuery.new('firstname' => '', 'lastname' => '', 'email' => 'niklas.hanft@outlook.com').execute
|
10
23
|
end
|
11
24
|
|
12
|
-
|
13
|
-
|
25
|
+
assumed_result = { 'lastname' => 'Hanft', 'email' => 'niklas.hanft@outlook.com' }
|
26
|
+
|
27
|
+
test.assume('The response equals the given hash', assumed_result) do
|
28
|
+
UserQuery.new('lastname' => '', 'email' => 'niklas.hanft@outlook.com').execute
|
14
29
|
end
|
15
30
|
|
16
|
-
test.assume('The
|
17
|
-
UserQuery.new(
|
31
|
+
test.assume('The query_params are only the email', 'email' => 'niklas.hanft@outlook.com') do
|
32
|
+
UserQuery.new('lastname' => '', 'email' => 'niklas.hanft@outlook.com').query_params
|
18
33
|
end
|
34
|
+
end
|
19
35
|
|
20
|
-
|
21
|
-
|
36
|
+
Spectr.new.test 'Test the wrong initialization of the UserQuery class' do |test|
|
37
|
+
test.assume('The initialization should fail with a String', InitError) do
|
38
|
+
begin
|
39
|
+
UserQuery.new('WrongAttribute')
|
40
|
+
rescue InitError => e
|
41
|
+
e.class
|
42
|
+
end
|
22
43
|
end
|
23
44
|
|
24
|
-
test.assume('The
|
25
|
-
|
45
|
+
test.assume('The initialization should fail with an Integer', InitError) do
|
46
|
+
begin
|
47
|
+
UserQuery.new(1337)
|
48
|
+
rescue InitError => e
|
49
|
+
e.class
|
50
|
+
end
|
26
51
|
end
|
27
|
-
end
|
52
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Niklas Hanft
|
@@ -17,6 +17,7 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- README.md
|
20
|
+
- lib/init_error.rb
|
20
21
|
- lib/rubyql.rb
|
21
22
|
- test/user_query_spectr.rb
|
22
23
|
homepage: https://github.com/ParadoXxGER/rubyql
|