Module: XmlrpcController
- Defined in:
- lib/xmlrpc_controller.rb,
lib/xmlrpc_controller/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
-
#ifttt_defaults ⇒ Object
default handling methods for IFTTT to make a simple Webhook bridge.
-
#ifttt_new_post(title, body, categories, tags) ⇒ Object
actual method doing the webhook request.
- #metaWeblog_getCategories(args) ⇒ Object
- #metaWeblog_getRecentPosts(*args) ⇒ Object
- #metaWeblog_newPost(args) ⇒ Object
-
#mt_supportedMethods(*args) ⇒ Object
utility methods to make IFTTT believe we are a wordpress blog.
-
#rpc ⇒ Object
the main method handling the RPC request.
-
#rpc_response(value) ⇒ Object
convenience method to send back a xmlrpc response.
- #wp_newCategory(args) ⇒ Object
Instance Method Details
#ifttt_defaults ⇒ Object
default handling methods for IFTTT to make a simple Webhook bridge
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/xmlrpc_controller.rb', line 16 def ifttt_defaults @@xmlrpc_method_name_map ||= {} @@xmlrpc_method_name_map.merge!({ "metaWeblog.getRecentPosts" => :metaWeblog_getRecentPosts, "mt.supportedMethods" => :mt_supportedMethods, "metaWeblog.newPost" => :metaWeblog_newPost, "metaWeblog.getCategories" => :metaWeblog_getCategories, "wp.newCategory" => :wp_newCategory }) end |
#ifttt_new_post(title, body, categories, tags) ⇒ Object
actual method doing the webhook request
28 29 30 31 |
# File 'lib/xmlrpc_controller.rb', line 28 def ifttt_new_post(title, body, categories, ) # default protocol to respond to a new post is to POST a request to the url as specified in the tags, body is the payload HTTParty.post(.first, body: body) end |
#metaWeblog_getCategories(args) ⇒ Object
57 58 59 |
# File 'lib/xmlrpc_controller.rb', line 57 def (args) rpc_response("xml.value { xml.array { xml.data }}") end |
#metaWeblog_getRecentPosts(*args) ⇒ Object
43 44 45 |
# File 'lib/xmlrpc_controller.rb', line 43 def (*args) rpc_response("xml.value { xml.array { xml.data }}") end |
#metaWeblog_newPost(args) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/xmlrpc_controller.rb', line 47 def (args) new_post_params = Hash.from_xml(args.children[3].to_s)['param']['value']['struct']['member'] title = new_post_params[0]['value']['string'] body = new_post_params[1]['value']['string'] categories = (new_post_params[2]['value']['array']['data']['value'].class == Array ? new_post_params[2]['value']['array']['data']['value'].map { |e| e['string'] } : [new_post_params[2]['value']['array']['data']['value']['string']] ) = (new_post_params[3]['value']['array']['data']['value'].class == Array ? new_post_params[3]['value']['array']['data']['value'].map { |e| e['string'] } : [new_post_params[3]['value']['array']['data']['value']['string']] ) ifttt_new_post(title, body, categories, ) rpc_response("xml.value 'nothing'") end |
#mt_supportedMethods(*args) ⇒ Object
utility methods to make IFTTT believe we are a wordpress blog
39 40 41 |
# File 'lib/xmlrpc_controller.rb', line 39 def mt_supportedMethods(*args) rpc_response("xml.value { xml.array {xml.data { xml.value { xml.string 'metaWeblog.getRecentPosts' } \n xml.value { xml.string 'metaWeblog.newPost' } }}} ") end |
#rpc ⇒ Object
the main method handling the RPC request. It parses the method name, and passes the Nokogiri XML node that represents the params @@xmlrpc_method_name_map contains a map to resolve for non-ruby method names (with a . for example)
7 8 9 10 11 12 13 |
# File 'lib/xmlrpc_controller.rb', line 7 def rpc parsed_request = Nokogiri::XML.parse(request.body.read) method_name = parsed_request.xpath('methodCall/methodName').text method_params = parsed_request.xpath('methodCall/params') method_name = @@xmlrpc_method_name_map[method_name] || method_name self.send(method_name, method_params) end |
#rpc_response(value) ⇒ Object
convenience method to send back a xmlrpc response
34 35 36 |
# File 'lib/xmlrpc_controller.rb', line 34 def rpc_response(value) render inline: "xml.instruct! :xml, :version=>'1.0' \n xml.methodResponse { xml.params { xml.param { #{value} }}}", type: :builder, content_type: "text/xml" end |
#wp_newCategory(args) ⇒ Object
61 62 63 |
# File 'lib/xmlrpc_controller.rb', line 61 def wp_newCategory(args) rpc_response("xml.value { xml.array { xml.data }}") end |