Class: DestinationsController

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

Overview

DestinationsController

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 a set of destinations based on some criteria. Calling this method without any parameters will return ALL destinations. This is desirable for first the first request by a user. Subsequent requests should provide, the date of the last request (since timestamp) and an array of hashes containing the destination identifier and the updated_at timestamp for all of the destinations previously requested.

When these parameters are provided, the method returns only a changeset. The destination provides a status field to identify the type of change. status can be ‘N’ (new), ‘U’ (updated), ‘D’ (deleted), or ‘=’ (unchanged). Unchanged destinations can be ignored by the client.

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 receive notification of whether their items match the most current set.

NOTE: An invalid since parameter will not necessarily result in an error but may return unexpected results.

URL

/destinations/compare [POST]

Response Codes
200 OK

Successfully retrieved Destination objects

# Example response
 {
     "destination": {
         "row_id": "65.destination.1",
         "name": "Chicago, IL",
         "updated_at": "2012-04-10T14:05:52Z",
         "id": 1,
         "user_id": 65,
         "description": "The Begin Historic Route 66 sign is in front of the Chicago Art Institute at Adams Street. At Jackson and Jefferson Streets, you can still eat at the famous Lou Mitchell's Restaurant, which opened in 1923. The Union Train Station's Great Hall, built in 1925, has marble floors and walls as well as Corinthian columns. In the 1940s the station served over 100,000 passengers each day.",
         "status": "N",
         "photo_path": "http://www.getfitonroute66.com/images/destination_photos/1-1.jpg",
         "distance_from_start": 0
     }
 }

Examples:

#POST /destinations/compare
{
  since: '2011-10-11T00:00:00Z',
  destinations: [
     { id: 3000000000163, updated_at: '2011-06-23T16:33:35Z'},
     { id: 3000000000127, updated_at: '2011-06-23T16:33:35Z'},
     { id: 3000000000185, updated_at: '2011-10-10T17:07:08Z'}
  ]
}

Parameters:

  • since (Time)

    (optional) Allows clients to request all of the destinations that may have been added or updated since the provided timestamp.

  • destinations (Array)

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

Returns:

  • (Array)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/destinations_controller.rb', line 116

def compare
  # define some necessary variables
  # get the since timestamp if provided
  since = Time.parse(params[:since]) rescue nil
  # check for existing items (if provided)
  rids = [] # to hold the item ids
  clauses = [] # hold the conditional clauses generated
  (params[:destinations]||[]).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 << "(d.id = '#{rid}' AND d.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
    Destination.find_by_sql("SELECT destination_id FROM rel_promotions_destinations WHERE promotion_id = #{@user.promotion.id} AND destination_id IN (#{rids.join(',')})").each { |d| erids << d.destination_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(since,erids,clauses)  
  # Set the group concat max len so we can get all of the data, but just for this session
  Destination.connection.execute("set session group_concat_max_len=8192;");
      
  # get the items based on the sql
  items = []
  
  Destination.find_by_sql(sql).each do |d|
    items << parse_item(d) #parse each item
  end
  # append the items that were removed to our output (if any)
  drids.each do |id|
    items << {
      :destination => {
        :id => unique_id_for_item(id), :status => 'D'
      }
    }
  end
  respond_with(items, :location => :compare_destinations, :status => :ok)
rescue
  head :bad_request
end