-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchordsearch.rb
60 lines (50 loc) · 1.36 KB
/
chordsearch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'sinatra'
require 'haml'
require 'json'
require 'cgi'
set :app_file, __FILE__
enable :static
require './lib/chord'
require './lib/instruments'
require './lib/chord_db'
require './lib/chord_collection'
get '/' do
haml :index
end
get %r{^/(\w+)$} do |instrument|
redirect "/#{instrument}/"
end
get %r{^/collection/([^/]+)/?$} do |collection|
@collection = ChordCollection.find(collection)
end
get %r{^/(\w+)/$} do |instrument|
redirect '/' unless ChordDB[instrument]
@query = {}
@search_chord = ChordDB[instrument].new
@chords = []
@collection = collection
haml :chords
end
get %r{^/(\w+)/(.*\.json)$} do |instrument, q|
query = ChordDB.query_from_param(q)
ChordDB.find_chords(query, instrument).to_json
end
get %r{^/(\w+)/([^/]+)/add/([^/]+)/(.*)$} do |instrument, q, collection, chord_key|
@collection = ChordCollection.find(collection) || ChordCollection.new('name' => collection)
@collection << chord_key
@collection.save
redirect "/#{instrument}/#{q}?c=#{collection}"
end
get %r{^/(\w+)/(.*)$} do |instrument, q|
@query = ChordDB.query_from_param(q)
@search_chord = ChordDB[instrument].search_chord(@query)
@chords = ChordDB.find_chords(@query, instrument)
@collection = collection
haml :chords
end
def collection
if params['c']
ChordCollection.find(params['c']) ||
ChordCollection.new('name' => params['c'])
end
end