Class: MealTicket

Inherits:
Object
  • Object
show all
Includes:
MealTicketRoutes
Defined in:
lib/meal_ticket.rb

Defined Under Namespace

Classes: Config

Constant Summary

Constants included from MealTicketRoutes

MealTicketRoutes::FLICKR_API_BASE_URL

Instance Method Summary collapse

Methods included from MealTicketRoutes

#facebook_auth_url, #facebook_exchange_url, #flickr_auth_url, #flickr_frob_url

Constructor Details

#initialize(app) ⇒ MealTicket

Returns a new instance of MealTicket.



64
65
66
# File 'lib/meal_ticket.rb', line 64

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/meal_ticket.rb', line 68

def call(env)
  @env = env
  if env["PATH_INFO"] =~ /^\/meal_ticket\/facebook_callback/
    facebook_callback
  elsif env["PATH_INFO"] =~ /^\/meal_ticket\/flickr_callback/
    flickr_callback
  else
    @app.call(env)
  end
end

#facebook_callbackObject



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
# File 'lib/meal_ticket.rb', line 79

def facebook_callback
  if get_query_string_parameter("code")
    response = get facebook_exchange_url(get_root_url, get_query_string_parameter("code"))

    if response.body.include? "access_token"
      response.body =~ /access_token=([^&]+)(?:&expires=(.*))?/ # TODO: genericize get_query_string_parameter to handle this?
      token = $1 || ""
      expires = $2 || ""
      query_string = "facebook[token]=#{CGI.escape(token)}&facebook[expires]=#{CGI.escape(expires)}"
    else
      parsed = JSON.parse(response.body)
      query_string = to_params({:facebook => parsed})
    end
  else
    query_string = "facebook[error]=#{get_query_string_parameter("error")}"
  end

  body = %(<html><body>You are being redirected.</body></html>)
  headers = {
        'Location' => "#{get_root_url}#{MealTicket::Config.facebook_callback}?#{query_string}",
        'Content-Type' => 'text/html',
        'Content-Length' => body.length.to_s
  }
  [302, headers, [body]]
end

#flickr_callbackObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/meal_ticket.rb', line 105

def flickr_callback

  xml = get flickr_frob_url(get_query_string_parameter("frob"))

  parsed = Crack::XML.parse(xml.body)
  if parsed["rsp"]["stat"] == "ok"
    remote_id = parsed["rsp"]["auth"]["user"]["nsid"] || ""
    token = parsed["rsp"]["auth"]["token"] || ""
    query_string = "flickr[token]=#{CGI.escape(token)}&flickr[user_id]=#{CGI.escape(remote_id)}"
  else
    code = parsed["rsp"]["err"]["code"]
    msg = parsed["rsp"]["err"]["msg"]
    query_string = "flickr[error][code]=#{CGI.escape code}&flickr[error][msg]=#{CGI.escape msg}"
  end

  body = %(<html><body>You are being redirected.</body></html>)
  headers = {
        'Location' => "#{get_root_url}#{MealTicket::Config.flickr_callback}?#{query_string}",
        'Content-Type' => 'text/html',
        'Content-Length' => body.length.to_s
  }
  [302, headers, [body]]
end