aboutsummaryrefslogtreecommitdiffstats
path: root/actionwebservice/examples/metaWeblog/blog_controller.rb
blob: aff2e909ea7fbcb7a90dc499f7a97e3370ee35eb (plain) (blame)
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
# point your client at http://project_url/blog/api to test
# this

# structures as defined by the metaWeblog/blogger
# specifications.
module Blog
  class Enclosure < ActionService::Struct
    member :url,    :string
    member :length, :int
    member :type,   :string
  end

  class Source < ActionService::Struct
    member :url,  :string
    member :name, :string
  end

  class Post < ActionService::Struct
    member :title,       :string
    member :link,        :string
    member :description, :string
    member :author,      :string
    member :category,    :string
    member :comments,    :string
    member :enclosure,   Enclosure
    member :guid,        :string
    member :pubDate,     :string
    member :source,      Source
  end

  class Blog < ActionService::Struct
    member :url,      :string
    member :blogid,   :string
    member :blogName, :string
  end
end

# skeleton metaWeblog API
class MetaWeblogAPI < ActionService::API::Base
  inflect_names false

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

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

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

  api_method :getUsersBlogs, :returns => [[Blog::Blog]], :expects => [
    {:appkey=>:string},
    {:username=>:string},
    {:password=>:string},
  ]

  api_method :getRecentPosts, :returns => [[Blog::Post]], :expects => [
    {:blogid=>:string},
    {:username=>:string},
    {:password=>:string},
    {:numberOfPosts=>:int},
  ]
end

class BlogController < ApplicationController
  web_service_api MetaWeblogAPI

  def initialize
    @postid = 0
  end

  def newPost
    $stderr.puts 'Creating post: username=%s password=%s struct=%s' % [
      @params['username'],
      @params['password'],
      @params['struct'].inspect
    ]
    (@postid += 1).to_s
  end

  def editPost
    $stderr.puts 'Editing post: username=%s password=%s struct=%s' % [
      @params['username'],
      @params['password'],
      @params['struct'].inspect
    ]
    true
  end

  def getUsersBlogs
    $stderr.puts "Returning user %s's blogs" % @params['username']
    blog = Blog::Blog.new(
      :url =>'http://blog.xeraph.org',
      :blogid => 'sttm',
      :blogName => 'slave to the machine'
    )
    [blog]
  end

  def getRecentPosts
    $stderr.puts "Returning recent posts (%d requested)" % @params['numberOfPosts']
    post1 = Blog::Post.new(
      :title => 'first post!',
      :link => 'http://blog.xeraph.org/testOne.html',
      :description => 'this is the first post'
    )
    post2 = Blog::Post.new(
      :title => 'second post!',
      :link => 'http://blog.xeraph.org/testTwo.html',
      :description => 'this is the second post'
    )
    [post1, post2]
  end
end