Class: Wakame::CommandQueue::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/wakame/command_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(command_queue) ⇒ Adapter

Returns a new instance of Adapter.



74
75
76
# File 'lib/wakame/command_queue.rb', line 74

def initialize(command_queue)
  @command_queue = command_queue
end

Instance Method Details

#authentication(path, query) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/wakame/command_queue.rb', line 114

def authentication(path, query)
  key = Wakame.config.private_key
  req = query.split(/\&signature\=/)
  hash = OpenSSL::HMAC::digest(OpenSSL::Digest::SHA256.new, key, req[0])
  hmac = Base64.encode64(hash).gsub(/\+/, "").gsub(/\n/, "").to_s
  if hmac != path["signature"]
    raise "Authentication failed"
  end
end

#call(env) ⇒ Object



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
# File 'lib/wakame/command_queue.rb', line 78

def call(env)
  req = Rack::Request.new(env)
  begin
    unless req.get?().to_s == "true"
      raise "No Support Response"
    end
    query = req.query_string()
    params = req.params()
    if Wakame.config.enable_authentication == "true"
      auth = authentication(params, query)
    end
    cname = params["action"].split("_")
    begin
      cmd = eval("Command::#{(cname.collect{|c| c.capitalize}).join}").new 
      cmd.options = params
      command = @command_queue.send_cmd(cmd)

      if command.is_a?(Exception)
        status = 500
        body = json_encode(status, command.message)
      else
        status = 200
        body = json_encode(status, "OK", command)
      end
    rescue => e
      status = 404
      body = json_encode(status, e)
    end
  rescue => e
    status = 403
    body = json_encode(status, e)
    Wakame.log.error(e)
  end
  [ status, {'Content-Type' => 'text/javascript+json'}, body]
end

#json_encode(status, message, data = nil) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/wakame/command_queue.rb', line 124

def json_encode(status, message, data=nil)
  if status == 200 && data.is_a?(Hash)
    body = [{:status=>status, :message=>message}, {:data=>data}].to_json
  else
    body = [{:status=>status, :message=>message}].to_json
  end
  body
end