class Api::V3::MacrosController

Has to be authenticated with :user_email and :user_token

Information returns in <macro_info>;

{
    id: 85,
    name: "Active carbon in soil, Blank",
    slug: "active-carbon-in-soil-blank-short",
    description: "Associated with the Active carbon in soil. Saves calibration value to EEPROM in slot userdef6 (1025+).",
    default_y_axis: "",
    default_x_axis: "",
    javascript_code: "var output = {}; .......";
    return output;
   ",
    json_data: "{..........}",
    created_at: "2015-04-01T14:41:07.065Z",
    updated_at: "2015-04-03T13:10:05.017Z",
    is_deleted: false,
    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: ""
            }
        ]
    }
}

Public Instance Methods

create() click to toggle source

Creates a macro

POST /api/v3/macros.json

Input params:

:macro => { :name => “???”, :description => “???”, :default_y_axis => “???”, :default_x_axis => “???”, :javascript_code=> “???”, :json_data=> “???”}

Ex:

post :create, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :macro => { :name => "New Macor 101", :description => "", :default_y_axis => "",  :default_x_axis => "", :javascript_code=> "foo bar", :json_data=> "foo bar"}, :format => :json}

Output:

On success - {:status => "success", :notice => "...", :macro => <macro_info>}
On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v3/macros_controller.rb, line 173
def create
  @macro = Macro.new(params[:macro])
  @macro.user_id = current_user.id
  @macro.save
end
index() click to toggle source

Returns list of macros

GET /api/v3/macros.json

Input params:

:user_id

If :user_id is set; returns all the macros the current user has created.

If :user_id is not set; returns all the macros.

Ex:

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

Output:

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

Note: To list all macros (including deleted), need to pass 'include_deleted=true'

GET /api/v3/macros.json?include_deleted=true

To list the macros for only given ids, need to pass 'ids' parameter with comma separated macro ids

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

# File app/controllers/api/v3/macros_controller.rb, line 83
def index
  if params[:include_deleted] == "true"
    @macros = Macro.unscoped.all
  else
    @macros = Macro.all
  end

  if params[:ids].present?
    macro_ids = params[:ids].to_s.split(",").compact.uniq
    @macros = @macros.where(id: macro_ids).order("id")
    return
  end

  if params[:user_id]
    @macros = @macros.where(user_id:params[:user_id])
  end
end
macros_hashes() click to toggle source

Returns list of macros with id & hash

GET 'api/v3/macros_hashes.json'

Ex:

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

Output:

On success - {:status => "success", :macros => [{id: 213, md5hash: csk38xoiw...3c}, {}]}
On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v3/macros_controller.rb, line 115
def macros_hashes
  @macros = Macro.order("id").page(params[:page]).per(1000)
end
show() click to toggle source

Returns a individual macro

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

Input params:

:id - macro id requested

Ex:

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

Output:

On success - {:status => "success", :macro => <macro_info>}
On failed - {:status => "failed", :notice => "..."}

Note: To get a deleted macro, need to pass 'include_deleted=true'

Ex:

get :show, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :id => 1, :include_deleted => 'true', :format => :json}
# File app/controllers/api/v3/macros_controller.rb, line 145
def show
  @macro = Macro.unscoped.where(id: params[:id]).first
end
update() click to toggle source

Updates a macro

PUT /api/v3/macros/:id.json

Input params:

:id // requested macro_id, :macro => { :name => “???”, :description => “???”, :default_y_axis => “???”, :default_x_axis => “???”, :javascript_code=> “???”, :json_data=> “???”}

Ex:

put :create, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :id => "xxx", :macro => { :name => "New Macor 101", :description => "", :default_y_axis => "",  :default_x_axis => "", :javascript_code=> "foo bar", :json_data=> "foo bar"}, :format => :json}

Output:

On success - {:status => "success", :notice => "...", :macro => <macro_info>}
On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v3/macros_controller.rb, line 204
def update
  @macro = Macro.where(:id => params[:id]).first

  if @macro && @macro.user_id.eql?(current_user.id)
    @macro.update_attributes(params[:macro])
  end
end