diff --git a/Gemfile b/Gemfile index 8526b35f2..f5a1d3c16 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,7 @@ gem 'gon' # for passthrough of Ruby variables to Javascript variables gem 'eventmachine' gem 'amqp' gem 'logging-rails', :require => 'logging/rails' +gem 'tire' group :development, :test do gem 'rspec-rails', '2.11.0' diff --git a/app/controllers/api_genres_controller.rb b/app/controllers/api_genres_controller.rb index 74de97234..4c56f33f0 100644 --- a/app/controllers/api_genres_controller.rb +++ b/app/controllers/api_genres_controller.rb @@ -1,10 +1,9 @@ -class ApiGenresController < ApplicationController +class ApiGenresController < ApiController # have to be signed in currently to see this screen before_filter :signed_in_user - #respond_to :json, :xml - respond_to :html + respond_to :json def index @genres = Genre.find(:all) diff --git a/app/controllers/api_instruments_controller.rb b/app/controllers/api_instruments_controller.rb index a9d860063..3b644956e 100644 --- a/app/controllers/api_instruments_controller.rb +++ b/app/controllers/api_instruments_controller.rb @@ -1,10 +1,9 @@ -class ApiInstrumentsController < ApplicationController +class ApiInstrumentsController < ApiController # have to be signed in currently to see this screen before_filter :signed_in_user - #respond_to :json, :xml - respond_to :html + respond_to :json def index @instruments = Instrument.where('instruments.popularity > 0').order('instruments.popularity DESC, instruments.description ASC') diff --git a/app/controllers/api_music_sessions_controller.rb b/app/controllers/api_music_sessions_controller.rb index 00988d5d8..0cb5c6e3c 100644 --- a/app/controllers/api_music_sessions_controller.rb +++ b/app/controllers/api_music_sessions_controller.rb @@ -8,7 +8,6 @@ class ApiMusicSessionsController < ApiController def initialize @mq_router = MQRouter.new @message_factory = MessageFactory.new - end def index @@ -88,7 +87,7 @@ class ApiMusicSessionsController < ApiController raise ActiveRecord::RecordNotound end - @music_session.delete + @music_session.destroy # required to make 'tire' integration work respond_with @music_session, responder: ApiResponder end diff --git a/app/controllers/api_search_controller.rb b/app/controllers/api_search_controller.rb new file mode 100644 index 000000000..e22dfdbaa --- /dev/null +++ b/app/controllers/api_search_controller.rb @@ -0,0 +1,12 @@ +class ApiSearchController < ApiController + + # have to be signed in currently to see this screen + before_filter :signed_in_user + + respond_to :json + + def index + @search = Search.search(params[:query]) + end + +end diff --git a/app/controllers/api_users_controller.rb b/app/controllers/api_users_controller.rb index 110091cf6..09db4e923 100644 --- a/app/controllers/api_users_controller.rb +++ b/app/controllers/api_users_controller.rb @@ -45,7 +45,7 @@ class ApiUsersController < ApplicationController def delete @user = User.find(params[:id]) - @user.delete + @user.destroy # required to make 'tire' integration work respond_with @user, responder: ApiResponder end diff --git a/app/views/api_search/index.rabl b/app/views/api_search/index.rabl new file mode 100644 index 000000000..05a954b28 --- /dev/null +++ b/app/views/api_search/index.rabl @@ -0,0 +1,17 @@ +object @search + +child(:bands => :bands) { + attributes :id, :name, :location, :photo_url, :logo_url +} + +child(:musicians => :musicians) { + attributes :id, :name, :location, :photo_url +} + +child(:fans => :fans) { + attributes :id, :name, :location, :photo_url +} + +child(:recordings => :recordings) { + attributes :id, :name +} \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 0a34552b6..49fc0de3e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -71,7 +71,15 @@ module SampleApp # Add the assets/fonts directory to assets.paths config.assets.paths << "#{Rails.root}/app/assets/fonts" + # where is rabbitmq? config.rabbitmq_host = "localhost" config.rabbitmq_port = 5672 + + # where is elasticsearch? + config.elasticsearch_uri = "http://localhost:9200" + # or 'verify' or 'none'. 'autorepair will automatically rebuild the elasticsearch index on startup of rails, + # if an inconsistency is detected' + config.elasticsearch_verify_mode = "autorepair" + end end diff --git a/config/initializers/tire.rb b/config/initializers/tire.rb new file mode 100644 index 000000000..7bc8c9ef5 --- /dev/null +++ b/config/initializers/tire.rb @@ -0,0 +1,16 @@ +Tire.configure do + logger Rails.root + "log/tire_#{Rails.env}.log" + url Rails.application.config.elasticsearch_uri +end + +User.create_search_index unless User.search_index.exists? +Band.create_search_index unless Band.search_index.exists? + +# Verify elasticsearch integrity +if Rails.application.config.elasticsearch_verify_mode == "autorepair" + unless TireTasks.verify + TireTasks.rebuild_indexes + end +elsif Rails.application.config.elasticsearch_verify_mode == "verify" + TireTasks.verify +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index dcf5c0eab..157c58185 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -85,5 +85,8 @@ SampleApp::Application.routes.draw do # invitations match '/instruments/:id' => 'api_instruments#show', :via => :get, :as => 'api_instrument_detail' match '/instruments' => 'api_instruments#index', :via => :get + + # search + match '/search' => 'api_search#index', :via => :get end end diff --git a/spec/requests/music_session_pages_spec.rb b/spec/requests/music_session_pages_spec.rb index f2afb17b4..cf1aa0c9f 100644 --- a/spec/requests/music_session_pages_spec.rb +++ b/spec/requests/music_session_pages_spec.rb @@ -216,6 +216,7 @@ describe "Music Session API ", :type => :api do client = FactoryGirl.create(:connection, :user => user, :ip_address => "1.1.1.1") post '/api/sessions.json', { :description => "a session", :client_id => client.client_id, :genres => ["classical"], :musician_access => true, :tracks => [{"instrument_id" => "electric guitar", "sound" => "mom"}]}.to_json, "CONTENT_TYPE" => 'application/json' + last_response.status.should eql(422) JSON.parse(last_response.body)["errors"]["connection_tracks"][0].should == "is invalid" diff --git a/spec/requests/search_api_spec.rb b/spec/requests/search_api_spec.rb new file mode 100644 index 000000000..8ad5cc24c --- /dev/null +++ b/spec/requests/search_api_spec.rb @@ -0,0 +1,63 @@ +require 'spec_helper' + +describe "Search API ", :type => :api do + + include Rack::Test::Methods + + + describe "profile page" do + let(:user) { FactoryGirl.create(:user) } + + + before(:each) do + Band.delete_search_index + Band.create_search_index + User.delete_search_index + User.create_search_index + + post '/sessions', "session[email]" => user.email, "session[password]" => user.password + rack_mock_session.cookie_jar["remember_token"].should == user.remember_token + end + + it "empty search" do + get '/api/search.json' + last_response.status.should == 200 + JSON.parse(last_response.body).should eql(JSON.parse('{"fans" : [], "musicians" : [], "bands": [], "recordings" : []}')) + end + + it "simple search" do + User.delete_search_index # so that the user created before the test and logged in doesn't show up + User.create_search_index + @musician = User.save(name: "Peach", email: "user@example.com", + password: "foobar", password_confirmation: "foobar", musician: true) + @fan = User.save(name: "Peach Peach", email: "fan@example.com", + password: "foobar", password_confirmation: "foobar", musician: false) + @band = Band.save(name: "Peach pit", website: "www.bands.com", biography: "zomg we rock") + @band2 = Band.save(name: "Peach", website: "www.bands2.com", biography: "zomg we rock") + User.search_index.refresh + Band.search_index.refresh + + + get '/api/search.json?query=peach' + last_response.status.should == 200 + response = JSON.parse(last_response.body) + + response["musicians"].length.should == 1 + musician = response["musicians"][0] + musician["id"].should == @musician.id + + response["fans"].length.should == 1 + fan = response["fans"][0] + fan["id"].should == @fan.id + + response["bands"].length.should == 2 + band = response["bands"][0] + band["id"].should == @band2.id + band = response["bands"][1] + band["id"].should == @band.id + + response["recordings"].length.should == 0 + band = response["recordings"][0] + end + end +end