Class: RecipesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/recipes_controller.rb

Overview

RecipesController

Constant Summary

Constant Summary

Constants inherited from ApplicationController

ApplicationController::SupportedClients

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#absolutize_path, #clean_summary

Methods included from Authorization

included, ri?, #role_included?, #set_promotion_for_public_request

Instance Method Details

- (Array) compare

Get the recipes. Calling this method without any parameters will return ALL recipes. This is desirable for the first request by a user. Subsequent requests should provide an array of hashes containing the recipe identifier and the updated_at timestamp for all of the items previously requested.

When these parameters are provided, the method returns only a changeset. The produce item provides a status field to identify the type of change. status can be ‘N’ (new), ‘U’ (updated), or ‘D’ (deleted). Unmodified recipes are not returned.

This means clients can do an initial request without any parameters and receive the current full set. At a later date, the client could request updates and know whether their items match the most current set.

URL

/recipes/compare [POST]

Response Codes
200 OK

Successfully retrieved Recipe objects

# Example response
[
 {
  "recipe": {
    "row_id": "3.recipe.3163",
    "course": "Dinner / Lunch / Brunch / Breakfast",
    "total_time": "35 minutes",
    "title": "Spaghetti Frittata",
    "status": "N", # N = new, U = updated, D = deleted
    "updated_at": "2011-06-23T16:33:35Z",
    "is_featured": false,
    "date": "2011-01-03",
    "id": 3163,
    "user_id": 3,
    "copyright": "Provided by EatingWell.com © 2011 EatingWell® Media Group",
    "content": "<p>Recipe content</p>",
    "servings": 6,
    "summary": "Summary of recipe",
    "photo_url": "http://dashboard.hesapps.com/images/recipe/MP3163.JPG",
    "active_time": "25 minutes"
  }
}

]

Examples:

#POST /recipes/compare
{
  recipes: [
     { id: 3163, updated_at: '2011-06-23T16:33:35Z'},
     { id: 3182, updated_at: '2011-06-23T16:33:35Z'},
     { id: 3185, updated_at: '2011-10-10T17:07:08Z'}
  ]
}

Parameters:

  • recipes (Array)

    (optional) Array of hash objects containing id and updated_at.

Returns:

  • (Array)


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/controllers/recipes_controller.rb', line 191

def compare
  
  # check for existing items (if provided)
  rids = [] # to hold the item ids
  clauses = [] # hold the conditional clauses generated
  (params[:recipes]||[]).each do |v|
    rid = v[:id].to_i
    rids << rid #append the id to the array
    # format the time to be databse friendly
    uat = Time.parse(v[:updated_at]).strftime("%Y-%m-%d %H:%M:%S")
    # add the sql clause
    clauses << "(r.id = '#{rid}' AND r.updated_at > '#{uat}')"
  end
  
  # if there are items passed in, check to see if they still exist
  erids = [] # to hold the items that still exist
  unless rids.empty?
    # add them to the array if we found any
    Recipe.find_by_sql("SELECT r.id FROM central.recipes_view r WHERE r.id IN (#{rids.join(',')})").each { |r| erids << r.id }
  end
  # get the difference between the original and the still exists array (if any)
  drids = rids - erids
  
  # get the sql statement
  sql = item_sql(rids,clauses)

  # Set the group concat max len so we can get all of the data, but just for this session
  Recipe.connection.execute("set session group_concat_max_len=8192;");

  # get the items based on the sql
  items = []
  Recipe.find_by_sql(sql).each do |e|
    items << parse_item(e) #parse each item
  end
  
  # append the items that were removed to our output (if any)
  drids.each do |id|
    items << {
      :recipe => {
        :row_id => unique_id_for_item('recipe',id),
        :id => id,
        :status => 'D'
      }
    }
  end
  
  respond_with(items, :location => :compare_recipes, :status => :ok)

rescue
  head :bad_request
end