Class: Rack::PassbookRack

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/passbook_rack.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ PassbookRack

Returns a new instance of PassbookRack.



4
5
6
7
# File 'lib/rack/passbook_rack.rb', line 4

def initialize(app)
  @app = app
  @parameters = {}
end

Instance Method Details

#append_parameter_separator(url) ⇒ Object



45
46
# File 'lib/rack/passbook_rack.rb', line 45

def append_parameter_separator url
end

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/passbook_rack.rb', line 9

def call(env)
  @parameters['authToken'] = env['HTTP_AUTHORIZATION'].gsub(/ApplePass /,'') if env['HTTP_AUTHORIZATION']
  @parameters.merge!(Rack::Utils.parse_nested_query(env['QUERY_STRING']))
  method_and_params = find_method env['PATH_INFO']
  if method_and_params
    case method_and_params[:method]
    when 'device_register_delete'
      if env['REQUEST_METHOD'] == 'POST'
        [Passbook::PassbookNotification.
          register_pass(method_and_params[:params].merge! JSON.parse(env['rack.input'].read 1000))[:status],
          {}, ['']]
      elsif env['REQUEST_METHOD'] == 'DELETE'
        [Passbook::PassbookNotification.unregister_pass(method_and_params[:params])[:status], {}, {}]
      end
    when 'passes_for_device'
      response = Passbook::PassbookNotification.passes_for_device(method_and_params[:params])
      [response ? 200 : 204, {}, [response.to_json]]
    when 'latest_pass'
      response = Passbook::PassbookNotification.latest_pass(method_and_params[:params])
      if response[:status] == 200
        [200, {'Content-Type' => 'application/vnd.apple.pkpass',
          'Content-Disposition' => 'attachment',
          'filename' => "#{method_and_params[:params]['serialNumber']}.pkpass","last-modified" => response[:last_modified]}, [response[:latest_pass]]]
      else
        [response[:status], {}, {}]
      end
    when 'log'
      Passbook::PassbookNotification.passbook_log JSON.parse(env['rack.input'].read 10000)
      [200, {}, {}]
    else
    end
  else
    @app.call env
  end
end

#each(&block) ⇒ Object



48
49
# File 'lib/rack/passbook_rack.rb', line 48

def each(&block)
end

#find_method(path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rack/passbook_rack.rb', line 52

def find_method(path)
  parsed_path = path.split '/'
  url_beginning = parsed_path.index 'v1'
  if url_beginning
    args_length = parsed_path.size - url_beginning

    if  (parsed_path[url_beginning + 1 ] == 'devices') and
      (parsed_path[url_beginning + 3 ] == 'registrations')
      if args_length == 6
        return method_and_params_hash 'device_register_delete', path
      elsif args_length == 5
        return method_and_params_hash 'passes_for_device', path
      end
    elsif parsed_path[url_beginning + 1] == 'passes' and args_length == 4
      return method_and_params_hash 'latest_pass', path
    elsif parsed_path[url_beginning + 1] == 'log' and args_length == 2
      return {:method => 'log'}
    end
  end

  return nil
end