Elvuel

善战者无赫赫之功

  • Home
  • Atom
  • About Elvuel

ActiveResource for no restful style

辛卯[兔]年 三月十九

上周群里有朋友在rails应用中需调用某api,不过接口非restful风格的,如要成功实现调用可以自行编写模块或引用第三方库,不过既然是在rails下,也可以拿AtciveResource来改动一下。

以下代码是个人做的一些小变更扩展来适应其api接口的调用--以下代码纯属实践娱乐,贴出来备忘。

# File config/initializers/active_resource_ext.rb

module ActiveResource

 module Formats
 module NoneFormat
 extend self

 def extension
 ""
 end

 def mime_type
 ""
 end

 def encode(hash, options = nil)
 # 具体处理看实际需要
 #ActiveSupport::JSON.encode(hash, options)
 ""
 end

 # to Base.instantiate_collection
 def decode(json)
 # 具体处理看实际需要
 json
 end
 end
 end
 # ActiveResource::Base
 class Base
 
 class 〈〈 self
 def element_path(id, prefix_options = {}, query_options = nil)
 prefix_options, query_options = split_options(prefix_options) if query_options.nil?
 if format.extension == ""
 "#{prefix(prefix_options)}#{collection_name}/#{URI.escape id.to_s}#{query_string(query_options)}"
 else
 "#{prefix(prefix_options)}#{collection_name}/#{URI.escape id.to_s}.#{format.extension}#{query_string(query_options)}"
 end
 end

 def new_element_path(prefix_options = {})
 if format.extension == ""
 "#{prefix(prefix_options)}#{collection_name}/new"
 else
 "#{prefix(prefix_options)}#{collection_name}/new.#{format.extension}"
 end
 end
 
 def collection_path(prefix_options = {}, query_options = nil)
 prefix_options, query_options = split_options(prefix_options) if query_options.nil?
 if format.extension == ""
 "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
 else
 "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
 end
 end

 def instantiate_collection(collection, prefix_options = {})
 if collection.is_a? Array
 collection.collect! { |record| instantiate_record(record, prefix_options) }
 else
 collection.to_s
 end
 end
 end
 end
 
 # ActiveResource::Connection
 class Connection
 private
 def build_request_headers(headers, http_method, uri)
 if format.mime_type == ""
 authorization_header(http_method, uri).update(default_header).update(headers)
 else
 authorization_header(http_method, uri).update(default_header).update(http_format_header(http_method)).update(headers)
 end
 end
 end
end

read more >>

Tags: activeresource rails restful api   Posted in Ruby & Rails

生成rails api文档

戊子[鼠]年 一月十八

进入命令行模式
x:〉rails elvuel
x:〉cd elvuel
x:〉elvuel〉rake rails:freeze:gems
接着执行:(执行完成后 将在doc/api目录下会有一份完整的RAILS API文档)
x:〉elvuel〉rake doc:rails

read more >>

Tags: rails api doc   Posted in Ruby & Rails

  • Search

  • Categories

    • Ruby & Rails
    • Jobs
    • Uncategorized
  • Recent Posts

    • Prawn使用Annotation坐...
    • ejabberd mod_archive
    • Capybara get session in ...
    • Juggernaut & Rails
    • RottenEgg发布
    • ruby rmagick 生成DM
    • ActiveResource for no r...
    • Rails生成Model文档
    • 转-Method triggers:inst...
    • 招聘-Ruby on Rails软件...
  • Links

    • RailsCasts
    • The Ruby Reflector
    • Alltop Ruby
    • Command Line Refer
    • Ruby Code Snippets
    • GitHub
    • API Dock
    • OpenSourceRails
    • RailsPlugins
    • RailsLab
    • 15DaysOfJQuery
  • Archives

    • 2012年(1)

    • 2012年01月(1)
    • 2011年(10)

    • 2011年12月(1)
    • 2011年11月(1)
    • 2011年08月(2)
    • 2011年04月(2)
    • 2011年03月(1)
    • 2011年01月(3)
    • 2010年(27)

    • 2010年12月(1)
    • 2010年11月(2)
    • 2010年10月(4)
    • 2010年09月(7)
    • 2010年08月(1)
    • 2010年07月(5)
    • 2010年06月(1)
    • 2010年04月(6)
    • 2009年(13)

    • 2009年10月(1)
    • 2009年05月(2)
    • 2009年04月(1)
    • 2009年03月(2)
    • 2009年02月(7)
    • 2008年(19)

    • 2008年08月(1)
    • 2008年07月(1)
    • 2008年06月(1)
    • 2008年05月(3)
    • 2008年04月(1)
    • 2008年03月(2)
    • 2008年02月(10)

Copyright © 2010 elvuel.com Powered by Elvuel.