Commit b09f337ebcc382d115ea501c579176656f2bec66

Authored by Victor Costa
2 parents 68a0e942 cf2b8ce4

Merge branch 'api' into stable

Conflicts:
	Gemfile
Gemfile
... ... @@ -24,10 +24,6 @@ gem 'locale', '~> 2.0.5'
24 24 # FIXME list here all actual dependencies (i.e. the ones in debian/control),
25 25 # with their GEM names (not the Debian package names)
26 26  
27   -group :development do
28   - gem 'bullet'
29   -end
30   -
31 27 group :production do
32 28 gem 'dalli', '~> 2.7.0'
33 29 end
... ...
lib/api/api.rb
... ... @@ -3,6 +3,11 @@ Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file}
3 3  
4 4 module API
5 5 class API < Grape::API
  6 + before { start_log }
  7 + before { setup_multitenancy }
  8 + before { detect_stuff_by_domain }
  9 + after { end_log }
  10 +
6 11 version 'v1'
7 12 prefix "api"
8 13 format :json
... ... @@ -19,5 +24,15 @@ module API
19 24 mount V1::Categories
20 25 mount Session
21 26  
  27 + # hook point which allow plugins to add Grape::API extensions to API::API
  28 + #finds for plugins which has api mount points classes defined (the class should extends Grape::API)
  29 + @plugins = Noosfero::Plugin.all.map { |p| p.constantize }
  30 + @plugins.each do |klass|
  31 + if klass.public_methods.include? 'api_mount_points'
  32 + klass.api_mount_points.each do |mount_class|
  33 + mount mount_class if mount_class && ( mount_class < Grape::API )
  34 + end
  35 + end
  36 + end
22 37 end
23 38 end
... ...
lib/api/helpers.rb
... ... @@ -3,7 +3,7 @@ module API
3 3 PRIVATE_TOKEN_PARAM = :private_token
4 4  
5 5 def logger
6   - API.logger
  6 + @logger ||= Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_api.log"))
7 7 end
8 8  
9 9 def current_user
... ... @@ -166,6 +166,17 @@ module API
166 166 end
167 167 protected
168 168  
  169 + def start_log
  170 + logger.info "Started #{request.path}"
  171 + end
  172 + def end_log
  173 + logger.info "Completed #{request.path}"
  174 + end
  175 +
  176 + def setup_multitenancy
  177 + Noosfero::MultiTenancy.setup!(request.host)
  178 + end
  179 +
169 180 def detect_stuff_by_domain
170 181 @domain = Domain.find_by_name(request.host)
171 182 if @domain.nil?
... ...
lib/api/v1/articles.rb
1 1 module API
2 2 module V1
3 3 class Articles < Grape::API
4   - before { detect_stuff_by_domain }
5 4 before { authenticate! }
6 5  
7 6 resource :articles do
... ... @@ -19,7 +18,6 @@ module API
19 18 # :params => API::Entities::Article.documentation
20 19 # }
21 20 get do
22   -
23 21 articles = select_filtered_collection_of(environment, 'articles', params)
24 22 present articles, :with => Entities::Article
25 23 end
... ...
lib/api/v1/categories.rb
1 1 module API
2 2 module V1
3 3 class Categories < Grape::API
4   - before { detect_stuff_by_domain }
5 4 before { authenticate! }
6 5  
7 6 resource :categories do
... ...
lib/api/v1/comments.rb
1 1 module API
2 2 module V1
3 3 class Comments < Grape::API
4   -
5   - before { detect_stuff_by_domain }
6 4 before { authenticate! }
7 5  
8 6 resource :articles do
... ...
lib/api/v1/communities.rb
1 1 module API
2 2 module V1
3 3 class Communities < Grape::API
4   - before { detect_stuff_by_domain }
5 4 before { authenticate! }
6 5  
7 6 resource :communities do
... ...
lib/api/v1/enterprises.rb
1 1 module API
2 2 module V1
3 3 class Enterprises < Grape::API
4   - before { detect_stuff_by_domain }
5 4 before { authenticate! }
6 5  
7 6 resource :enterprises do
... ...
lib/api/v1/people.rb
1 1 module API
2 2 module V1
3 3 class People < Grape::API
4   - before { detect_stuff_by_domain }
5 4 before { authenticate! }
6 5  
7 6 resource :people do
... ...
lib/api/v1/users.rb
1 1 module API
2 2 module V1
3 3 class Users < Grape::API
4   -
5   - before { detect_stuff_by_domain }
6 4 before { authenticate! }
7 5  
8 6 resource :users do
... ...
lib/noosfero/plugin.rb
... ... @@ -161,6 +161,10 @@ class Noosfero::Plugin
161 161 def has_admin_url?
162 162 File.exists?(File.join(root_path, 'controllers', "#{name.underscore}_admin_controller.rb"))
163 163 end
  164 +
  165 + # -> define grape class used to map resource api provided by the plugin
  166 + def api_mount_points
  167 + end
164 168 end
165 169  
166 170 def expanded_template(file_path, locals = {})
... ...