Class: EntriesController

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

Overview

EntriesController

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 entries. Calling this method without any parameters will return ALL entries. This is desirable for the first request by a user. Subsequent requests should provide an array of hashes containing the entry 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 entry provides a status field to identify the type of change. status can be ‘N’ (new), ‘U’ (updated), or ‘D’ (deleted). Unmodified entries 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

/entries/compare [POST]

Response Codes
200 OK

Successfully retrieved Entry objects

# Example response
 {
     "entry": {
         "group": "201203.March 2012",
         "row_id": "65.entry.55667",
         "is_logged": true,
         "summed_exercise_minutes": 870,
         "updated_at": "2012-06-25T15:17:14Z",
         "id": 55667,
         "activities": [
             {
                 "entry_activity": {
                     "row_id": "65.entry_activity.13426",
                     "name": "Aquathlon",
                     "entry_id": 55667,
                     "updated_at": "2012-03-28T15:04:55Z",
                     "activity_id": 114,
                     "id": 13426,
                     "exercise": 60
                 }
             },
             {
                 "entry_activity": {
                     "row_id": "65.entry_activity.13540",
                     "name": "Jogging",
                     "entry_id": 55667,
                     "updated_at": "2012-06-05T17:41:23Z",
                     "activity_id": 51,
                     "id": 13540,
                     "exercise": 30
                 }
             },
         ],
         "logged_on": "2012-03-06",
         "capped_exercise_minutes": 90,
         "user_id": 65,
         "status": "N"
     }
 }

Examples:

#POST /entries/compare
{
  entries: [
     { id: 1737, updated_at: '2011-06-23T16:33:35Z'},
     { id: 1727, updated_at: '2011-06-23T16:33:35Z'},
     { id: 1785, updated_at: '2011-10-10T17:07:08Z'}
  ]
}

Parameters:

  • entries (Array)

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

Returns:

  • (Array)


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/controllers/entries_controller.rb', line 151

def compare
  
  # check for existing items (if provided)
  rids = [] # to hold the item ids
  clauses = [] # hold the conditional clauses generated

  (params[:entries]||[]).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 << "(e.id = '#{rid}' AND e.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
    Entry.find_by_sql("SELECT e.id FROM entries e WHERE e.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)

  # get the items based on the sql
  items = []
  Entry.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 << {
      :entry => {
        :row_id => unique_id_for_item('entry',id),
        :id => id,
        :status => 'D'
      }
    }
  end
  respond_with(items, :location => :compare_entries, :status => :ok)
rescue
  head :bad_request
end