Class: TipsController

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

Overview

TipsController

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

/tips/compare [POST]

Response Codes
200 OK

Successfully retrieved Tip objects

# Example response
 {
     "tip": {
         "group": "2012-03.March 2012",
         "row_id": "65.tip.1",
         "title": "Gorillas in Our Midst",
         "updated_at": "1970-01-01T00:00:00Z",
         "date": "2012-03-05",
         "id": 1,
         "user_id": 65,
         "content": "In years past gorillas were thought of as fearsome beasts. But the work of many researchers, including Dian Fossey, has brought the truth to light. Gorillas are instead a family-oriented, intelligent, and typically gentle creature. In fact, their marked similarity to humans is surprising and endearing.",
         "summary": "",
         "status": "N",
         "photo_path": "http://example.getfitonroute66.com/images/tip/1.jpg"
     }
 }

Examples:

#POST /tips/compare
{
  tips: [
     { 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:

  • tips (Array)

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

Returns:

  • (Array)


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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/tips_controller.rb', line 70

def compare
  # check for existing items (if provided)
  rids = {} # to hold the data
  (params[:tips]||[]).each do |v|
    rids[v[:id]] = Time.parse(v[:updated_at])
  end
  
  # set up some convenience methods
  promotion = @user.promotion
  trip = @user.trip
  profile = trip.profile
  skip_days = Tip::SkipDays
  date = Tip.get_date_from_day_number_and_year(1,promotion.current_date.year,@user)
  edate = trip. # don't show tips beyond my last entry
  items = []
  # for each tip, let's do this

  promotion.tips.each do |t|
    if date <= edate
      
      while skip_days.include?(date.wday)
        date += 1
      end
      
      unless rids.empty?
        sid = t.id.to_s
        is_found = rids.keys.include?(sid)
        is_updated = is_found && t.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(t,date,is_updated ? 'U' : 'N')
        end
      # if there were no ids sent to us, then all items are returned
      else
        items << parse_item(t,date)
      end
      
      date += 1
    else
      break
    end
  end

  # Any remaining items are considered deleted
  rids.each do |id|
    items << {
      :tip => { 
        :row_id => unique_id_for_item('tip',id),
        :id => id.to_i,
        :status => 'D'
      }
    }
  end

  respond_with(items, :location => :compare_tips, :status => :ok)

rescue
  head :bad_request
end