Skip to content

How do I do ... with Webmachine?

kgish edited this page Jan 2, 2015 · 5 revisions

Add CORS headers

def finish_request
  response.headers['Access-Control-Allow-Origin'] = '*'
  response.headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, ...'
  response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE'
  end
end

Return a 201 from a PUT

def allowed_methods
  ["PUT"]
end

def content_types_accepted
  [["application/json", :from_json]]
end

def resource_exists?
  widget
end

def from_json
  response_code = widget ? 200 : 201
  # create or update widget...
  response_code
end

private

def widget
  @widget ||= Widget.find(request.path_info[:id])
end

Implement cookie based authentication

def is_authorized?
  # Implement any logic you like here, return truthy or falsey
end