こんにちは、外部ゲートウェイから Ruby on Rails のデータベースにメッセージをインポートしようとしています。メッセージは JSON を使用して送信されますが、何を取得してもエラーが発生します。
757: unexpected token at 'sting another 8:16 09\/02\/2013","from":"254720321222","to":"2658","date":"2013-02-09 17:17:19"},{"id":256069,"text":"Wajpt","from":"254704476069","to":"2658","date":"2013-02-09 20:12:30"},{"id":256510,"text":"STOP","from":"254789934061","to":"2658","date":"2013-02-10 07:20:01"},{"id":256774,"text":"Adan","from":"254729933301","to":"2658","date":"2013-02-10 10:36:11"},{"id":257039,"text":"1967","from":"254700365399","to":"2658","date":"2013-02-10 14:13:55"}]}}
これは私のコントローラコードです:
require 'AfricasTalkingGateway'
class MessageController < ApplicationController
username = 'username'
apikey = 'apikey'
gateway = AfricasTalkingGateway.new(username, apikey)
last_received_id = 212815
while true
messages = gateway.fetch_messages(last_received_id)
messages.each {|x|
puts 'from' + x.from + 'to' + x.to + 'text' + x.text + 'date' + x.date
last_received_id = x.id
}
break if messages.length == 0
def create
@message = Message.new(:from => x.from, :to => x.to, :date => x.date, :text => x.text, :giud => x.id)
@message.save
def index
@message = Message.find(:all)
end
end
そして私のゲートウェイrbファイル:
require 'rubygems'
require 'curb'
require 'json'
class AfricasTalkingGatewayAuthenticationError < Exception
end
class AfricasTalkingGatewayUnexpectedError < Exception
end
class SMSMessage
attr_accessor :id, :text, :from, :to, :date
def initialize(m_id, m_text,m_from, m_to ,m_date)
@id = m_id
@text = m_text
@from = m_from
@to = m_to
@date = m_date
end
end
class StatusReport
attr_accessor :number, :status, :cost
def initialize(m_number, m_status, m_cost)
@number = m_number
@status = m_status
@cost = m_cost
end
end
class AfricasTalkingGateway
#Constants
URL = 'https://api.africastalking.com/version1/messaging'
ACCEPT_TYPE = 'application/json'
def initialize(user_name,api_key)
@user_name = user_name
@api_key = api_key
end
def send_message(recipients, message)
data = nil
response_code = nil
post_body = {:username => @user_name, :message => message, :to => recipients }
http = Curl.post(URL, post_body) do |curl|
curl.headers['Accept'] = ACCEPT_TYPE
curl.headers['apiKey'] = @api_key
curl.verbose = true
curl.on_body { |body|
data = body
body.to_s.length
}
curl.on_complete { |resp| response_code = resp.response_code }
end
raise AfricasTalkingGatewayAuthenticationError if response_code == 401
raise AfricasTalkingGatewayUnexpectedError "Unexpected error occured" if response_code != 201 or data.nil?
reports = JSON.parse(data)["SMSMessageData"]["Recipients"].collect { |entry|
StatusReport.new entry["number"], entry["status"], entry["cost"]
}
return reports
end
def fetch_messages(last_received_id)
data = nil
response_code = nil
http = Curl.get("#{URL}?username=#{@user_name}&lastReceivedId=#{last_received_id}") do |curl|
curl.headers['Accept'] = ACCEPT_TYPE
curl.headers['apiKey'] = @api_key
curl.verbose = false
curl.on_body { |body|
data = body
body.to_s.length
}
curl.on_complete { |resp| response_code = resp.response_code }
end
raise AfricasTalkingGatewayAuthenticationError if response_code == 401
raise AfricasTalkingGatewayUnexpectedError "Data is nil for some unexpected reason" if response_code != 200 or data.nil?
messages = JSON.parse(data)["SMSMessageData"]["Messages"].collect { |msg|
SMSMessage.new msg["id"], msg["text"], msg["from"] , msg["to"], msg["date"]
}
return messages
end
end
この問題の解決を手伝ってください