blogger_service.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:3k
源码类别:

Ajax

开发平台:

Others

  1. module BloggerStructs
  2.   class Blog < ActionWebService::Struct
  3.     member :url,      :string
  4.     member :blogid,   :string
  5.     member :blogName, :string
  6.   end
  7.   class User < ActionWebService::Struct
  8.     member :userid, :string
  9.     member :firstname, :string
  10.     member :lastname, :string
  11.     member :nickname, :string
  12.     member :email, :string
  13.     member :url, :string
  14.   end
  15. end
  16. class BloggerApi < ActionWebService::API::Base
  17.   inflect_names false
  18.   api_method :deletePost,
  19.     :expects => [ {:appkey => :string}, {:postid => :int}, {:username => :string}, {:password => :string},
  20.                   {:publish => :bool} ],
  21.     :returns => [:bool]
  22.   api_method :getUserInfo,
  23.     :expects => [ {:appkey => :string}, {:username => :string}, {:password => :string} ],
  24.     :returns => [BloggerStructs::User]
  25.   api_method :getUsersBlogs,
  26.     :expects => [ {:appkey => :string}, {:username => :string}, {:password => :string} ],
  27.     :returns => [[BloggerStructs::Blog]]
  28.   api_method :newPost,
  29.     :expects => [ {:appkey => :string}, {:blogid => :string}, {:username => :string}, {:password => :string},
  30.                   {:content => :string}, {:publish => :bool} ],
  31.     :returns => [:int]
  32. end
  33. class BloggerService < TypoWebService
  34.   web_service_api BloggerApi
  35.   before_invocation :authenticate
  36.   def deletePost(appkey, postid, username, password, publish)
  37.     Article.destroy(postid)
  38.     true
  39.   end
  40.   def getUserInfo(appkey, username, password)
  41.     BloggerStructs::User.new(
  42.       :userid => username,
  43.       :firstname => "",
  44.       :lastname => "",
  45.       :nickname => username,
  46.       :email => "",
  47.       :url => this_blog.base_url
  48.     )
  49.   end
  50.   def getUsersBlogs(appkey, username, password)
  51.     [BloggerStructs::Blog.new(
  52.       :url      => this_blog.base_url,
  53.       :blogid   => this_blog.id,
  54.       :blogName => this_blog.blog_name
  55.     )]
  56.   end
  57.   def newPost(appkey, blogid, username, password, content, publish)
  58.     title, categories, body = content.match(%r{^<title>(.+?)</title>(?:<category>(.+?)</category>)?(.+)$}mi).captures rescue nil
  59.     article = Article.new
  60.     article.body           = body || content || ''
  61.     article.title          = title || content.split.slice(0..5).join(' ') || ''
  62.     article.published      = publish
  63.     article.author         = username
  64.     article.published_at   = Time.now
  65.     article.user           = @user
  66.     article.allow_comments = this_blog.default_allow_comments
  67.     article.allow_pings    = this_blog.default_allow_pings
  68.     article.text_filter    = this_blog.text_filter
  69.     article.save
  70.     if categories
  71.       categories.split(",").each do |c|
  72.         article.categories << Category.find_by_name(c.strip) rescue nil
  73.       end
  74.     end
  75.     article.id
  76.   end
  77. end