这次我们来为第三方可户提供一个 WEB SERVICES 的服务吧 也就是 server side
很简单, 首先在 api/apis 目录下 给一个层 layer
文件名称 greeting_api.rb
class GreetingApi < ActionWebService::API::Base
api_method :dogreeting,
:expects => [{:name=>:string}, {:content_type=>:string}, {:data=>:base64}],
:returns => [{:greeting => :string}]
end
controller 中加一个 greeting_controller.rb
class GreetingController < ApplicationController
wsdl_service_name 'Greeting'
def dogreeting(name, content_type, data)
raise "No data" unless (data.length > 0)
# Write the data to a temp file
puts data
#data = "e:/sqt.rb"
temp_file = File.new("e://stingray.jpg",'wb')
#temp_file.path = "e://"
file = File.open(data, 'rb')
temp_file << file.read
#temp_file << data
puts temp_file.path
temp_file.close
#temfile.close
#new_file = self.new(:name => name, :mime_type => content_type,:user => user)
# The following method takes the supplied file path and puts it in
# the right place on the system (in my case it actually does a few
# things with it, and then moves it to an image server
#new_asset.file = file_path
#new_asset.save
return "hello babe!"
end
end
运行一个功能测试
require File.dirname(__FILE__) + '/../test_helper'
require 'greeting_controller'
class GreetingController; def rescue_action(e) raise e end; end
class GreetingControllerApiTest < Test::Unit::TestCase
def setup
@controller = GreetingController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_dogreeting #(name, content_type, data)
result = invoke :dogreeting, "Joe", "gb2312","e://calender.jpg" # this is the real test call
#assert_equal "Hello Joe", result
puts result
end
end
很简单, 首先在 api/apis 目录下 给一个层 layer
文件名称 greeting_api.rb
class GreetingApi < ActionWebService::API::Base
api_method :dogreeting,
:expects => [{:name=>:string}, {:content_type=>:string}, {:data=>:base64}],
:returns => [{:greeting => :string}]
end
controller 中加一个 greeting_controller.rb
class GreetingController < ApplicationController
wsdl_service_name 'Greeting'
def dogreeting(name, content_type, data)
raise "No data" unless (data.length > 0)
# Write the data to a temp file
puts data
#data = "e:/sqt.rb"
temp_file = File.new("e://stingray.jpg",'wb')
#temp_file.path = "e://"
file = File.open(data, 'rb')
temp_file << file.read
#temp_file << data
puts temp_file.path
temp_file.close
#temfile.close
#new_file = self.new(:name => name, :mime_type => content_type,:user => user)
# The following method takes the supplied file path and puts it in
# the right place on the system (in my case it actually does a few
# things with it, and then moves it to an image server
#new_asset.file = file_path
#new_asset.save
return "hello babe!"
end
end
运行一个功能测试
require File.dirname(__FILE__) + '/../test_helper'
require 'greeting_controller'
class GreetingController; def rescue_action(e) raise e end; end
class GreetingControllerApiTest < Test::Unit::TestCase
def setup
@controller = GreetingController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_dogreeting #(name, content_type, data)
result = invoke :dogreeting, "Joe", "gb2312","e://calender.jpg" # this is the real test call
#assert_equal "Hello Joe", result
puts result
end
end