diff --git a/app/controllers/artist_controller.rb b/app/controllers/artist_controller.rb
index c2813eb..1581ba4 100644
--- a/app/controllers/artist_controller.rb
+++ b/app/controllers/artist_controller.rb
@@ -9,5 +9,20 @@ class ArtistController < ApplicationController
end
@artist = Artist.getByName(name)
end
+
+ def autocomplete
+ autocomplete = Artist.getLastFmAutocomplete(params[:query])
+ return render :nothing => true if autocomplete.nil?
+ suggestions = []
+ pp autocomplete["response"]
+ autocomplete["response"]["docs"].each do |doc|
+ suggestions << doc["artist"] unless suggestions.include?(doc["artist"])
+ end
+
+ render :json => {
+ :query => params[:query],
+ :suggestions => suggestions
+ }
+ end
end
diff --git a/app/models/artist.rb b/app/models/artist.rb
index 9fc8143..ba3292e 100644
--- a/app/models/artist.rb
+++ b/app/models/artist.rb
@@ -1,8 +1,19 @@
class Artist < ActiveRecord::Base
set_table_name 'musicbrainz.bh_artist'
has_many :albums, :conditions => "release_type = 1", :order => 'year ASC, id ASC'
-
+
def self.getByName(name)
Artist.first(:conditions => ['name = ? AND id=credit_id', name], :order => 'rating DESC')
end
+
+ def self.getLastFmAutocomplete(query)
+ return nil if query.nil? or query.strip.empty?
+
+ json = ActiveSupport::JSON.decode(open(
+ 'http://www.last.fm/search/autocomplete' <<
+ '?q=' << URI.escape(query)
+ ).read)
+ return nil if json.empty? else return json
+ end
end
+
diff --git a/app/views/main/index.html.erb b/app/views/main/index.html.erb
index 3eb1856..2acb3cf 100644
--- a/app/views/main/index.html.erb
+++ b/app/views/main/index.html.erb
@@ -1,3 +1,17 @@
+
+
SEARCH
<%= form_tag('/artist', :method => 'post') %>
diff --git a/config/routes.rb b/config/routes.rb
index e6d1ac6..341dd2d 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -59,6 +59,7 @@ Beathaven::Application.routes.draw do
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
match 'artist(/:name)' => 'artist#view'
- match 'listen/:id' => 'track#listen'
+ match 'listen/:id' => 'track#listen'
+ match 'search/autocomplete' => 'artist#autocomplete'
end
diff --git a/public/stylesheets/beathaven.css b/public/stylesheets/beathaven.css
index 9899f3d..3336b70 100644
--- a/public/stylesheets/beathaven.css
+++ b/public/stylesheets/beathaven.css
@@ -110,6 +110,7 @@ body {
}
.search #name {
+ width: 300px;
border: 1px solid ##bdbdbd;
background: #555555;
color: #ffffff;
diff --git a/public/stylesheets/search-autocomplete.css b/public/stylesheets/search-autocomplete.css
index e056d5c..ec11895 100644
--- a/public/stylesheets/search-autocomplete.css
+++ b/public/stylesheets/search-autocomplete.css
@@ -1,6 +1,7 @@
-.autocomplete-w1 { background:url(img/shadow.png) no-repeat bottom right; position:absolute; top:0px; left:0px; margin:8px 0 0 6px; /* IE6 fix: */ _background:none; _margin:0; }
+.autocomplete-w1 { background:url(../images/shadow.png) no-repeat bottom right; position:absolute; top:0px; left:0px; margin:8px 0 0 6px; /* IE6 fix: */ _background:none; _margin:0; }
.autocomplete { border:1px solid #999; background:#FFF; cursor:default; text-align:left; max-height:350px; overflow:auto; margin:-6px 6px 6px -6px; /* IE6 specific: */ _height:350px; _margin:0; _overflow-x:hidden; }
.autocomplete .selected { background:#F0F0F0; }
.autocomplete div { padding:2px 5px; white-space:nowrap; }
.autocomplete strong { font-weight:normal; color:#3399FF; }
+