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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e001320b038e8c6316fe99ed61f706fd89027f9
4
- data.tar.gz: 0024b94ff7ed2c24f1519de8458c703d0858087b
3
+ metadata.gz: 26b1e539c01c2707d553beb0c1fd10bfc271a49b
4
+ data.tar.gz: 47c5dac8de78ad8c455db1e40ae627165f321ab3
5
5
  SHA512:
6
- metadata.gz: 3274005fde19a0dbcd06b3c99d4c5d8665a195772864a1ae2015178ada2c447676c6dc110f0488c9483c5bf9e737cecccde4823e7289ef587b032d7531c6dd05
7
- data.tar.gz: 57ba32c92e096e0aba05fd27f60b1f1a0ae668ed4610575822f994d6f083902a4a35c708eacca2aa18eac03a03b9b3f4edb3ec5faf4e86f9f2fed0cad9dc5166
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
- require 'rubyql'
13
-
14
- class UserQuery < RubyQL
15
- def query
16
- User.find_by(query_params).attributes
17
- end
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
- UserQuery.new({"firstname"=>"", "lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute
29
- => {"firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}
30
-
31
- UserQuery.new({"lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute
32
- => {"lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}
33
-
34
- UserQuery.new({"lastname"=>"", "email"=>"", "id"=>1}).execute
35
- => {"lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com", "id"=>1}
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
- require 'rubyql
44
-
45
- class PlainQuery < RubyQL
46
- def query
47
- {"firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com", "id"=>1337, "another_attribute"=>"Hello World"}
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
- PlainQuery.new({"firstname"=>"", "lastname"=>"", "email"=>"niklas.hanft@outlook.com", "id"=>""}).execute
56
- => {"firstname"=>"Hanft", "lastname"=>"Niklas", "email"=>"niklas.hanft@outlook.com", "id"=>1337}
57
-
58
- PlainQuery.new({"email"=>"niklas.hanft@outlook.com", "id"=>"", "another_attribute"=>""}).execute
59
- => {"email"=>"niklas.hanft@outlook.com", "id"=>1337, "another_attribute"=>"Hello World"}
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: In a Rails Project:
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).attributes
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
 
@@ -0,0 +1,2 @@
1
+ class InitError < StandardError
2
+ end
@@ -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, value|
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 |key, value|
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 |key, value|
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
-
@@ -1,27 +1,52 @@
1
1
  require 'spectr'
2
2
  require_relative '../lib/rubyql'
3
+ require_relative '../lib/init_error'
3
4
 
4
- Spectr.new.test 'Test the queryable class' do |test|
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
- class UserQuery < RubyQL
7
- def query
8
- {"username"=>"paradoxxger", "firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}
9
- end
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
- test.assume('The response is a hash', true) do
13
- UserQuery.new({"firstname"=>"", "lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute.is_a?(Hash)
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 response equals the given hash', {"firstname"=>"Niklas", "lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}) do
17
- UserQuery.new({"firstname"=>"", "lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute
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
- test.assume('The response equals the given hash, removing some attributes', {"lastname"=>"Hanft", "email"=>"niklas.hanft@outlook.com"}) do
21
- UserQuery.new({"lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).execute
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 query_params are only the email', {"email" => "niklas.hanft@outlook.com"}) do
25
- UserQuery.new({"lastname"=>"", "email"=>"niklas.hanft@outlook.com"}).query_params
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.2
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