class Api::V2::MacrosController
Has to be authenticated with :user_email and :user_token
Information returns in <macro_info>;
{:id=>79, :name=>"Chlorophyll content (SPAD) multi-thick leaf", :slug=>"chlorophyll-content-spad-multi-thick-leaf", :description=>"Measures transmission at 940nm and 650nm in order to determine the 'greenness' of a leaf, ...", :default_y_axis=>"", :default_x_axis=>"", :javascript_code=>"var output = {};\r\nvar minolta_spad = 0;\r\nvar averages = 0;\r\n\r\n// ....", :json_data=> "{\"time_offset\":300,\"time\":1424882281324,\"device_id\":81,\"firmware_version\":20652,20533,84,20521,20557]}]]}", :creator=>"", :created_at=>Tue, 03 Mar 2015 21:04:35 UTC +00:00, :updated_at=>Wed, 04 Mar 2015 14:34:20 UTC +00:00}
Public Instance Methods
Creates a macro
POST /api/v2/macros/(.:format)
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"}}
Output;
On success - {:status => "success", :notice => "...", :macro => <macro_info>} On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v2/macros_controller.rb, line 103 def create @macro = Macro.new(params[:macro]) @macro.user_id = current_user.id if @macro.save render json: {status: "success", notice: "Macro created successfully", macro: @macro.info} else render json: {status: "failed", notice: "Error: #{@macro.errors.full_messages.join(',')}"} end end
Returns list of macros
GET /api/v2/macros(.:format)
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"}
Output;
On success - {:status => "success", :macros => [<macro_info>, ...]} On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v2/macros_controller.rb, line 43 def index if params[:user_id] render json: {status: "success", macros: Macro.where(user_id:params[:user_id]).all.map(&:info)} else render json: {status: "success", macros: Macro.all.map(&:info)} end end
Returns a individual macro
GET /api/v2/macros/:id(.:format)
Input params;
:id - macro id requested
Ex;
get :show, {:user_email => foo@bar.com, :user_token => "xxxxxxx", :id => 1}
Output;
On success - {:status => "success", :macro => <macro_info>} On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v2/macros_controller.rb, line 69 def show @macro = Macro.where(id: params[:id]).first if @macro render json: {status: "success", notice: "Record found", macro: @macro.info} else render json: {status: "failed", notice: "Error: Couldn't find a macro with id #{params[:id]}"} end end
Updates a macro
PUT /api/v2/macros/:id(.:format)
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"}}
Output;
On success - {:status => "success", :notice => "...", :macro => <macro_info>} On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v2/macros_controller.rb, line 140 def update @macro = Macro.where(:id => params[:id]).first if @macro && @macro.user_id.eql?(current_user.id) if @macro.update_attributes(params[:macro]) render json: {status: "success", notice: "Macro updated successfully", macro: @macro.info} else render json: {status: "failed", notice: "Error: #{@macro.errors.full_messages.join(',')}"} end else render json: {status: "failed", notice: "Error: Couldn't find a macro with id #{params[:id]}"} end end