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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/montage_rails/model_generator.rb +49 -0
  3. data/lib/generators/montage_rails/templates/model.rb +5 -0
  4. data/lib/generators/montage_rails/templates/resource.rb +22 -0
  5. data/lib/generators/montage_rails/templates/test_data.rb +6 -0
  6. data/lib/montage_rails/application_resource.rb +63 -0
  7. data/lib/montage_rails/mock_server.rb +54 -0
  8. data/lib/montage_rails/railtie.rb +5 -0
  9. data/lib/montage_rails/version.rb +1 -1
  10. data/lib/montage_rails.rb +34 -1
  11. data/test/dummy/app/models/movie.rb +1 -0
  12. data/test/dummy/app/models/test_mod.rb +3 -0
  13. data/test/dummy/app/models/test_one.rb +3 -0
  14. data/test/dummy/config/initializers/montage.rb +1 -0
  15. data/test/dummy/config/routes.rb +3 -54
  16. data/test/dummy/log/development.log +8 -0
  17. data/test/dummy/log/test.log +4074 -0
  18. data/test/dummy/test/fixtures/actors.yml +9 -0
  19. data/test/dummy/test/fixtures/movies.yml +17 -0
  20. data/test/dummy/test/fixtures/studios.yml +7 -0
  21. data/test/dummy/test/fixtures/test_mods.yml +9 -0
  22. data/test/dummy/test/fixtures/test_ones.yml +11 -0
  23. data/test/dummy/test/models/actor_test.rb +7 -0
  24. data/test/dummy/test/models/movie_test.rb +7 -0
  25. data/test/dummy/test/models/studio_test.rb +7 -0
  26. data/test/dummy/test/models/test_mod_test.rb +5 -0
  27. data/test/dummy/test/models/test_one_test.rb +7 -0
  28. data/test/dummy/test/montage_resources/actor_resource.rb +26 -0
  29. data/test/dummy/test/montage_resources/movie_resource.rb +43 -0
  30. data/test/dummy/test/montage_resources/studio_resource.rb +18 -0
  31. data/test/dummy/test/montage_resources/test_data/actor_test_data.yml +8 -0
  32. data/test/dummy/test/montage_resources/test_data/movie_test_data.yml +8 -0
  33. data/test/dummy/test/montage_resources/test_data/studio_test_data.yml +3 -0
  34. data/test/dummy/test/resources/test_mod_resource.rb +22 -0
  35. data/test/dummy/test/resources/test_one_resource.rb +26 -0
  36. data/test/dummy/test/test_helper.rb +0 -0
  37. data/test/montage_rails/application_resource_test.rb +92 -0
  38. data/test/montage_rails/base_test.rb +22 -22
  39. data/test/montage_rails/relation_test.rb +1 -1
  40. data/test/montage_rails_test.rb +57 -14
  41. data/test/test_helper.rb +9 -166
  42. metadata +108 -22
  43. data/test/resources/actor_resource.rb +0 -141
  44. data/test/resources/movie_resource.rb +0 -221
  45. data/test/resources/studio_resource.rb +0 -56
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46ce8bab509be091652298c3b0dfd98b36d5c4f4
4
- data.tar.gz: 2aab9728cc4920f5e017587a53563f477acffef5
3
+ metadata.gz: 521228f95db5309b934fe1f0a862a9bc58760796
4
+ data.tar.gz: 4c8c2ebba95259f43432b709467f9d15ffc266bf
5
5
  SHA512:
6
- metadata.gz: 6b6ff26988a6d5f3d0087f564cc061639b0a70752258486e77c3f145e7c1cb60328849847312827df51391bbf1be24fbf17555d464ecaa48de4ba9a7b6f628ea
7
- data.tar.gz: 25ce4d25f278bffbed45643195f81b1edbf1d7e95adb9abc57e5a601bbb8638b44ca8eff4bf08e57509e237cc5a67895fdd17ad21cbedbefd32909ac280bb211
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,5 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %> < <%= parent_class_name.classify %>
3
+
4
+ end
5
+ <% 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,6 @@
1
+ <% 10.times do |i| -%>
2
+ -
3
+ <% attributes.each do |attribute| -%>
4
+ <%= attribute.name -%>: <%= random_for_type(attribute.type) %>
5
+ <% end -%>
6
+ <% 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
@@ -0,0 +1,5 @@
1
+ module MontageRails
2
+ class Railtie < Rails::Railtie
3
+
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module MontageRails
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  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
@@ -29,4 +29,5 @@ class Movie < MontageRails::Base
29
29
  self.votes = 600
30
30
  save!
31
31
  end
32
+
32
33
  end
@@ -0,0 +1,3 @@
1
+ class TestMod < MontageRails::Base
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ class TestOne < MontageRails::Base
2
+
3
+ end
@@ -2,4 +2,5 @@ MontageRails.configure do |c|
2
2
  c.token = "fb761e07-a12b-40bb-a42f-2202ecfd1046"
3
3
  c.domain = "testco"
4
4
  c.no_caching = true
5
+ c.use_mock_server = true
5
6
  end
@@ -1,56 +1,5 @@
1
- Rails.application.routes.draw do
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
- # Example resource route with options:
18
- # resources :products do
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
  FooBar Load (673.5ms) {:filter=>{}, :limit=>1}
1200
1200
  FooBar Load (325.2ms) {:filter=>{}, :limit=>1}
1201
1201
  Movie Load (1348.9ms) {:filter=>{}, :limit=>1}
1202
+ Movie Load (8.4ms) {:filter=>{}, :limit=>1}
1203
+ Movie Load (3990.5ms) {:filter=>{}, :limit=>1}
1204
+ Movie Load (1410.1ms) {:filter=>{}, :order_by=>"rank", :ordering=>"asc", :limit=>10}
1205
+ Movie Load (797.0ms) {:filter=>{}, :order_by=>"rank", :ordering=>"desc", :limit=>10}
1206
+ Movie Load (234.9ms) {:filter=>{:foo=>"bar"}}
1207
+ Movie Load (394.4ms) {:filter=>{:foo=>"bar"}, :order_by=>"foo", :ordering=>"asc"}
1208
+ Movie Load (754.8ms) {:filter=>{}, :order_by=>"foo", :ordering=>"asc"}
1209
+ Movie Load (421.9ms) {:filter=>{}, :limit=>1}