apn_for_rails gem 实现问题
我已经正确安装并配置了 apn_for_rails gem,由于我对 Ruby on Rails 不熟悉,我遇到的问题可能是语法问题。
文件push_notification.rb下有这个
class ApnDevice < ActiveRecord::Base
end
我在/models中的routes.rb
match '/api/v2.0/RegisterIOSDevice', :to => Api::V_2_0::ApiNotifications
,然后我在/lib/api/v_2_0中有另一个名为api_notifications.rb的文件,
require 'rubygems'
require 'sinatra/base'
require 'nokogiri'
require 'apn_on_rails'
module Api
module V_2_0
class ApiPushNotification < sinatra::Base
include ApplicationHelper
include Api::V_2_0::ApiResponse
include ApiUtil
def push_notification(params)
status = -1
error = nil
begin
if params[:device_token].blank?
return status_respnse(params,status,'device_token cant be blank')
end
existing_device_token = ApnDevice.find_by_token(params[:device_token])
if existing_device_token
return status_response(params,status,'Token already exists for device')
end
token = ApnDevice.create!(:token => params[:device_token])
rescue Exception => e
error = e.message
Rails.logger.info "#{e.class} : #{e.message}"
Rails.logger.info e.backtrace.join("\n")
rescue => e
error = e
Rails.logger.info "Caught exception: #{e}"
Rails.logger.info e.backtrace.join("\n")
end #end of rescue
status_response(params,status,error)
end
get '/api/v2.0/RegisterIOSDevice.:format' do
push_notification(params)
end
post '/api/v2.0/RegisterIosDevice.:format' do
push_notification(params)
end
end #end of class ApiPushNotification
end #end of module V_2_0
end #end of module Api
但当我运行我的服务器时,它给了我这个巨大的错误,我不会发布整个内容,但它基本上说这
/lib/api/v_2_0/api_notifications.rb:7: invalid multibyte char (US-ASCII) (SyntaxError)
/lib/api/v_2_0/api_notifications.rb:7: syntax error, unexpected $end, expecting keyword_end
后面是一个堆栈跟踪,我不会发布,但我似乎找不到问题,有什么想法吗?
编辑:
好的,我添加了 utf-8 建议,现在我收到一个新错误:
lib/api/v_2_0/api_notifications.rb:46: syntax error, unexpected keyword_end, expecting $end (SyntaxError)
I have the apn_for_rails gem installed and configured correctly, the problem I'm running into is probably a syntax problem due to my newness to Ruby on Rails.
I have this in /models under the file push_notification.rb
class ApnDevice < ActiveRecord::Base
end
in routes.rb
match '/api/v2.0/RegisterIOSDevice', :to => Api::V_2_0::ApiNotifications
and then I have another file in /lib/api/v_2_0 called api_notifications.rb
require 'rubygems'
require 'sinatra/base'
require 'nokogiri'
require 'apn_on_rails'
module Api
module V_2_0
class ApiPushNotification < sinatra::Base
include ApplicationHelper
include Api::V_2_0::ApiResponse
include ApiUtil
def push_notification(params)
status = -1
error = nil
begin
if params[:device_token].blank?
return status_respnse(params,status,'device_token cant be blank')
end
existing_device_token = ApnDevice.find_by_token(params[:device_token])
if existing_device_token
return status_response(params,status,'Token already exists for device')
end
token = ApnDevice.create!(:token => params[:device_token])
rescue Exception => e
error = e.message
Rails.logger.info "#{e.class} : #{e.message}"
Rails.logger.info e.backtrace.join("\n")
rescue => e
error = e
Rails.logger.info "Caught exception: #{e}"
Rails.logger.info e.backtrace.join("\n")
end #end of rescue
status_response(params,status,error)
end
get '/api/v2.0/RegisterIOSDevice.:format' do
push_notification(params)
end
post '/api/v2.0/RegisterIosDevice.:format' do
push_notification(params)
end
end #end of class ApiPushNotification
end #end of module V_2_0
end #end of module Api
but it gives me this huge error when I run my server, I won't post the whole thing, but it says this basically
/lib/api/v_2_0/api_notifications.rb:7: invalid multibyte char (US-ASCII) (SyntaxError)
/lib/api/v_2_0/api_notifications.rb:7: syntax error, unexpected $end, expecting keyword_end
this is followed by a stack trace which I will not post but I can't seem to find the problem, any ideas?
Edit:
ok I added the utf-8 suggestion, now I'm getting a new error:
lib/api/v_2_0/api_notifications.rb:46: syntax error, unexpected keyword_end, expecting $end (SyntaxError)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道你在那里做什么,但是如果你在 lib 中使用 Sinatra 而不是,为什么还要使用 Rails 呢?
对于您的错误,请在第一行添加以下代码:
I don't know what you are doing there, but why are you using Rails if you use Sinatra instead in lib?
To your error, add the following code at the first line: