public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
technoweenie (author)
Sat Sep 09 10:40:11 -0700 2006
commit  3746ecbc9dc0b57038bba61d0bfc9e0118189798
tree    bfa67a9486bf04011be725df392516eaf7227e8b
parent  a29900652b6aa6c1d72b8bb3b3d2d21c01a0982c
mephisto / app / apis / meta_weblog_api.rb
100644 138 lines (116 sloc) 5.241 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
module MetaWeblogStructs
  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 :mt_text_more, :string
    member :mt_excerpt, :string
    member :mt_keywords, :string
    member :mt_allow_comments, :int
    member :mt_allow_pings, :int
    member :mt_convert_breaks, :string
    member :mt_tb_ping_urls, [:string]
    member :dateCreated, :time
  end
 
  class MediaObject < ActionWebService::Struct
    member :bits, :string
    member :name, :string
    member :type, :string
  end
 
  class Url < ActionWebService::Struct
    member :url, :string
  end
end
 
class MetaWeblogApi < ActionWebService::API::Base
  inflect_names false
 
  api_method :getCategories,
    :expects => [ {:blogid => :string}, {:username => :string}, {:password => :string} ],
    :returns => [[:string]]
 
  api_method :getPost,
    :expects => [ {:postid => :string}, {:username => :string}, {:password => :string} ],
    :returns => [MetaWeblogStructs::Article]
 
  api_method :getRecentPosts,
    :expects => [ {:blogid => :string}, {:username => :string}, {:password => :string}, {:numberOfPosts => :int} ],
    :returns => [[MetaWeblogStructs::Article]]
 
  api_method :deletePost,
    :expects => [ {:appkey => :string}, {:postid => :string}, {:username => :string}, {:password => :string}, {:publish => :int} ],
    :returns => [:bool]
 
  api_method :editPost,
    :expects => [ {:postid => :string}, {:username => :string}, {:password => :string}, {:struct => MetaWeblogStructs::Article}, {:publish => :int} ],
    :returns => [:bool]
 
  api_method :newPost,
    :expects => [ {:blogid => :string}, {:username => :string}, {:password => :string}, {:struct => MetaWeblogStructs::Article}, {:publish => :int} ],
    :returns => [:string]
 
  api_method :newMediaObject,
    :expects => [ {:blogid => :string}, {:username => :string}, {:password => :string}, {:data => MetaWeblogStructs::MediaObject} ],
    :returns => [MetaWeblogStructs::Url]
end
 
class MetaWeblogService < XmlRpcService
  web_service_api MetaWeblogApi
  before_invocation :authenticate
 
  def getCategories(blogid, username, password)
    site.sections.find(:all).collect &:name
  end
 
  def getPost(postid, username, password)
    article = @user.articles.find(postid)
    article_dto_from(article)
  end
 
  def getRecentPosts(blogid, username, password, numberOfPosts)
    @user.articles.find(:all, :order => "created_at DESC", :limit => numberOfPosts).collect{ |c| article_dto_from(c) }
  end
 
  def newPost(blogid, username, password, struct, publish)
    article = @user.articles.build :site => site
    post_it(article, username, password, struct, publish)
  end
  
  def editPost(postid, username, password, struct, publish)
    article = @user.articles.find(postid)
    post_it(article, username, password, struct, publish)
    true
  end
  
  def deletePost(appkey, postid, username, password, publish)
    article = @user.articles.find(postid)
    article.destroy
    true
  end
 
  #def newMediaObject(blogid, username, password, data)
  # resource = @user.resources.create(:filename => data['name'], :created_at => Time.now)
  # resource.write_to_disk(data['bits'])
  #
  # MetaWeblogStructs::Url.new("url" => controller.url_for(:controller => "/files/#{resource.filename}"))
  #end
 
  def article_dto_from(article)
    MetaWeblogStructs::Article.new(
      :description => article.body,
      :title => article.title,
      :postid => article.id.to_s,
      :url => article_url(article).to_s,
      :link => article_url(article).to_s,
      :permaLink => article.permalink.to_s,
      :categories => article.sections.collect { |c| c.name },
      :mt_text_more => article.body.to_s,
      :mt_excerpt => article.excerpt.to_s,
      # :mt_keywords => article.keywords.to_s,
      # :mt_allow_comments => article.allow_comments? ? 1 : 0,
      # :mt_allow_pings => article.allow_pings? ? 1 : 0,
      # :mt_convert_breaks => (article.text_filter.name.to_s rescue ''),
      # :mt_tb_ping_urls => article.pings.collect { |p| p.url },
      :dateCreated => (article.published_at rescue "")
      )
  end
 
  protected
    def article_url(article)
      article.published? && article.full_permalink
    end
    
    def post_it(article, user, password, struct, publish)
      article.attributes = {:updater => @user, :section_ids => Section.find(:all, :conditions => ['name IN (?)', struct['categories']]).collect(&:id),
        :body => struct['description'].to_s, :title => struct['title'].to_s, :excerpt => struct['mt_excerpt'].to_s}
      utc_date = Time.utc(struct['dateCreated'].year, struct['dateCreated'].month, struct['dateCreated'].day, struct['dateCreated'].hour, struct['dateCreated'].sec, struct['dateCreated'].min) rescue article.published_at
 
      article.published_at = publish == true ? utc_date : nil
      article.save!
      article.id
    end
end