lib/carte/server.rb in carte-server-0.0.14 vs lib/carte/server.rb in carte-server-0.0.15
- old
+ new
@@ -19,11 +19,10 @@
set :protection, :except => :path_traversal
set :views, File.join(File.dirname(__FILE__), 'server/views')
set :public_folder, 'public'
set :default, JSON.parse(File.read(File.join(File.dirname(__FILE__), 'shared/default.json')))
set :carte, {}
- set :script_path, '/app.js'
end
helpers do
def config
@config ||= settings.default.update(settings.carte)
@@ -59,83 +58,71 @@
renderer = Redcarpet::Render::HTML.new(filter_html:true)
Redcarpet::Markdown.new(renderer, autolink: true).render(markdown)
end
end
- get '/' do
- haml :index
+ get '/cards.xml' do
+ @cards = search(params)
+ builder :cards
end
-
- get '/app.js' do
- cache_control :public
- send_file settings.carte['script_path']
+
+ get '/cards.json' do
+ cards = search(params)
+ if cards.respond_to?(:current_page) && cards.respond_to?(:total_pages)
+ current_page = cards.current_page.to_i
+ total_pages = cards.total_pages
+ end
+ cards = cards.map {|card| {id: card.id, title: card.title, content: card.content, version: card.version, tags: card.tags}}
+ {cards: cards, page: {current: current_page, total: total_pages}}.to_json
end
+
+ get '/cards/:title.json' do
+ card = Card.where(title: params[:title]).first
+ halt 404 if card.nil?
+ {card: {id: card.id, title: card.title, content: card.content, version: card.version, tags: card.tags, lefts: card.lefts(4), rights: card.rights(4)}}.to_json
+ end
- namespace '/api' do
- get '/cards.xml' do
- @cards = search(params)
- builder :cards
+ post '/cards.json' do
+ card = Card.new(json_data.slice('title', 'content', 'tags'))
+ if card.save
+ status 201
+ {card: {id: card.id}}.to_json
+ else
+ status 400
+ {card: {errors: card.errors}}.to_json
end
-
- get '/cards.json' do
- cards = search(params)
- if cards.respond_to?(:current_page) && cards.respond_to?(:total_pages)
- current_page = cards.current_page.to_i
- total_pages = cards.total_pages
- end
- cards = cards.map {|card| {id: card.id, title: card.title, content: card.content, version: card.version, tags: card.tags}}
- {cards: cards, page: {current: current_page, total: total_pages}}.to_json
- end
-
- get '/cards/:title.json' do
- card = Card.where(title: params[:title]).first
- halt 404 if card.nil?
- {card: {id: card.id, title: card.title, content: card.content, version: card.version, tags: card.tags, lefts: card.lefts(4), rights: card.rights(4)}}.to_json
- end
-
- post '/cards.json' do
- card = Card.new(json_data.slice('title', 'content', 'tags'))
- if card.save
- status 201
- {card: {id: card.id}}.to_json
- else
- status 400
- {card: {errors: card.errors}}.to_json
- end
- end
+ end
- put '/cards/:title.json' do
- card = Card.where(title: params[:title]).first
- halt 404 if card.nil?
- card.histories.create!
- p json_data.slice('new_title', 'content', 'tags').compact
- if card.update_attributes(json_data.slice('new_title', 'content', 'tags').compact)
- status 201
- {}.to_json
- else
- status 400
- {card: {errors: card.errors}}.to_json
- end
+ put '/cards/:title.json' do
+ card = Card.where(title: params[:title]).first
+ halt 404 if card.nil?
+ card.histories.create!
+ if card.update_attributes(json_data.slice('new_title', 'content', 'tags').compact)
+ status 201
+ {}.to_json
+ else
+ status 400
+ {card: {errors: card.errors}}.to_json
end
-
- #delete '/cards/:title.json' do
- # card = Card.where(title: params[:title]).first
- # halt 404 if card.nil?
- # card.destroy
- #end
-
- get '/cards/:title/history.json' do
- card = Card.where(title: params[:title]).first
- halt 404 if card.nil?
- {history: card.histories}.to_json
- end
+ end
+
+ #delete '/cards/:title.json' do
+ # card = Card.where(title: params[:title]).first
+ # halt 404 if card.nil?
+ # card.destroy
+ #end
+
+ get '/cards/:title/history.json' do
+ card = Card.where(title: params[:title]).first
+ halt 404 if card.nil?
+ {history: card.histories}.to_json
+ end
- get '/tags.json' do
- {tags: Card.all_tags}.to_json
- end
+ get '/tags.json' do
+ {tags: Card.all_tags}.to_json
+ end
- error(404) do
- {}.to_json
- end
+ error(404) do
+ {}.to_json
end
end
end