montage_rails 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/montage_rails/model_generator.rb +49 -0
- data/lib/generators/montage_rails/templates/model.rb +5 -0
- data/lib/generators/montage_rails/templates/resource.rb +22 -0
- data/lib/generators/montage_rails/templates/test_data.rb +6 -0
- data/lib/montage_rails/application_resource.rb +63 -0
- data/lib/montage_rails/mock_server.rb +54 -0
- data/lib/montage_rails/railtie.rb +5 -0
- data/lib/montage_rails/version.rb +1 -1
- data/lib/montage_rails.rb +34 -1
- data/test/dummy/app/models/movie.rb +1 -0
- data/test/dummy/app/models/test_mod.rb +3 -0
- data/test/dummy/app/models/test_one.rb +3 -0
- data/test/dummy/config/initializers/montage.rb +1 -0
- data/test/dummy/config/routes.rb +3 -54
- data/test/dummy/log/development.log +8 -0
- data/test/dummy/log/test.log +4074 -0
- data/test/dummy/test/fixtures/actors.yml +9 -0
- data/test/dummy/test/fixtures/movies.yml +17 -0
- data/test/dummy/test/fixtures/studios.yml +7 -0
- data/test/dummy/test/fixtures/test_mods.yml +9 -0
- data/test/dummy/test/fixtures/test_ones.yml +11 -0
- data/test/dummy/test/models/actor_test.rb +7 -0
- data/test/dummy/test/models/movie_test.rb +7 -0
- data/test/dummy/test/models/studio_test.rb +7 -0
- data/test/dummy/test/models/test_mod_test.rb +5 -0
- data/test/dummy/test/models/test_one_test.rb +7 -0
- data/test/dummy/test/montage_resources/actor_resource.rb +26 -0
- data/test/dummy/test/montage_resources/movie_resource.rb +43 -0
- data/test/dummy/test/montage_resources/studio_resource.rb +18 -0
- data/test/dummy/test/montage_resources/test_data/actor_test_data.yml +8 -0
- data/test/dummy/test/montage_resources/test_data/movie_test_data.yml +8 -0
- data/test/dummy/test/montage_resources/test_data/studio_test_data.yml +3 -0
- data/test/dummy/test/resources/test_mod_resource.rb +22 -0
- data/test/dummy/test/resources/test_one_resource.rb +26 -0
- data/test/dummy/test/test_helper.rb +0 -0
- data/test/montage_rails/application_resource_test.rb +92 -0
- data/test/montage_rails/base_test.rb +22 -22
- data/test/montage_rails/relation_test.rb +1 -1
- data/test/montage_rails_test.rb +57 -14
- data/test/test_helper.rb +9 -166
- metadata +108 -22
- data/test/resources/actor_resource.rb +0 -141
- data/test/resources/movie_resource.rb +0 -221
- data/test/resources/studio_resource.rb +0 -56
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 521228f95db5309b934fe1f0a862a9bc58760796
|
4
|
+
data.tar.gz: 4c8c2ebba95259f43432b709467f9d15ffc266bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb2aa1347f8c8e3dd2c7853336843c0f6619016e2a157213b6104ef4cbe5183a6711e74363dddde5eda71437d3f30a62b1650d32f7561b1288179fc5b4b1d79b
|
7
|
+
data.tar.gz: 4f88f51c56214b420cddf269a6ccf7c08f627640b51551558521a6931d74388681292295b1b6d6b00b9f6d73bcaeeee37cc809da1c9bab7ec7e786c6ac633aa5
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'faker'
|
2
|
+
class MontageRails::ModelGenerator < ::Rails::Generators::NamedBase
|
3
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
4
|
+
argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
|
5
|
+
|
6
|
+
desc "Creates a MontageRails model, a testing resource, and tests"
|
7
|
+
|
8
|
+
def create_model_file
|
9
|
+
template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_test_resource
|
13
|
+
template "resource.rb", File.join("test/montage_resources", class_path, "#{file_name}_resource.rb")
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_test_data
|
17
|
+
template 'test_data.rb', File.join("test/montage_resources/test_data", class_path, "#{file_name}_test_data.yml") # TODO
|
18
|
+
end
|
19
|
+
|
20
|
+
hook_for :test_framework
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def random_for_type(type)
|
25
|
+
case type
|
26
|
+
when :integer
|
27
|
+
Faker::Number.number(10)
|
28
|
+
when :float
|
29
|
+
Faker::Number.decimal(2, 3)
|
30
|
+
when :text
|
31
|
+
Faker::Lorem.words(10).join(' ')
|
32
|
+
when :date
|
33
|
+
Faker::Date.backward(14)
|
34
|
+
when :time
|
35
|
+
Faker::Time.between(2.days.ago, Time.now, :all)
|
36
|
+
when :datetime
|
37
|
+
Faker::Time.between(2.days.ago, Time.now, :all)
|
38
|
+
when :numeric
|
39
|
+
Faker::Number.number(10)
|
40
|
+
else
|
41
|
+
# TODO
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
def parent_class_name
|
47
|
+
options[:parent] || "MontageRails::Base"
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<% module_namespacing do -%>
|
2
|
+
class <%= class_name %>Resource < MontageRails::ApplicationResource
|
3
|
+
def self.schema_definition
|
4
|
+
{
|
5
|
+
name: "<%= class_name.downcase.pluralize %>",
|
6
|
+
fields: [
|
7
|
+
<% attributes.each do |attribute| -%>
|
8
|
+
{
|
9
|
+
name: "<%= attribute.name -%>",
|
10
|
+
datatype: "<%= attribute.type -%>",
|
11
|
+
},
|
12
|
+
<% end -%>
|
13
|
+
],
|
14
|
+
links: {
|
15
|
+
self: "http://testco.dev.montagehot.club/api/v1/schemas/<%= class_name.downcase.pluralize %>/",
|
16
|
+
query: "http://testco.dev.montagehot.club/api/v1/schemas/<%= class_name.downcase.pluralize %>/query/",
|
17
|
+
create_document: "http://testco.dev.montagehot.club/api/v1/schemas/<%= class_name.downcase.pluralize %>/save/"
|
18
|
+
}
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
<% end -%>
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module MontageRails
|
2
|
+
class ApplicationResource
|
3
|
+
|
4
|
+
attr_accessor :params, :data
|
5
|
+
|
6
|
+
def self.fetch_item
|
7
|
+
self.new.read_yaml.first
|
8
|
+
end
|
9
|
+
|
10
|
+
def read_yaml
|
11
|
+
YAML.load_file(class_to_filename)
|
12
|
+
end
|
13
|
+
|
14
|
+
def class_to_filename
|
15
|
+
File.join(Rails.root, "test","montage_resources","test_data",
|
16
|
+
"#{self.class.to_s.underscore.sub("_resource","")}_test_data.yml")
|
17
|
+
end
|
18
|
+
|
19
|
+
def find(id)
|
20
|
+
read_yaml.select{|item| item["id"] == id}.first
|
21
|
+
end
|
22
|
+
|
23
|
+
def query(params)
|
24
|
+
@data = self.read_yaml
|
25
|
+
@params = params
|
26
|
+
execute_filters
|
27
|
+
@data
|
28
|
+
end
|
29
|
+
|
30
|
+
def execute_filters
|
31
|
+
return unless @params["filter"]
|
32
|
+
@params["filter"].each do |filter, value|
|
33
|
+
key = filter.scan(/__(.+)/).last
|
34
|
+
key = key.last if !key.nil? # key is either nil or an array of a single string, reduce that
|
35
|
+
parameter = key.nil? ? filter : filter.chomp("__#{key}")
|
36
|
+
case key
|
37
|
+
when "ieq" #case insensitve equality
|
38
|
+
when "not" #not operator, field != value
|
39
|
+
when "contains" # value in field
|
40
|
+
when "icontains" # case insensitive version of contains
|
41
|
+
when "in" # field in value
|
42
|
+
when "notin" # field not in value
|
43
|
+
when "gt" # field > value
|
44
|
+
@data = @data.select{ |item| item[parameter] > value}
|
45
|
+
when "gte" # field >= value
|
46
|
+
@data = @data.select{ |item| item[parameter] >= value}
|
47
|
+
when "lt" # field < value
|
48
|
+
@data = @data.select{ |item| item[parameter] < value}
|
49
|
+
when "lte" # field <= value
|
50
|
+
@data = @data.select{ |item| item[parameter] <= value}
|
51
|
+
when "startswith" # field.startswith(value)
|
52
|
+
when "istartswith" # case-insensitive starts with
|
53
|
+
when "endswith" # field.endsswith(value)
|
54
|
+
when "iendswith" # case-insensitvie endswith
|
55
|
+
else #default is the 'equal to' operator
|
56
|
+
@data = @data.select{ |item| item[parameter]==value}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module MontageRails
|
4
|
+
class MockServer < Sinatra::Base
|
5
|
+
include ActiveSupport::Inflector
|
6
|
+
|
7
|
+
def fetch_schema_resource
|
8
|
+
require Rails.root.join('test','montage_resources',(params[:schema].singularize+'_resource.rb')).to_s
|
9
|
+
"#{params[:schema].singularize.classify}Resource".constantize.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def load_schema(schema)
|
13
|
+
require Rails.root.join('test','montage_resources',(schema.singularize+'_resource.rb')).to_s
|
14
|
+
"#{schema.singularize.classify}Resource".constantize.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def post_payload
|
18
|
+
return unless request.body.length > 0
|
19
|
+
request.body.rewind
|
20
|
+
JSON.parse request.body.read
|
21
|
+
end
|
22
|
+
|
23
|
+
before do
|
24
|
+
content_type :json
|
25
|
+
# puts 'request full path for method ' + request.request_method + ' is: ' + request.fullpath
|
26
|
+
end
|
27
|
+
|
28
|
+
get '/api/v1/schemas/:schema' do
|
29
|
+
fetch_schema_resource.class.schema_definition.to_json
|
30
|
+
end
|
31
|
+
|
32
|
+
post '/api/v1/schemas/:schema/query' do
|
33
|
+
data = fetch_schema_resource.query(post_payload)
|
34
|
+
return { data: data, cursors:{next:nil, previous:nil}}.to_json
|
35
|
+
end
|
36
|
+
|
37
|
+
get '/api/v1/schemas/:schema/:uuid' do
|
38
|
+
fetch_schema_resource.find(params[:uuid]).to_json
|
39
|
+
end
|
40
|
+
|
41
|
+
post '/api/v1/schemas/:schema/save' do
|
42
|
+
{ data: post_payload }.to_json
|
43
|
+
end
|
44
|
+
|
45
|
+
# get '/' do
|
46
|
+
# value = 'This is the rooooot!'
|
47
|
+
# value
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# get '/api/v1/files' do
|
51
|
+
# {data:{}}.to_json
|
52
|
+
# end
|
53
|
+
end
|
54
|
+
end
|
data/lib/montage_rails.rb
CHANGED
@@ -7,15 +7,47 @@ require 'montage_rails/version'
|
|
7
7
|
require 'montage_rails/errors'
|
8
8
|
require 'montage_rails/base'
|
9
9
|
require 'montage_rails/query_cache'
|
10
|
+
require 'montage_rails/mock_server'
|
11
|
+
require 'montage_rails/application_resource'
|
12
|
+
|
13
|
+
require 'montage_rails/railtie' if defined?(Rails)
|
10
14
|
|
11
15
|
module MontageRails
|
12
16
|
class << self
|
13
|
-
attr_accessor :username, :password, :token, :domain, :no_caching
|
17
|
+
attr_accessor :username, :password, :token, :domain, :no_caching, :use_mock_server, :server_url, :debugger
|
14
18
|
|
15
19
|
def configure
|
16
20
|
yield self
|
17
21
|
validate
|
18
22
|
get_token unless token
|
23
|
+
boot_server if use_mock_server
|
24
|
+
end
|
25
|
+
|
26
|
+
def boot_server
|
27
|
+
test_server.boot if Rails.env.test?
|
28
|
+
end
|
29
|
+
|
30
|
+
def set_url_prefix(url)
|
31
|
+
@url_prefix=url
|
32
|
+
end
|
33
|
+
|
34
|
+
def url_prefix=(value)
|
35
|
+
@url_prefix=value
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_server
|
39
|
+
require 'capybara/rails' if defined?(Rails)
|
40
|
+
@test_server ||= Capybara::Server.new(Rails.application)
|
41
|
+
end
|
42
|
+
|
43
|
+
def url_prefix
|
44
|
+
@url_prefix ||= @server_url if @server_url
|
45
|
+
|
46
|
+
if Rails.env.test? && !@server_url && @use_mock_server && !@url_prefix
|
47
|
+
@url_prefix="http://#{test_server.host}:#{test_server.port}/montage_rails_mock"
|
48
|
+
end
|
49
|
+
|
50
|
+
@url_prefix
|
19
51
|
end
|
20
52
|
|
21
53
|
def connection
|
@@ -23,6 +55,7 @@ module MontageRails
|
|
23
55
|
Montage::Client.new do |c|
|
24
56
|
c.token = token
|
25
57
|
c.domain = domain
|
58
|
+
c.url_prefix = url_prefix
|
26
59
|
end
|
27
60
|
end
|
28
61
|
end
|
data/test/dummy/config/routes.rb
CHANGED
@@ -1,56 +1,5 @@
|
|
1
|
-
|
2
|
-
# The priority is based upon order of creation: first created -> highest priority.
|
3
|
-
# See how all your routes lay out with "rake routes".
|
4
|
-
|
5
|
-
# You can have the root of your site routed with "root"
|
6
|
-
# root 'welcome#index'
|
7
|
-
|
8
|
-
# Example of regular route:
|
9
|
-
# get 'products/:id' => 'catalog#view'
|
10
|
-
|
11
|
-
# Example of named route that can be invoked with purchase_url(id: product.id)
|
12
|
-
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
|
13
|
-
|
14
|
-
# Example resource route (maps HTTP verbs to controller actions automatically):
|
15
|
-
# resources :products
|
1
|
+
require 'montage_rails/mock_server'
|
16
2
|
|
17
|
-
|
18
|
-
|
19
|
-
# member do
|
20
|
-
# get 'short'
|
21
|
-
# post 'toggle'
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# collection do
|
25
|
-
# get 'sold'
|
26
|
-
# end
|
27
|
-
# end
|
28
|
-
|
29
|
-
# Example resource route with sub-resources:
|
30
|
-
# resources :products do
|
31
|
-
# resources :comments, :sales
|
32
|
-
# resource :seller
|
33
|
-
# end
|
34
|
-
|
35
|
-
# Example resource route with more complex sub-resources:
|
36
|
-
# resources :products do
|
37
|
-
# resources :comments
|
38
|
-
# resources :sales do
|
39
|
-
# get 'recent', on: :collection
|
40
|
-
# end
|
41
|
-
# end
|
42
|
-
|
43
|
-
# Example resource route with concerns:
|
44
|
-
# concern :toggleable do
|
45
|
-
# post 'toggle'
|
46
|
-
# end
|
47
|
-
# resources :posts, concerns: :toggleable
|
48
|
-
# resources :photos, concerns: :toggleable
|
49
|
-
|
50
|
-
# Example resource route within a namespace:
|
51
|
-
# namespace :admin do
|
52
|
-
# # Directs /admin/products/* to Admin::ProductsController
|
53
|
-
# # (app/controllers/admin/products_controller.rb)
|
54
|
-
# resources :products
|
55
|
-
# end
|
3
|
+
Rails.application.routes.draw do
|
4
|
+
mount MontageRails::MockServer => "/montage_rails_mock"
|
56
5
|
end
|
@@ -1199,3 +1199,11 @@ Started GET "/montage_rails_mock/api/v1/schemas/test_ones/" for ::1 at 2015-07-1
|
|
1199
1199
|
[1m[36mFooBar Load (673.5ms)[0m [1m{:filter=>{}, :limit=>1}[0m
|
1200
1200
|
[1m[35mFooBar Load (325.2ms)[0m {:filter=>{}, :limit=>1}
|
1201
1201
|
[1m[36mMovie Load (1348.9ms)[0m [1m{:filter=>{}, :limit=>1}[0m
|
1202
|
+
[1m[36mMovie Load (8.4ms)[0m [1m{:filter=>{}, :limit=>1}[0m
|
1203
|
+
[1m[35mMovie Load (3990.5ms)[0m {:filter=>{}, :limit=>1}
|
1204
|
+
[1m[36mMovie Load (1410.1ms)[0m [1m{:filter=>{}, :order_by=>"rank", :ordering=>"asc", :limit=>10}[0m
|
1205
|
+
[1m[35mMovie Load (797.0ms)[0m {:filter=>{}, :order_by=>"rank", :ordering=>"desc", :limit=>10}
|
1206
|
+
[1m[36mMovie Load (234.9ms)[0m [1m{:filter=>{:foo=>"bar"}}[0m
|
1207
|
+
[1m[35mMovie Load (394.4ms)[0m {:filter=>{:foo=>"bar"}, :order_by=>"foo", :ordering=>"asc"}
|
1208
|
+
[1m[36mMovie Load (754.8ms)[0m [1m{:filter=>{}, :order_by=>"foo", :ordering=>"asc"}[0m
|
1209
|
+
[1m[36mMovie Load (421.9ms)[0m [1m{:filter=>{}, :limit=>1}[0m
|