From aafcfabd266d38456de853c48d91f35f35249a4d Mon Sep 17 00:00:00 2001 From: Saurabh Bhatia Date: Thu, 20 Mar 2014 17:14:16 +0800 Subject: [PATCH] Public API for Albums and Locations --- app/controllers/api_controller.rb | 87 +++++++++++++++++++++++++++++++ config/routes.rb | 14 +++++ 2 files changed, 101 insertions(+) create mode 100644 app/controllers/api_controller.rb diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb new file mode 100644 index 0000000..955f2c7 --- /dev/null +++ b/app/controllers/api_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index baac9f1..b1c81d0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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'