diff --git a/lib/noosfero/api/api.rb b/lib/noosfero/api/api.rb new file mode 100644 index 0000000..de2024d --- /dev/null +++ b/lib/noosfero/api/api.rb @@ -0,0 +1,107 @@ +require 'grape' +#require 'rack/contrib' +Dir["#{Rails.root}/lib/noosfero/api/*.rb"].each {|file| require file unless file =~ /api\.rb/} + +module Noosfero + module API + + class Federation < Grape::API + format :json + content_type :txt, "text/plain" + + #REMOVE ME - testing routes only + get "/me" do + present_partial current_person, :with => Entities::Person, :current_person => current_person + end + end + + class BaseAPI < Grape::API + before { set_locale } + before { setup_multitenancy } + before { detect_stuff_by_domain } + before { filter_disabled_plugins_endpoints } + before { init_noosfero_plugins } + after { set_session_cookie } + + version 'v1' + prefix [ENV['RAILS_RELATIVE_URL_ROOT'], "api"].compact.join('/') + format :json + content_type :txt, "text/plain" + + mount V1::Articles + mount V1::Comments + mount V1::Users + mount V1::Communities + mount V1::People + mount V1::Enterprises + mount V1::Categories + mount V1::Tasks + mount V1::Tags + mount V1::Environments + mount V1::Search + mount V1::Contacts + mount V1::Boxes + mount V1::Profiles + mount V1::Activities + mount Session + end + + class API < Grape::API + use Rack::JSONP + helpers APIHelpers + + logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log")) + logger.formatter = GrapeLogging::Formatters::Default.new + #use GrapeLogging::Middleware::RequestLogger, { logger: logger } + + rescue_from :all do |e| + logger.error e + error! e.message, 500 + end + + @@NOOSFERO_CONF = nil + def self.NOOSFERO_CONF + if @@NOOSFERO_CONF + @@NOOSFERO_CONF + else + file = Rails.root.join('config', 'noosfero.yml') + @@NOOSFERO_CONF = File.exists?(file) ? YAML.load_file(file)[Rails.env] || {} : {} + end + end + + mount Noosfero::API::BaseAPI + mount Noosfero::API::Federation + + # hook point which allow plugins to add Grape::API extensions to API::API + #finds for plugins which has api mount points classes defined (the class should extends Grape::API) + @plugins = Noosfero::Plugin.all.map { |p| p.constantize } + @plugins.each do |klass| + if klass.public_methods.include? :api_mount_points + klass.api_mount_points.each do |mount_class| + mount mount_class if mount_class && ( mount_class < Grape::API ) + end + end + end + + def self.endpoint_unavailable?(endpoint, environment) + api_class = endpoint.options[:app] || endpoint.options[:for] + if api_class.present? + klass = api_class.name.deconstantize.constantize + return klass < Noosfero::Plugin && !environment.plugin_enabled?(klass) + end + end + + class << self + def endpoints_with_plugins(environment = nil) + if environment.present? + cloned_endpoints = endpoints_without_plugins.dup + cloned_endpoints.delete_if { |endpoint| endpoint_unavailable?(endpoint, environment) } + else + endpoints_without_plugins + end + end + alias_method_chain :endpoints, :plugins + end + end + end +end -- libgit2 0.21.2