Class: Passbook::RegistrationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/passbook/registrations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/passbook/registrations_controller.rb', line 43

def create
  puts "Handling registration request..."
  # validate that the request is authorized to deal with the pass referenced
  pass = Passbook.pass_type_id_to_class(params[:pass_type_id]).where(:serial_number => params[:serial_number]).where(:authentication_token => authentication_token).first
  unless pass.blank?
    puts 'Pass and authentication token match.'

    # Validate that the device has not previously registered
    # Note: this is done with a composite key that is combination of the device_id and the pass serial_number
    uuid = params[:device_id] + "-" + params[:serial_number]
    if Registration.where(:uuid => uuid).count < 1
      # No registration found, lets add the device
      # push_token = params[:push_token]
      Registration.create!(:uuid => uuid,
                          :device_id => params[:device_id],
                          :pass_type_id => params[:pass_type_id],
                          :push_token => push_token,
                          :serial_number => params[:serial_number])

      # spawn an event for registration creation
      pass.register_handler if pass.respond_to? :register_handler

      # Return a 201 CREATED status
      # status 201
      render :json => {}, :status => 201
    else
      # The device has already registered for updates on this pass
      # Acknowledge the request with a 200 OK response
      # status 200
      render :json => {}, :status => 200
    end

  else
    # The device did not statisfy the authentication requirements
    # Return a 401 NOT AUTHORIZED response
    # status 401
    render :json => {}, :status => 401
  end
end

#deleteObject



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
# File 'app/controllers/passbook/registrations_controller.rb', line 84

def delete
  puts "Handling unregistration request..."
  puts authentication_token
  if Passbook.pass_type_id_to_class(params[:pass_type_id]).where(:serial_number => params[:serial_number], :authentication_token => authentication_token).first
    puts 'Pass and authentication token match.'

    # Validate that the device has previously registered
    # Note: this is done with a composite key that is combination of the device_id and the pass serial_number
    uuid = params[:device_id] + "-" + params[:serial_number]
    registration = Registration.find_by_uuid(uuid)
    unless registration.blank?
      Registration.delete registration.id

      # spawn an event for unregistration event
      pass.unregister_handler if pass.respond_to? :unregister_handler

      render :json => {}, :status => 200
    else
      puts 'Registration does not exist.'
      render :json => {}, :status => 401
    end
  else
    # Not authorized
    render :json => {}, :status => 401
  end
end

#updatableObject



111
112
113
114
115
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
# File 'app/controllers/passbook/registrations_controller.rb', line 111

def updatable
  puts "Handling updates request..."
  # Check first that the device has registered with the service
  if Registration.where(:device_id => params[:device_id]).count > 0

    # The device is registered with the service

    # Find the registrations for the device
    registered_serial_numbers = Registration.where(:device_id => params[:device_id], :pass_type_id => params[:pass_type_id]).collect{|r| r[:serial_number]}

    # The passesUpdatedSince param is optional for scoping the update query
    if params[:passesUpdatedSince] && params[:passesUpdatedSince] != ""
      registered_passes = Passbook.pass_type_id_to_class(params[:pass_type_id]).where(:serial_number => registered_serial_numbers).where('updated_at IS NULL OR updated_at >= ?', params[:passesUpdatedSince])
    else
      registered_passes = Passbook.pass_type_id_to_class(params[:pass_type_id]).where(:serial_number => registered_serial_numbers)
    end

    # Are there passes that this device should recieve updates for?
    if registered_passes.count > 0
      # Found passes that could be updated for this device
      # Build the response object
      update_time = registered_passes.map(&:updated_at).max
      updatable_passes_payload = {:lastUpdated => update_time}
      updatable_passes_payload[:serialNumbers] = registered_passes.collect{|rp| rp[:serial_number]}

      render :json => updatable_passes_payload.to_json, :status => 200
    else
      render :json => {}, :status=>204
    end

  else
    # This device is not currently registered with the service
    render :json => {}, :status=> 404
  end
end