class Api::V3::DataController

Has to be authenticated with :user_email and :user_token

Information returns in <datum_info>:

{
    id: 25375,
    datum_url: "http://localhost:3000/projects/active-c-in-soils-calibration/explore/25375",
    user_id: 23,
    status: "submitted",
    name: "0.390",
    user_answers: {},
    location: [
    42.7225676,
    -84.4746886
  ],
    sample: [....]
}

Public Instance Methods

all() click to toggle source
# File app/controllers/api/v3/data_controller.rb, line 166
def all
  @project = Project.where(id: params[:project_id]).first
  if @project.present?
    begin
      data = ActiveRecord::Base.connection.execute("select datum from projectdata where project_id = #{@project.id}")
      datum = []
      data.each { |row| datum << JSON.parse(row["datum"]) }
      render json: {status: "success", notice: "Record found", data: datum}
    rescue Exception => e
      render json: {status: "failed", notice: e.message} and return
    end
  else
    render json: {status: "failed", notice: "Record not found"} and return
  end
end
create() click to toggle source

Creates a datum

POST /api/v3/projects/:project_id/data.json

1) Only survey data (“user_answers” key):

If you are interested in submitting only answers to a survey, and you don’t have any additional sensor data, simply use the “user_answers” key.

Each answer should have it’s unique question ID followed by the ID value.

These question IDs are pulled from the project itself.

Ex:

 project_data = {
     "time" => 1393970826,
     "device_id" => "1",
     "firmware_version" => "Alfie Arabidopsis",
     "location" => [42.61092271,-84.40556498],
     "user_answers" => {"943"=>"user answer 1", "944"=>"user answer 2", "945"=>"user answer 3"}
 }

post :create, {:project_id => @project.id, :data => project_data, :user_email => @collaborator.email, :user_token => @collaborator.authentication_token, :format=>:json}

2) Only sensor data (“sample” key):

Ex:

 project_data = {
   "time" => 1393970826,
   "device_id" => "1",
   "firmware_version" => "Alfie Arabidopsis",
   "location" => [42.61092271,-84.40556498],
   "sample" => [[{ "protocol_id" => "698", "time" => 1393970826, "get_ir_baseline" => [[15, 145.32, 97.59], [14, 5463.24, 6029.19]], "light_intensity" => 1.0, "r" => 0.0, "g" => 0.0, "b" => 0.0, "relative_humidity" => 58.37, "temperature" => 21.31, "data_raw" => [798, 880, 818, 859, 802, 824, 796, 826, 813, 833, 812, 833, 821, 832, 821, 819, 813, 852, 831, 831, -30, -33, -9, -5, 0, -13, -3, -4, -6, -15, 10, -97, 6, 2, -31, -4, -36, 4, 3, -24, -62, -19, -33, -43, -61, -49, -51, -49, -65, -48, -39, -71, -121, -65, -24, -36, 41, -11, -47, -57, -38, -16, -60, -57, -60, -57, -20, -30, -28, -26, -55, -44, -37, -33, -29, -49, -47, -53, -59, -42, -34, -37, -29, -45, 14, -45, -61, -57, -36, -27, -33, -12, -4, -21, -17, -1, -6, -5, -14, -24, -17, -31, 6, -14, 4, -20, -2, -6, 2, -17]},
                 { "protocol_id" => "699", "time" => 1393970826, "get_blank_cal" => [[12, 31225.0], [20, 63597.0]], "get_userdef0" => [2.6, 0.0], "relative_humidity" => 58.35, "temperature" => 21.36, "light_intensity" => 1.0, "r" => 0.0, "g" => 0.0, "b" => 0.0, "data_raw" => [30328, 25322, 23631, 24079, 23823, 23835, 23828, 23935, 23928, 23934, 23925, 23947, 23958, 23934, 23992, 23821, 23933, 23819, 23949, 23939, 24056, 24065, 24082, 23922, 23974, 23937, 23955, 23916, 23935, 23832, 23938, 23833, 23809, 23936, 23961, 23950, 24066, 24097, 24095, 24070, 24091, 24082, 23952, 23947, 23958, 23937, 23653, 23919, 23700, 23671, 23662, 23664, 23708, 23686, 23675, 23635, 23705, 23692, 23667, 23683, 23659, 23631, 23836, 23651, 23722, 23704, 23735, 23693, 23736, 23720, 23743, 23739, 23847, 23876, 23852, 23840, 23839, 23839, 28510, 23825, 23855, 23823, 23848, 23870, 23860, 23859, 23911, 23885, 23847, 28540, 28532, 28514, 28533, 28515, 28498, 28514, 28461, 28508, 28476, 28499, 50066, 59721, 59386, 59319, 59638, 59173, 59521, 59275, 59684, 59343, 59330, 59461, 59236, 59529, 59195, 59417, 59683, 59225, 59488, 59237, 59209, 59692, 59209, 59720, 59287, 59704, 59167, 59667, 59215, 59299, 59212, 59359, 59419, 59145, 59220, 59356, 59105, 59532, 59692, 59210, 59691, 59330, 59654, 59307, 59648, 59169, 59363, 59593, 59658, 59192, 59299, 59477, 59158, 59653, 59290, 59618, 59302, 59129, 59363, 59185, 59284, 59612, 59398, 59627, 59282, 59725, 59171, 59614, 59330, 59616, 59282, 59230, 59660, 59674, 59118, 59644, 59286, 59576, 59714, 59190, 59214, 59711, 59267, 59275, 59645, 59154, 59268, 59363, 59318, 59387, 59514, 59730, 59358, 59159, 59481, 59709, 59182, 59176, 59183, 59188] }]]
 }

post :create, {:project_id => @project.id, :data => project_data, :user_email => @collaborator.email, :user_token => @collaborator.authentication_token, :format=>:json}

3) Both survey and sensor data (“user_answers” + “sample” keys)

Ex:

 project_data = {
     "time" => 1393970826,
     "device_id" => "1",
     "firmware_version" => "Alfie Arabidopsis",
     "location" => [42.61092271,-84.40556498],
     "user_answers" => {"943"=>"user answer 1", "944"=>"user answer 2", "945"=>"user answer 3"},
     "sample" => [[{ "protocol_id" => "698", "time" => 1393970826, "get_ir_baseline" => [[15, 145.32, 97.59], [14, 5463.24, 6029.19]], "light_intensity" => 1.0, "r" => 0.0, "g" => 0.0, "b" => 0.0, "relative_humidity" => 58.37, "temperature" => 21.31, "data_raw" => [798, 880, 818, 859, 802, 824, 796, 826, 813, 833, 812, 833, 821, 832, 821, 819, 813, 852, 831, 831, -30, -33, -9, -5, 0, -13, -3, -4, -6, -15, 10, -97, 6, 2, -31, -4, -36, 4, 3, -24, -62, -19, -33, -43, -61, -49, -51, -49, -65, -48, -39, -71, -121, -65, -24, -36, 41, -11, -47, -57, -38, -16, -60, -57, -60, -57, -20, -30, -28, -26, -55, -44, -37, -33, -29, -49, -47, -53, -59, -42, -34, -37, -29, -45, 14, -45, -61, -57, -36, -27, -33, -12, -4, -21, -17, -1, -6, -5, -14, -24, -17, -31, 6, -14, 4, -20, -2, -6, 2, -17]},
                   { "protocol_id" => "699", "time" => 1393970826, "get_blank_cal" => [[12, 31225.0], [20, 63597.0]], "get_userdef0" => [2.6, 0.0], "relative_humidity" => 58.35, "temperature" => 21.36, "light_intensity" => 1.0, "r" => 0.0, "g" => 0.0, "b" => 0.0, "data_raw" => [30328, 25322, 23631, 24079, 23823, 23835, 23828, 23935, 23928, 23934, 23925, 23947, 23958, 23934, 23992, 23821, 23933, 23819, 23949, 23939, 24056, 24065, 24082, 23922, 23974, 23937, 23955, 23916, 23935, 23832, 23938, 23833, 23809, 23936, 23961, 23950, 24066, 24097, 24095, 24070, 24091, 24082, 23952, 23947, 23958, 23937, 23653, 23919, 23700, 23671, 23662, 23664, 23708, 23686, 23675, 23635, 23705, 23692, 23667, 23683, 23659, 23631, 23836, 23651, 23722, 23704, 23735, 23693, 23736, 23720, 23743, 23739, 23847, 23876, 23852, 23840, 23839, 23839, 28510, 23825, 23855, 23823, 23848, 23870, 23860, 23859, 23911, 23885, 23847, 28540, 28532, 28514, 28533, 28515, 28498, 28514, 28461, 28508, 28476, 28499, 50066, 59721, 59386, 59319, 59638, 59173, 59521, 59275, 59684, 59343, 59330, 59461, 59236, 59529, 59195, 59417, 59683, 59225, 59488, 59237, 59209, 59692, 59209, 59720, 59287, 59704, 59167, 59667, 59215, 59299, 59212, 59359, 59419, 59145, 59220, 59356, 59105, 59532, 59692, 59210, 59691, 59330, 59654, 59307, 59648, 59169, 59363, 59593, 59658, 59192, 59299, 59477, 59158, 59653, 59290, 59618, 59302, 59129, 59363, 59185, 59284, 59612, 59398, 59627, 59282, 59725, 59171, 59614, 59330, 59616, 59282, 59230, 59660, 59674, 59118, 59644, 59286, 59576, 59714, 59190, 59214, 59711, 59267, 59275, 59645, 59154, 59268, 59363, 59318, 59387, 59514, 59730, 59358, 59159, 59481, 59709, 59182, 59176, 59183, 59188] }]]
 }

post :create, {:project_id => @project.id, :data => project_data, :user_email => @collaborator.email, :user_token => @collaborator.authentication_token, :format=>:json}

4) Uploading data with the project questions having the type “Take Picture”

You can submit the answers as base64 encoded images, if the question type is “Take Picture”

Ex:

 project_data = {
     "time" => 1393970826,
     "device_id" => "1",
     "firmware_version" => "Alfie Arabidopsis",
     "location" => [42.61092271,-84.40556498],
     "user_answers" => {"946"=>"data:image/jpeg;base64,/9j/4AAQSkZJRgABA.....", "947"=>"data:image/png;base64,iVBORw0KGgoAA.....", "945"=>"user answer 3"},
     "sample" => [[{ "protocol_id" => "698", "time" => 1393970826, "get_ir_baseline" => [[15, 145.32, 97.59], [14, 5463.24, 6029.19]], "light_intensity" => 1.0, "r" => 0.0, "g" => 0.0, "b" => 0.0, "relative_humidity" => 58.37, "temperature" => 21.31, "data_raw" => [798, 880, 818, 859, 802, 824, 796, 826, 813, 833, 812, 833, 821, 832, 821, 819, 813, 852, 831, 831, -30, -33, -9, -5, 0, -13, -3, -4, -6, -15, 10, -97, 6, 2, -31, -4, -36, 4, 3, -24, -62, -19, -33, -43, -61, -49, -51, -49, -65, -48, -39, -71, -121, -65, -24, -36, 41, -11, -47, -57, -38, -16, -60, -57, -60, -57, -20, -30, -28, -26, -55, -44, -37, -33, -29, -49, -47, -53, -59, -42, -34, -37, -29, -45, 14, -45, -61, -57, -36, -27, -33, -12, -4, -21, -17, -1, -6, -5, -14, -24, -17, -31, 6, -14, 4, -20, -2, -6, 2, -17]},
                   { "protocol_id" => "699", "time" => 1393970826, "get_blank_cal" => [[12, 31225.0], [20, 63597.0]], "get_userdef0" => [2.6, 0.0], "relative_humidity" => 58.35, "temperature" => 21.36, "light_intensity" => 1.0, "r" => 0.0, "g" => 0.0, "b" => 0.0, "data_raw" => [30328, 25322, 23631, 24079, 23823, 23835, 23828, 23935, 23928, 23934, 23925, 23947, 23958, 23934, 23992, 23821, 23933, 23819, 23949, 23939, 24056, 24065, 24082, 23922, 23974, 23937, 23955, 23916, 23935, 23832, 23938, 23833, 23809, 23936, 23961, 23950, 24066, 24097, 24095, 24070, 24091, 24082, 23952, 23947, 23958, 23937, 23653, 23919, 23700, 23671, 23662, 23664, 23708, 23686, 23675, 23635, 23705, 23692, 23667, 23683, 23659, 23631, 23836, 23651, 23722, 23704, 23735, 23693, 23736, 23720, 23743, 23739, 23847, 23876, 23852, 23840, 23839, 23839, 28510, 23825, 23855, 23823, 23848, 23870, 23860, 23859, 23911, 23885, 23847, 28540, 28532, 28514, 28533, 28515, 28498, 28514, 28461, 28508, 28476, 28499, 50066, 59721, 59386, 59319, 59638, 59173, 59521, 59275, 59684, 59343, 59330, 59461, 59236, 59529, 59195, 59417, 59683, 59225, 59488, 59237, 59209, 59692, 59209, 59720, 59287, 59704, 59167, 59667, 59215, 59299, 59212, 59359, 59419, 59145, 59220, 59356, 59105, 59532, 59692, 59210, 59691, 59330, 59654, 59307, 59648, 59169, 59363, 59593, 59658, 59192, 59299, 59477, 59158, 59653, 59290, 59618, 59302, 59129, 59363, 59185, 59284, 59612, 59398, 59627, 59282, 59725, 59171, 59614, 59330, 59616, 59282, 59230, 59660, 59674, 59118, 59644, 59286, 59576, 59714, 59190, 59214, 59711, 59267, 59275, 59645, 59154, 59268, 59363, 59318, 59387, 59514, 59730, 59358, 59159, 59481, 59709, 59182, 59176, 59183, 59188] }]]
 }

post :create, {:project_id => @project.id, :data => project_data, :user_email => @collaborator.email, :user_token => @collaborator.authentication_token, :format=>:json}

Output:

On success - {:status => "success", :notice => "...", :datum => <datum_info>}
On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v3/data_controller.rb, line 146
def create
  @project = Project.find(params[:project_id])

  unless @project
    render json: {status: "failed", notice: "Error: Couldn't find a project with id #{params[:project_id]}"} and return
  end

  begin
    params[:data][:sample] = params[:data][:sample].to_json unless (params[:data].blank? || params[:data][:sample].blank?)
  rescue Exception
    render json: {status: "failed", notice: "invalid sample data provided"} and return
  end

  data = params[:data].is_a?(Hash) ? params[:data] : {}

  data[:api_version] = 3

  @datum = @project.data.create(data_map: data, user_id: current_user.id, checksum: Digest::MD5.hexdigest(params.to_s))
end
index() click to toggle source

Returns list of datum

GET /api/v3/:project_id/data.json

Input params:

:project_id //the project which data belongs to

Ex:

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

Output:

On success - {:status => "success", :data => [<datum_info>, ...]}
On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v3/data_controller.rb, line 42
def index
  @project = Project.find(params[:project_id])
end
show() click to toggle source

Shows a datum

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

Input params:

:project_id // the project which data belongs to :id // a datum id which belongs to the project having the :project_id

Ex:

get :show, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :project_id => "xx", :datum_id => "yy" :format => :json}

Output

On success - {:status => "success", :datum => <datum_info> }
On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v3/data_controller.rb, line 64
def show
  @project = Project.find(params[:project_id])
  @datum = @project.data.find_by(id: params[:id])
end