httpsql 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -36,6 +36,7 @@ Assume you have a model, widget, whose fields are id, int_field, string_field, c
36
36
 
37
37
  create_table "widgets", :force => true do |t|
38
38
  t.integer "int_field"
39
+ t.float "dec_field"
39
40
  t.string "string_field"
40
41
  t.datetime "created_at", :null => false
41
42
  t.datetime "updated_at", :null => false
@@ -45,7 +46,7 @@ Assume you have a model, widget, whose fields are id, int_field, string_field, c
45
46
 
46
47
  class Widget < ActiveRecord::Base
47
48
  include Httpsql
48
- attr_accessible :int_field, :string_field
49
+ attr_accessible :int_field, :dec_field, :string_field
49
50
  end
50
51
 
51
52
  ### api.rb
@@ -98,6 +99,18 @@ Query your new API
98
99
  curl 'http://localhost:3000/api/v1/widgets?id[]=1&id[]=2&created_at.gt=2013-06-01&fields[]=id&fields[]=int_field'
99
100
  SELECT id, int_field FROM widgets WHERE id IN (1,2) AND created_at > '2013-06-01'
100
101
 
102
+ curl 'http://localhost:3000/api/v1/widgets?int_field.sum'
103
+ SELECT SUM(int_field) AS int_field FROM widgets
104
+
105
+ curl 'http://localhost:3000/api/v1/widgets?int_field.maximum'
106
+ SELECT MAX(int_field) AS int_field FROM widgets
107
+
108
+ curl 'http://localhost:3000/api/v1/widgets?int_field.minimum'
109
+ SELECT MIN(int_field) AS int_field FROM widgets
110
+
111
+ curl 'http://localhost:3000/api/v1/widgets?dec_field.round=1'
112
+ SELECT ROUND(dec_field, 1) AS dec_field FROM widgets
113
+
101
114
  curl 'http://localhost:3000/api/v1/describe_api'
102
115
  Returns JSON describing the API
103
116
 
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require "bundler/gem_tasks"
2
2
  require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
- t.libs.push "lib"
5
+ t.libs.push "test"
6
6
  t.test_files = FileList["test/*_test.rb"]
7
7
  t.verbose = true
8
8
  end
data/lib/httpsql.rb CHANGED
@@ -7,20 +7,32 @@ module Httpsql
7
7
 
8
8
  module ClassMethods
9
9
  def where_params_eq(params={})
10
- cond = valid_params(params).map do |k,v|
10
+ fields = params[:field] || []
11
+ conds = []
12
+
13
+ valid_params(params).each do |k,v|
11
14
  (k, m) = k.to_s.split('.')
12
15
  next if k.to_s == 'access_token'
13
16
  if m
14
- arel_table[k.to_sym].send(m.to_sym, v)
17
+ if %w(sum minimum maximum).include?(m)
18
+ fields << arel_table[k].send(m).as(k)
19
+ elsif !arel_table[k].respond_to?(m)
20
+ args = v.split(',')
21
+ fields << Arel::Nodes::NamedFunction.new(m, [arel_table[k], *args], k)
22
+ else
23
+ conds << arel_table[k].send(m, v)
24
+ end
15
25
  elsif v.respond_to?(:any?)
16
- arel_table[k.to_sym].in(v)
26
+ conds << arel_table[k].in(v)
17
27
  else
18
- arel_table[k.to_sym].eq(v)
28
+ conds << arel_table[k].eq(v)
19
29
  end
20
- end.inject{|x,y| x.and(y)}
30
+ end
31
+
32
+ conds = conds.compact.inject{|x,y| x.and(y)}
21
33
 
22
- ar_rel = where(cond)
23
- ar_rel = ar_rel.select(params[:field]) if params[:field]
34
+ ar_rel = where(conds)
35
+ ar_rel = ar_rel.select(fields) if fields.any?
24
36
  ar_rel
25
37
  end
26
38
 
@@ -45,3 +57,4 @@ module Httpsql
45
57
  end
46
58
  end
47
59
  end
60
+
@@ -1,3 +1,3 @@
1
1
  module Httpsql
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/test/httpsql_test.rb CHANGED
@@ -1,34 +1,12 @@
1
- require 'minitest/spec'
2
- require 'minitest/autorun'
3
- require 'active_record'
4
- require 'httpsql'
5
-
6
- ActiveRecord::Base.configurations[:test] = {adapter: 'sqlite3', database: 'tmp/httpsql_test'}
7
- ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[:test])
8
- ActiveRecord::Base.connection.execute %Q{ DROP TABLE IF EXISTS foo_models }
9
- ActiveRecord::Base.connection.execute %Q{
10
- CREATE TABLE foo_models (
11
- id integer,
12
- int_field integer,
13
- string_field text,
14
- access_token text,
15
- created_at text default CURRENT_TIMESTAMP,
16
- updated_at text default CURRENT_TIMESTAMP,
17
- primary key(id)
18
- );
19
- }
20
- class FooModel < ActiveRecord::Base
21
- include Httpsql
22
- attr_accessible :int_field, :string_field, :access_token
23
- end
1
+ require 'test_helper'
24
2
 
25
3
  def generate_models
26
4
  FooModel.create!([
27
- {int_field: 0, string_field: "zero", access_token: "000"},
28
- {int_field: 1, string_field: "one", access_token: "111"},
29
- {int_field: 2, string_field: "two", access_token: "222"},
30
- {int_field: 3, string_field: "three", access_token: "333"},
31
- {int_field: 4, string_field: "four", access_token: "444"},
5
+ {int_field: 0, dec_field: 0.01, string_field: "zero", access_token: "000"},
6
+ {int_field: 1, dec_field: 1.01, string_field: "one", access_token: "111"},
7
+ {int_field: 2, dec_field: 2.01, string_field: "two", access_token: "222"},
8
+ {int_field: 3, dec_field: 3.01, string_field: "three", access_token: "333"},
9
+ {int_field: 4, dec_field: 4.01, string_field: "four", access_token: "444"},
32
10
  ])
33
11
  end
34
12
 
@@ -112,10 +90,35 @@ describe Httpsql do
112
90
  FooModel.where_params_eq("int_field.eq" => 0, field: [:int_field, :id]).must_equal model
113
91
  end
114
92
 
93
+ it 'sums the specified field' do
94
+ models = generate_models
95
+ expected = models.collect(&:int_field).inject(:+)
96
+ FooModel.where_params_eq("int_field.sum" => nil).first.int_field.must_equal expected
97
+ end
98
+
99
+ it 'selects the maximum value for the specified field' do
100
+ models = generate_models
101
+ expected = models.collect(&:int_field).max
102
+ FooModel.where_params_eq("int_field.maximum" => nil).first.int_field.must_equal expected
103
+ end
104
+
105
+ it 'selects the minimum value for the specified field' do
106
+ models = generate_models
107
+ expected = models.collect(&:int_field).min
108
+ FooModel.where_params_eq("int_field.minimum" => nil).first.int_field.must_equal expected
109
+ end
110
+
111
+ it 'generates the specified sql for rounding' do
112
+ models = generate_models
113
+ expected = models.collect(&:dec_field).map{|v| v.round.to_f}
114
+ FooModel.where_params_eq("dec_field.round" => "1").collect(&:dec_field).map{|v| v.to_f}.must_equal expected
115
+ end
116
+
115
117
  it 'generates the correct documentation' do
116
118
  FooModel.route_params.must_equal({
117
119
  "id" => {:type => "integer", :desc => "id", :primary => true},
118
120
  "int_field" => {:type => "integer", :desc => "int_field", :primary => false},
121
+ "dec_field" => {:type => "decimal", :desc => "dec_field", :primary => false},
119
122
  "string_field" => {:type => "text", :desc => "string_field", :primary => false},
120
123
  "access_token" => {:type => "text", :desc => "access_token", :primary => false},
121
124
  "created_at" => {:type => "text", :desc => "created_at", :primary => false},
@@ -0,0 +1,25 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'active_record'
4
+ require 'httpsql'
5
+
6
+ ActiveRecord::Base.configurations[:test] = {adapter: 'sqlite3', database: 'tmp/httpsql_test'}
7
+ ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[:test])
8
+ ActiveRecord::Base.connection.execute %Q{ DROP TABLE IF EXISTS foo_models }
9
+ ActiveRecord::Base.connection.execute %Q{
10
+ CREATE TABLE foo_models (
11
+ id integer,
12
+ int_field integer,
13
+ dec_field decimal,
14
+ string_field text,
15
+ access_token text,
16
+ created_at text default CURRENT_TIMESTAMP,
17
+ updated_at text default CURRENT_TIMESTAMP,
18
+ primary key(id)
19
+ );
20
+ }
21
+ class FooModel < ActiveRecord::Base
22
+ include Httpsql
23
+ attr_accessible :int_field, :dec_field, :string_field, :access_token
24
+ end
25
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httpsql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-07-03 00:00:00.000000000 Z
14
+ date: 2013-07-16 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: bundler
@@ -112,6 +112,7 @@ files:
112
112
  - lib/httpsql.rb
113
113
  - lib/httpsql/version.rb
114
114
  - test/httpsql_test.rb
115
+ - test/test_helper.rb
115
116
  homepage: https://github.com/Adaptly/httpsql
116
117
  licenses:
117
118
  - MIT
@@ -140,3 +141,4 @@ summary: Select model specified fields, create arbitrary queries, all using CGI
140
141
  parameters
141
142
  test_files:
142
143
  - test/httpsql_test.rb
144
+ - test/test_helper.rb