class Api::V3::ProjectsController

Has to be authenticated with :user_email and :user_token

Information returns in <project_info>;

{
    id: 226,
    slug: "active-c-in-soils-calibration",
    name: "Active C in soils: CALIBRATION",
    created_at: "2015-03-30T13:29:24.354Z",
    updated_at: "2015-04-03T13:42:35.844Z",
    creator: {
        name: "Dan TerAvest",
        id: 23,
        email: "teravestdan@gmail.com",
        bio: "I am a PhD candidate currently living and working in Malawi. My PhD research involves analyzing the sustainability of multiple cropping systems on smallholder farms in Malawi",
        institute: "Washington State University",
        profile_url: "http://localhost:3000/users/dan-teravest",
        contributions: 3650,
        projects: 6,
        collaborations: 4,
        avatar: {
            original: "http://localhost:3000/assets/avatar.png",
            thumb: "http://localhost:3000/assets/thumb_avatar.png",
            medium: "http://localhost:3000/assets/medium_avatar.png"
        },
        latest_activity: "2015-04-06T14:15:29.499Z",
        badges: [
            {
                name: "",
                url: ""
            }
        ]
    },
   description: "<p>This project will generate a standard curve for KMnO4 oxidation of active C in soils and submit this data to the device. This project needs to be run prior to measuring unknown soil samples for active soil C. We recommend that you prepare all stock, standard, and working solutions prior to running this project&nbsp;</p> "
}

Public Instance Methods

active_projects() click to toggle source

Returns list of active projects for the current logged in user, with described associated objects

GET /api/v3/users/active_projects.json

Ex:

get :active_projects, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :format => :json}

Output:

On success - {:status => "success", :projects => [<project_info>, ...]}
On failed - {:status => "failed", :notice => "..."}

If project ids passed, it will return only projects with mentioned project ids

GET /api/v3/users/active_projects.json?ids=72,57,44

Ex:

get :active_projects, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :format => :json}

Output:

On success - {:status => "success", :projects => [<project_info>, ...]}
On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v3/projects_controller.rb, line 173
def active_projects
  if params[:ids].present?
    project_ids = params[:ids].to_s.split(",").compact.uniq
    if project_ids.length > 0
      @projects = current_user.active_selecetd_projects(project_ids.map(&:to_i))
    end
  else
    @projects = current_user.active_projects
  end
end
active_projects_hashes() click to toggle source

Returns list of active projects hashes for the current logged in user.

GET /api/v3/users/active_projects_hashes.json

Ex:

get :active_projects_hashes, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :format => :json}

Output:

On success - {:status => "success", :projects => [<project_info>, ...]}
On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v3/projects_controller.rb, line 308
def active_projects_hashes
  @projects = current_user.active_projects
end
autocomplete() click to toggle source

Returns the search results according to the term, with maximum 5 results per request. This will be used to populate autocomplete words typed in text fields

GET '/api/v3/projects/autocomplete.json?term=test'

Ex:

get :autocomplete, {user_email => foo@bar.com, :user_token => "xxxxxxx", :format => :json}

Output:

{}

# File app/controllers/api/v3/projects_controller.rb, line 228
def autocomplete
  results = Project.search(params[:term], fields: [:name], limit: 5).map(&:name)
  render json: { status: "success", results: results }
end
index() click to toggle source

Returns list of projects

GET /api/v3/projects.json

Input params:

:all - 1, :page

If :all is set; returns all the projects pagingated according to the :page (if :page not passed 1st page is considered). page size is 10.

If :all is not set; returns all the projects the current user has access to (lead + collaborator).

If project ids passed, it will return only projects with mentioned project ids

GET /api/v3/projects.json?ids=72,57,44

Ex:

get :index, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :all => 1, :page => 2, :format => :json}

Output:

# if :all - 1
On success - {:status => "success", :projects => [<project_info>, ...]}
# else
On success - {:status => "success", :projects => [<project_info>, ...], :page => xx, :total_pages => xx}

On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v3/projects_controller.rb, line 74
def index
  if params[:ids].present?
    project_ids = params[:ids].to_s.split(",").compact.uniq
    @projects = Project.where(id: project_ids).order("id")
    return
  end

  #filter project by user id - own projects and collaborations, if all send a special flags filter and paging
  if params[:all].to_i.eql?(1)
    @projects = Project.order("id").page(params[:page].to_i).per(10)
  else
    @projects = current_user.all_projects
  end
end
join() click to toggle source

Allows the current user to join a project

POST '/api/v3/projects/:id/join.json'

Ex:

post :join, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :format => :json}

Output:

On success - {:status => “success”} On failed - {:status => “failed”}

# File app/controllers/api/v3/projects_controller.rb, line 249
def join
  @project = Project.find(params[:id])
  if @project.is_open?
    @collaboration = ProjectCollaborator.where(:project_id=>@project.id,:user_id=>current_user.id).first || ProjectCollaborator.create(:project_id=>@project.id,:user_id=>current_user.id)

    if @collaboration.valid?
      render json: { status: 'success', notice: "You have successfully joined the project!"}
    else
      render json: { status: 'failed', notice: "Oops something went wrong. You cannot join at this time."}
    end
  else
    render json: { status: 'failed', notice: "Oops project is not open for contributions!"}
  end
end
leave() click to toggle source

Allows the current user to leave a project

POST '/api/v3/projects/:id/leave.json'

Ex:

post :leave, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :format => :json}

Output:

On success - {:status => “success”} On failed - {:status => “failed”}

# File app/controllers/api/v3/projects_controller.rb, line 279
def leave
  @project = Project.find(params[:id])
  @collaboration = ProjectCollaborator.where(:project_id=>@project.id,:user_id=>current_user.id).first

  if @collaboration.blank?
    render json: { status: 'failed', notice: "Oops nothing to do. You are not a collaborator for this project!"}
  else
    if  @collaboration.destroy
      render json: { status: 'success', notice: "You have successfully left the project!"}
    else
      render json: { status: 'failed', notice: "Oops something went wrong. You cannot leave  at this time."}
    end
  end
end
projects_hashes() click to toggle source

Returns hash list of projects

GET /api/v3/projects_hashes.json

Input params:

:all - 1, :page

If :all is set; returns all the projects pagingated according to the :page (if :page not passed 1st page is considered). page size is 10.

If :all is not set; returns all the projects the current user has access to (lead + collaborator).

Ex:

get :projects_hashes, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :all => 1, :page => 2, :format => :json}

Output:

# if :all - 1
On success - {:status => "success", :projects => [<id>,<md5hash>]}
# else
On success - {:status => "success", :projects => [<id>,<md5hash>], :page => xx, :total_pages => xx}

On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v3/projects_controller.rb, line 115
def projects_hashes
  #filter project by user id - own projects and collaborations, if all send a special flags filter and paging
  if params[:all].to_i.eql?(1)
    @projects = Project.order("id").page(params[:page].to_i).per(1000)
  else
    @projects = current_user.all_projects
  end
end
results() click to toggle source

Returns project results raw markdown content

POST '/api/v3/projects/:id/results.json'

Ex:

get :results, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :format => :json}

Output:

On success -

{"status": "success",
"notice": "Record found",
"project_id": 452,
"content": "**Test!**\r\n\r\nTesting results \r\n\r\n![Coconuts](http://circleofdocs.com/wp-content/uploads/2014/10/bigstock-Coconut-isolated-on-white-back-70653349.jpg \"Coconuts\")\r\n\r\n1. list text here\r\n1. list text here\r\n1. list text here",
"created_at": "2015-06-24T17:14:11.594Z",
"updated_at": "2016-02-02T20:04:37.843Z"}

On failed -

{"status": "failed",
"notice": "Error: Couldn't find a project with id xxxx"}
# File app/controllers/api/v3/projects_controller.rb, line 336
def results
  @project = Project.find_by_id(params[:id])
end
show() click to toggle source

Returns an individual project

GET /api/v3/projects/:id.json

Input params:

:id - project id requested

Ex:

get :show, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :id => 1, :format => :json}

Output:

On success - {:status => "success", :project => <project_info>}
On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v3/projects_controller.rb, line 142
def show
  @project = Project.find_by(id: params[:id])
end