From 67f84ed15478b7cb6db9b892bbab4e7f20416be1 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Wed, 15 Apr 2015 09:26:36 -0300 Subject: [PATCH] Remove old api files --- lib/api/api.rb | 38 -------------------------------------- lib/api/entities.rb | 87 --------------------------------------------------------------------------------------- lib/api/helpers.rb | 201 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- lib/api/session.rb | 44 -------------------------------------------- lib/api/v1/articles.rb | 85 ------------------------------------------------------------------------------------- lib/api/v1/categories.rb | 23 ----------------------- lib/api/v1/comments.rb | 42 ------------------------------------------ lib/api/v1/communities.rb | 38 -------------------------------------- lib/api/v1/enterprises.rb | 32 -------------------------------- lib/api/v1/people.rb | 32 -------------------------------- lib/api/v1/users.rb | 31 ------------------------------- 11 files changed, 0 insertions(+), 653 deletions(-) delete mode 100644 lib/api/api.rb delete mode 100644 lib/api/entities.rb delete mode 100644 lib/api/helpers.rb delete mode 100644 lib/api/session.rb delete mode 100644 lib/api/v1/articles.rb delete mode 100644 lib/api/v1/categories.rb delete mode 100644 lib/api/v1/comments.rb delete mode 100644 lib/api/v1/communities.rb delete mode 100644 lib/api/v1/enterprises.rb delete mode 100644 lib/api/v1/people.rb delete mode 100644 lib/api/v1/users.rb diff --git a/lib/api/api.rb b/lib/api/api.rb deleted file mode 100644 index fe39ea3..0000000 --- a/lib/api/api.rb +++ /dev/null @@ -1,38 +0,0 @@ -require 'grape' -Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} - -module API - class API < Grape::API - before { start_log } - before { setup_multitenancy } - before { detect_stuff_by_domain } - after { end_log } - - version 'v1' - prefix "api" - format :json - content_type :txt, "text/plain" - - helpers APIHelpers - - mount V1::Articles - mount V1::Comments - mount V1::Users - mount V1::Communities - mount V1::People - mount V1::Enterprises - mount V1::Categories - mount Session - - # 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 - end -end diff --git a/lib/api/entities.rb b/lib/api/entities.rb deleted file mode 100644 index e87cf87..0000000 --- a/lib/api/entities.rb +++ /dev/null @@ -1,87 +0,0 @@ -module API - module Entities - - Grape::Entity.format_with :timestamp do |date| - date.strftime('%Y/%m/%d %H:%M:%S') if date - end - - class Image < Grape::Entity - root 'images', 'image' - - expose :icon_url do |image, options| - image.public_filename(:icon) - end - - expose :minor_url do |image, options| - image.public_filename(:minor) - end - - expose :portrait_url do |image, options| - image.public_filename(:portrait) - end - - expose :thumb_url do |image, options| - image.public_filename(:thumb) - end - end - - class Profile < Grape::Entity - expose :identifier, :name, :id - expose :created_at, :format_with => :timestamp - expose :image, :using => Image - end - - class Person < Profile;end; - class Enterprise < Profile;end; - class Community < Profile - root 'communities', 'community' - expose :description - end - - class Category < Grape::Entity - root 'categories', 'category' - expose :name, :id, :slug - expose :image, :using => Image - end - - - class Article < Grape::Entity - root 'articles', 'article' - expose :id, :body - expose :created_at, :format_with => :timestamp - expose :title, :documentation => {:type => "String", :desc => "Title of the article"} - expose :created_by, :as => :author, :using => Profile - expose :profile, :using => Profile - expose :categories, :using => Category - end - - class Comment < Grape::Entity - root 'comments', 'comment' - expose :body, :title, :id - expose :created_at, :format_with => :timestamp - expose :author, :using => Profile - end - - - class User < Grape::Entity - root 'users', 'user' - expose :id - expose :login - expose :person, :using => Profile - expose :permissions do |user, options| - output = {} - user.person.role_assignments.map do |role_assigment| - if role_assigment.resource.respond_to?(:identifier) - output[role_assigment.resource.identifier] = role_assigment.role.permissions - end - end - output - end - end - - class UserLogin < User - expose :private_token - end - - end -end diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb deleted file mode 100644 index f856b32..0000000 --- a/lib/api/helpers.rb +++ /dev/null @@ -1,201 +0,0 @@ -module API - module APIHelpers - PRIVATE_TOKEN_PARAM = :private_token - - def logger - @logger ||= Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_api.log")) - end - - def current_user - private_token = params[PRIVATE_TOKEN_PARAM].to_s - @current_user ||= User.find_by_private_token(private_token) - @current_user = nil if !@current_user.nil? && @current_user.private_token_expired? - @current_user - end - - def current_person - current_user.person unless current_user.nil? - end - - def logout - @current_user = nil - end - - def environment - @environment - end - - def limit - limit = params[:limit].to_i - limit = default_limit if limit <= 0 - limit - end - - def period(from_date, until_date) - begin_period = from_date.nil? ? Time.at(0).to_datetime : from_date - end_period = until_date.nil? ? DateTime.now : until_date - - begin_period...end_period - end - - def parse_content_type(content_type) - return nil if content_type.blank? - content_type.split(',').map do |content_type| - content_type.camelcase - end - end - - def make_conditions_with_parameter(params = {}) - conditions = {} - from_date = DateTime.parse(params[:from]) if params[:from] - until_date = DateTime.parse(params[:until]) if params[:until] - - conditions[:type] = parse_content_type(params[:content_type]) unless params[:content_type].nil? - - conditions[:created_at] = period(from_date, until_date) if from_date || until_date - - conditions - end - - - def select_filtered_collection_of(object, method, params) - conditions = make_conditions_with_parameter(params) - - if params[:reference_id] - objects = object.send(method).send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - else - objects = object.send(method).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - end - objects - end - -#FIXME see if its needed -# def paginate(relation) -# per_page = params[:per_page].to_i -# paginated = relation.page(params[:page]).per(per_page) -# add_pagination_headers(paginated, per_page) -# -# paginated -# end - - def authenticate! - unauthorized! unless current_user - end - -#FIXME see if its needed -# def authenticated_as_admin! -# forbidden! unless current_user.is_admin? -# end -# -#FIXME see if its needed -# def authorize! action, subject -# unless abilities.allowed?(current_user, action, subject) -# forbidden! -# end -# end -# -#FIXME see if its needed -# def can?(object, action, subject) -# abilities.allowed?(object, action, subject) -# end - - # Checks the occurrences of required attributes, each attribute must be present in the params hash - # or a Bad Request error is invoked. - # - # Parameters: - # keys (required) - A hash consisting of keys that must be present - def required_attributes!(keys) - keys.each do |key| - bad_request!(key) unless params[key].present? - end - end - - # Checks the occurrences of uniqueness of attributes, each attribute must be present in the params hash - # or a Bad Request error is invoked. - # - # Parameters: - # keys (unique) - A hash consisting of keys that must be unique - def unique_attributes!(obj, keys) - keys.each do |key| - cant_be_saved_request!(key) if obj.send("find_by_#{key.to_s}", params[key]) - end - end - - def attributes_for_keys(keys) - attrs = {} - keys.each do |key| - attrs[key] = params[key] if params[key].present? or (params.has_key?(key) and params[key] == false) - end - attrs - end - - # error helpers - def forbidden! - render_api_error!('403 Forbidden', 403) - end - - def cant_be_saved_request!(attribute) - message = _("(Invalid request) #{attribute} can't be saved") - render_api_error!(message, 400) - end - - def bad_request!(attribute) - message = _("(Bad request) #{attribute} not given") - render_api_error!(message, 400) - end - - def something_wrong! - message = _("Something wrong happened") - render_api_error!(message, 400) - end - - def unauthorized! - render_api_error!(_('Unauthorized'), 401) - end - - def not_allowed! - render_api_error!(_('Method Not Allowed'), 405) - end - - def render_api_error!(message, status) - error!({'message' => message, :code => status}, status) - end - - def render_api_errors!(messages) - render_api_error!(messages.join(','), 400) - end - protected - - def start_log - logger.info "Started #{request.path} #{request.params.except('password')}" - end - def end_log - logger.info "Completed #{request.path}" - end - - def setup_multitenancy - Noosfero::MultiTenancy.setup!(request.host) - end - - def detect_stuff_by_domain - @domain = Domain.find_by_name(request.host) - if @domain.nil? - @environment = Environment.default - if @environment.nil? && Rails.env.development? - # This should only happen in development ... - @environment = Environment.create!(:name => "Noosfero", :is_default => true) - end - else - @environment = @domain.environment - end - end - - private - - def default_limit - 20 - end - - - end -end diff --git a/lib/api/session.rb b/lib/api/session.rb deleted file mode 100644 index 163d6e1..0000000 --- a/lib/api/session.rb +++ /dev/null @@ -1,44 +0,0 @@ -module API - - class Session < Grape::API - - # Login to get token - # - # Parameters: - # login (*required) - user login or email - # password (required) - user password - # - # Example Request: - # POST /login?login=some&password=pass - post "/login" do - user ||= User.authenticate(params[:login], params[:password], environment) - - return unauthorized! unless user - user.generate_private_token! - present user, :with => Entities::UserLogin - end - - # Create user. - # - # Parameters: - # email (required) - Email - # password (required) - Password - # login - login - # Example Request: - # POST /register?email=some@mail.com&password=pas&login=some - post "/register" do - required_attributes! [:email, :login, :password] - unique_attributes! User, [:email, :login] - attrs = attributes_for_keys [:email, :login, :password] - attrs[:password_confirmation] = attrs[:password] - user = User.new(attrs) - if user.save - user.activate - present user, :with => Entities::User - else - something_wrong! - end - end - - end -end diff --git a/lib/api/v1/articles.rb b/lib/api/v1/articles.rb deleted file mode 100644 index c8d3b88..0000000 --- a/lib/api/v1/articles.rb +++ /dev/null @@ -1,85 +0,0 @@ -module API - module V1 - class Articles < Grape::API - before { authenticate! } - - resource :articles do - - # Collect comments from articles - # - # Parameters: - # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created - # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected - # limit - amount of comments returned. The default value is 20 - # - # Example Request: - # GET /api/v1/articles?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10&content_type=Hub -# desc 'Articles.', { -# :params => API::Entities::Article.documentation -# } - get do - articles = select_filtered_collection_of(environment, 'articles', params) - present articles, :with => Entities::Article - end - - desc "Return the article id" - get ':id' do - present environment.articles.find(params[:id]), :with => Entities::Article - end - - get ':id/children' do - - conditions = make_conditions_with_parameter(params) - if params[:reference_id] - articles = environment.articles.find(params[:id]).children.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - else - articles = environment.articles.find(params[:id]).children.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - end - present articles, :with => Entities::Article - end - - get ':id/children/:child_id' do - present environment.articles.find(params[:id]).children.find(params[:child_id]), :with => Entities::Article - end - - - end - - resource :communities do - segment '/:community_id' do - resource :articles do - get do - community = environment.communities.find(params[:community_id]) - articles = select_filtered_collection_of(community, 'articles', params) - present articles, :with => Entities::Article - end - - get '/:id' do - community = environment.communities.find(params[:community_id]) - present community.articles.find(params[:id]), :with => Entities::Article - end - - # Example Request: - # POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body - post do - community = environment.communities.find(params[:community_id]) - klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] - article = klass_type.constantize.new(params[:article]) - article.last_changed_by = current_person - article.created_by= current_person - article.profile = community - - if !article.save - render_api_errors!(article.errors.full_messages) - end - present article, :with => Entities::Article - end - - end - end - - end - - end - end -end diff --git a/lib/api/v1/categories.rb b/lib/api/v1/categories.rb deleted file mode 100644 index 8250bca..0000000 --- a/lib/api/v1/categories.rb +++ /dev/null @@ -1,23 +0,0 @@ -module API - module V1 - class Categories < Grape::API - before { authenticate! } - - resource :categories do - - get do - type = params[:category_type] - categories = type.nil? ? environment.categories : environment.categories.find(:all, :conditions => {:type => type}) - present categories, :with => Entities::Category - end - - desc "Return the category by id" - get ':id' do - present environment.categories.find(params[:id]), :with => Entities::Category - end - - end - - end - end -end diff --git a/lib/api/v1/comments.rb b/lib/api/v1/comments.rb deleted file mode 100644 index 42c6f4b..0000000 --- a/lib/api/v1/comments.rb +++ /dev/null @@ -1,42 +0,0 @@ -module API - module V1 - class Comments < Grape::API - before { authenticate! } - - resource :articles do - # Collect comments from articles - # - # Parameters: - # reference_id - comment id used as reference to collect comment - # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected - # limit - amount of comments returned. The default value is 20 - # - # Example Request: - # GET /articles/12/comments?oldest&limit=10&reference_id=23 - get ":id/comments" do - - conditions = make_conditions_with_parameter(params) - - if params[:reference_id] - comments = environment.articles.find(params[:id]).comments.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit) - else - comments = environment.articles.find(params[:id]).comments.reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit) - end - present comments, :with => Entities::Comment - - end - - get ":id/comments/:comment_id" do - present environment.articles.find(params[:id]).comments.find(params[:comment_id]), :with => Entities::Comment - end - - # Example Request: - # POST api/v1/articles/12/comments?private_toke=234298743290432&body=new comment - post ":id/comments" do - present environment.articles.find(params[:id]).comments.create(:author => current_person, :body => params[:body]), :with => Entities::Comment - end - end - - end - end -end diff --git a/lib/api/v1/communities.rb b/lib/api/v1/communities.rb deleted file mode 100644 index f19f689..0000000 --- a/lib/api/v1/communities.rb +++ /dev/null @@ -1,38 +0,0 @@ -module API - module V1 - class Communities < Grape::API - before { authenticate! } - - resource :communities do - - # Collect comments from articles - # - # Parameters: - # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created - # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected - # limit - amount of comments returned. The default value is 20 - # - # Example Request: - # GET /communities?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 - # GET /communities?reference_id=10&limit=10&oldest - get do - communities = select_filtered_collection_of(current_person, 'communities', params) - present communities, :with => Entities::Community - end - - #FIXME See only public communities - get '/all' do - communities = select_filtered_collection_of(environment, 'communities', params) - present communities, :with => Entities::Community - end - - get ':id' do - community = environment.communities.find(params[:id]) - present community, :with => Entities::Community - end - - end - - end - end -end diff --git a/lib/api/v1/enterprises.rb b/lib/api/v1/enterprises.rb deleted file mode 100644 index ea0d765..0000000 --- a/lib/api/v1/enterprises.rb +++ /dev/null @@ -1,32 +0,0 @@ -module API - module V1 - class Enterprises < Grape::API - before { authenticate! } - - resource :enterprises do - - # Collect comments from articles - # - # Parameters: - # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created - # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected - # limit - amount of comments returned. The default value is 20 - # - # Example Request: - # GET /enterprises?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 - # GET /enterprises?reference_id=10&limit=10&oldest - get do - enterprises = select_filtered_collection_of(environment, 'enterprises', params) - present enterprises, :with => Entities::Enterprise - end - - desc "Return the article id" - get ':id' do - present environment.enterprises.find(params[:id]), :with => Entities::Enterprise - end - - end - - end - end -end diff --git a/lib/api/v1/people.rb b/lib/api/v1/people.rb deleted file mode 100644 index 56e55ab..0000000 --- a/lib/api/v1/people.rb +++ /dev/null @@ -1,32 +0,0 @@ -module API - module V1 - class People < Grape::API - before { authenticate! } - - resource :people do - - # Collect comments from articles - # - # Parameters: - # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created - # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected - # limit - amount of comments returned. The default value is 20 - # - # Example Request: - # GET /people?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 - # GET /people?reference_id=10&limit=10&oldest - get do - people = select_filtered_collection_of(environment, 'people', params) - present people, :with => Entities::Person - end - - desc "Return the person information" - get '/:id' do - present environment.people.find(params[:id]), :with => Entities::Person - end - - end - - end - end -end diff --git a/lib/api/v1/users.rb b/lib/api/v1/users.rb deleted file mode 100644 index 9408476..0000000 --- a/lib/api/v1/users.rb +++ /dev/null @@ -1,31 +0,0 @@ -module API - module V1 - class Users < Grape::API - before { authenticate! } - - resource :users do - - get do - present environment.users, :with => Entities::User - end - - get ":id" do - present environment.users.find(params[:id]), :with => Entities::User - end - - get ":id/permissions" do - user = environment.users.find(params[:id]) - output = {} - user.person.role_assignments.map do |role_assigment| - if role_assigment.resource.respond_to?(:identifier) && role_assigment.resource.identifier == params[:profile] - output[:permissions] = role_assigment.role.permissions - end - end - present output - end - - end - - end - end -end -- libgit2 0.21.2