Module: Iftttify

Defined in:
lib/iftttify.rb,
lib/iftttify/version.rb,
lib/iftttify/iftttify.rb,
lib/iftttify/exceptions.rb

Defined Under Namespace

Modules: ActAsIFTTT, Exceptions

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.members_to_hash(members) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/iftttify/iftttify.rb', line 35

def self.members_to_hash(members)
  h = Hash.new
  members.each do |kv|
    if kv["value"]["string"]
      h[kv["name"].to_sym] = kv["value"]["string"]
    end
  end
  h
end

.process(request, options) ⇒ Object



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
# File 'lib/iftttify/iftttify.rb', line 4

def self.process(request, options)
  post = Struct.new :blog_name, :username, :password, :title, :body
    
  r = Hash.from_xml(request.body.read)
  
  methodName = r["methodCall"]["methodName"]

  if methodName == 'mt.supportedMethods'
    'metaWeblog.getRecentPosts'
  elsif methodName == 'metaWeblog.getRecentPosts'
    '<array><data></data></array>'
  elsif methodName == 'metaWeblog.newPost'
    username = r["methodCall"]["params"]["param"][1]["value"]["string"]
    password = r["methodCall"]["params"]["param"][2]["value"]["string"]
    if !options[:auth] || (options[:auth][:username] == username) && (options[:auth][:password] == password)
      h = Iftttify::members_to_hash(r["methodCall"]["params"]["param"][3]["value"]["struct"]["member"])
      yield post.new(
                     r["methodCall"]["params"]["param"][0]["value"]["string"],
                     username,
                     password,
                     h[:title],
                     h[:description]
                     )
    else
      raise Iftttify::Exceptions::InvalidCredential, "Failed to verify credential provided: [#{username}] with [#{password}]"
    end
  else
    raise Iftttify::Exceptions::InvalidMethod, "Failed to recognize #{methodName}"
  end
end