Public API for Albums and Locations

This commit is contained in:
Saurabh Bhatia 2014-03-20 17:14:16 +08:00
parent 7b2f1c090b
commit aafcfabd26
2 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,87 @@
class ApiController < ApplicationController
def index
end
def get_albums
albums = Album.all
output = Array.new
albums.each do |album|
if album.cover_path
cover_path = album.cover_path
else
cover_path = "/assets/gallery/default.jpg"
end
output << {
album_cover_file: "http://#{request.host_with_port}"+cover_path,
album_name: album.name,
album_link:"http://#{request.host_with_port}#{album_images_api_index_path(album.id)}",
}
end
render :json=>JSON.pretty_generate(output)
end
def get_images
album = Album.find(params[:id])
images = album.album_images.all
output = Array.new
images.each do |image|
if image.file.theater.present?
@image_file = image.file.theater.url
else
@image_file = image.file.url
end
output << {
image_title: image.title,
image_description: image.description,
image_file: { url: "http://#{request.host_with_port}#{@image_file}",
thumb: "http://#{request.host_with_port}#{image.file.thumb.to_s}"}}
end
render :json=>JSON.pretty_generate(output)
end
def get_location_categories
location_module = ModuleApp.where(key: "location").first
location_categories = location_module.categories.all
@data = Array.new
location_categories.each do |category|
I18n.locale = :en
name_en = category.title
I18n.locale = :zh_tw
name_zh_tw = category.title
category_id = category.id.to_s
@data << { name_en: name_en, name_zh_tw: name_zh_tw, category_id: category_id, location_link: "http://#{request.host_with_port}"+"/#{get_locations_api_index_path}"+"?category_id=#{category_id}"}
end
render :json => JSON.pretty_generate(@data)
end
def get_locations
location_infos = LocationInfo.where(:category_id => params[:category_id])
@data = Array.new
location_infos.each do |location|
picurl = location.file.blank? ? '' : "http://#{request.host_with_port + location.file.url}"
thumburl = location.file.blank? ? '' : "http://#{request.host_with_port + location.file.thumb.url}"
@data << { id: location.id.to_s,
name: location.name,
pic_url: picurl,
thumb_url: thumburl,
longitude: location.longitude,
latitude: location.latitude,
description: location.description }
end
render :json => JSON.pretty_generate(@data)
end
end

View File

@ -1,4 +1,5 @@
Orbit::Application.routes.draw do
mount Ckeditor::Engine => '/ckeditor'
# get "robots.txt" => 'robots#index'
@ -402,6 +403,19 @@ Orbit::Application.routes.draw do
# controller_paths :mobile, %w[index announcement announcement_content dialog_contact dialog_copyright dialog_language map page page_content]
# end
resources :api do
collection do
get 'get_albums'
get ':id/get_images', to: 'api#get_images', as: 'album_images'
get 'get_location_categories'
get 'get_locations'
end
member do
get 'images'
end
end
scope '/mobile(/:app)' do
match '/announcement' => 'mobile#announcement', :as => 'mobile_announcement'
match '/announcement_content/:id' => 'mobile#announcement_content', :as => 'mobile_announcement_content'