Module: XmlrpcController

Defined in:
lib/xmlrpc_controller.rb,
lib/xmlrpc_controller/version.rb

Constant Summary collapse

VERSION =
"0.0.6"

Instance Method Summary collapse

Instance Method Details

#ifttt_defaultsObject

default handling methods for IFTTT to make a simple Webhook bridge



17
18
19
20
21
22
23
24
25
26
# File 'lib/xmlrpc_controller.rb', line 17

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



29
30
31
32
# File 'lib/xmlrpc_controller.rb', line 29

def ifttt_new_post(title, body, categories, tags)
  # 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(tags.first, body: body)
end

#metaWeblog_getCategories(args) ⇒ Object



58
59
60
# File 'lib/xmlrpc_controller.rb', line 58

def metaWeblog_getCategories(args)
  rpc_response("xml.value { xml.array { xml.data }}")
end

#metaWeblog_getRecentPosts(*args) ⇒ Object



44
45
46
# File 'lib/xmlrpc_controller.rb', line 44

def metaWeblog_getRecentPosts(*args)
  rpc_response("xml.value { xml.array { xml.data }}")
end

#metaWeblog_newPost(args) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/xmlrpc_controller.rb', line 48

def metaWeblog_newPost(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']] )
  tags = (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, tags)
  rpc_response("xml.value 'nothing'")
end

#mt_supportedMethods(*args) ⇒ Object

utility methods to make IFTTT believe we are a wordpress blog



40
41
42
# File 'lib/xmlrpc_controller.rb', line 40

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

#rpcObject

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)



8
9
10
11
12
13
14
# File 'lib/xmlrpc_controller.rb', line 8

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



35
36
37
# File 'lib/xmlrpc_controller.rb', line 35

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



62
63
64
# File 'lib/xmlrpc_controller.rb', line 62

def wp_newCategory(args)
  rpc_response("xml.value { xml.array { xml.data }}")
end