Web services for RoR 2

这次我们来为第三方可户提供一个  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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值