Report abuse

class Article < ActionWebService::Struct
  member :description, :string
  member :title, :string
  member :postid, :string
  member :url, :string
  member :link, :string
  member :permaLink, :string
  member :categories, [:string]
  member :dateCreated, :time

  def self.new_from_post(post, url)
    new(:description=>post.text,
                :title=>post.title,
                :postid=>post.id,
                :categories=>post.tags.collect(&:label),
                :url => url,
                :link => url,
                :permaLink => url,
                :dateCreated=>post.created_at
                )
  end
end

class Category < ActionWebService::Struct
  member :description, :string
  member :htmlUrl, :string
  member :rssUrl, :string
end

class MetaWeblogApi < ActionWebService::API::Base
  inflect_names false

  api_method :getRecentPosts,
              :expects => [{:blogid=>:string}, {:username=>:string}, {:password=>:string}, {:numberOfPosts=>:string}],
              :returns => [[Article]]

  api_method :newPost,
              :expects => [{:blogid=>:string}, {:username=>:string}, {:password=>:string}, {:struct=>Article}, {:publish=>:string}],
              :returns => [Article]

  api_method :editPost,
              :expects => [{:postid=>:string}, {:username=>:string}, {:password=>:string}, {:struct=>Article}, {:publish=>:string}],
              :returns => [Article]

  api_method :getPost,
              :expects => [{:postid=>:string}, {:username=>:string}, {:password=>:string}],
              :returns => [Article]

  api_method :getCategories,
              :expects => [{:blogid=>:string}, {:username=>:string}, {:password=>:string}],
              :returns => [[Category]]

end

class MetaWeblogService < ActionWebService::Base
  web_service_api MetaWeblogApi

  attr_accessor :controller

  def initialize(controller)
    @controller = controller
  end

  def getRecentPosts(blogid, email, password, limit)
    @author = get_author(email, password)
    limit = limit.to_i
    if @author
      articles = Array.new
      @author.posts[0, limit].each do |post|
        articles << Article.new_from_post(post, url_for_post(post))
      end
      articles
    else
      raise "Bad user".inspect
    end
  end

  def getCategories(blogid, email, password)
    @author = get_author(email, password)
    if @author
      categories = Array.new
      @author.tags.each do |tag|
        categories << Category.new(:description=>tag.label,
                                   :htmlUrl=>controller.url_for(:author=>@author, :controller=>'blog', :action=>'browse', :id=>tag),
                                   :rssUrl=>controller.url_for(:author=>@author, :controller=>'blog', :action=>'rss', :id=>tag)
                                   )
      end
      categories
    else
      raise "Bad user".inspect
    end
  end

  def getPost(postid, email, password)
    @author = get_author(email, password)
    post = Post.find(postid)
    if @author && post && (@author == post.author)
      Article.new_from_post(post, url_for_post(post))
    else
      raise "Bad user".inspect
    end
  end

  def editPost(postid, email, password, struct, publish)
    @author = get_author(email, password)
    post = Post.find(postid)
    if @author && post && (@author == post.author)
      post.title = struct[:title] || ''
      post.text = struct[:description] || ''
      post.my_format = 'html'
      if struct[:categories]
        struct[:categories].each do |c|
          post.tags = Array.new
          post.tags << Tag.find_by_label(c)
        end
      end
      if post.save
        Article.new_from_post(post, url_for_post(post) )
      else
        raise "Bad post".inspect
      end
    else
      raise "Bad user".inspect
    end
  end

  def newPost(blogid, email, password, struct, publish)
    @author = get_author(email, password)
    if @author
      post = Post.new(:author_id => @author)
      post.title = struct[:title] || ''
      post.text = struct[:description] || ''
      post.my_format = 'html'
      if struct[:categories]
        struct[:categories].each do |c|
          post.tags << Tag.find_by_label(c)
        end
      end
      if post.save
        Article.new_from_post(post, url_for_post(post) )
      else
        raise "Bad post".inspect
      end
    else
      raise "Bad user".inspect
    end
  end

  def url_for_post(post)
    controller.url_for(:author=>@author, :controller=>'blog', :action=>'view', :id=>post.id, :only_path => false)
  end

  def get_author(email, password)
    Author.login(email, password)
  end
end