Class: ActivitiesController

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

Overview

ActivitiesController

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

/activities/compare [POST]

Response Codes
200 OK

Successfully retrieved activity objects

# Example response
 {
     "activity": {
         "row_id": "65.activity.1",
         "name": "Archery",
         "updated_at": "2009-04-29T15:30:27Z",
         "id": 1,
         "user_id": 65,
         "description": "Practicing archery is a way to connect with one of the world's oldest sports. Invented in the Paleolithic or Mesolithic era, the bow and arrow are considered key discoveries in human history, ranking in importance with the art of making fire.",
         "status": "N",
         "photo_path": "http: //example.getfitonroute66.com/images/activities/Archery.jpg"
     }
 }

Examples:

#POST /activities/compare
{
  activities: [
     { id: 1, updated_at: '2011-06-23T16:33:35Z'},
     { id: 2, updated_at: '2011-06-23T16:33:35Z'},
     { id: 3, updated_at: '2011-10-10T17:07:08Z'}
  ]
}

Parameters:

  • activities (Array)

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

Returns:

  • (Array)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/activities_controller.rb', line 65

def compare
  # check for existing items (if provided)
  rids = {} # to hold the data
  (params[:activity]||[]).each do |v|
    rids[v[:id]] = Time.parse(v[:updated_at])
  end

  items = []
  # for each activity, let's do this
  @user.promotion.activities.each do |a|
    unless rids.empty?
      sid = a.id.to_s
      is_found = rids.keys.include?(sid)
      is_updated = is_found && a.updated_at > rids[sid]

      # delete it from the hash if we found it (so we don't have to loop through all of them every time)
      rids.delete(sid) if is_found

      # if it's been updated or wasn't found in our set, we'll want to add it to our returned items
      if is_updated || !is_found
        items << parse_item(a,is_updated ? 'U' : 'N')
      end
    # if there were no ids sent to us, then all items are returned
    else
      items << parse_item(a,is_updated ? 'U' : 'N')
    end
  end

  # Any remaining items are considered deleted
  rids.each do |v|
    items << {
      :activity => { 
        :row_id => unique_id_for_item('activity',v),
        :id => v.to_i,
        :status => 'D'
      }
    }
  end
  respond_with(items, :location => :compare_activities, :status => :ok)
rescue
  head :bad_request
end