Skip to content

Instantly share code, notes, and snippets.

@gnepud
Forked from rkh/rack_example.ru
Created June 17, 2012 18:12

Revisions

  1. @rkh rkh revised this gist Dec 8, 2011. 1 changed file with 9 additions and 17 deletions.
    26 changes: 9 additions & 17 deletions rack_example.ru
    Original file line number Diff line number Diff line change
    @@ -37,25 +37,17 @@ end

    class IdeaAPI
    def call(env)
    request = Rack::Request.new(env)
    case request.request_method
    case env['REQUEST_METHOD']
    when 'POST'
    begin
    idea = Idea.new(request.params)
    rescue Idea::InvalidParams => error
    [400, {"Content-Type" => "text/html"}, [error.message] ]
    else
    Idea.store << idea
    [200, {"Content-Type" => "text"}, ["Idea added, currently #{Idea.store.size} ideas are in memory!"]]
    end
    Idea.store << Idea.new(request.params)
    return [200, {"Content-Type" => "text/plain"}, ["Idea added, currently #{Idea.store.size} ideas are in memory!"]]
    when 'GET'
    [200, {"Content-Type" => "text"}, [Idea.store.map{|idea, idx| idea.to_s }.join("\n\n") + "\n"]]
    else
    [404, {}, ["Did you get lost?"]]
    end
    return [200, {"Content-Type" => "text/plain"}, [Idea.store.map{|idea, idx| idea.to_s }.join("\n\n") + "\n"]]
    end if env['PATH_INFO'] == '/ideas'
    [404, {}, ["Did you get lost?"]]
    rescue Idea::InvalidParams => error
    [400, {"Content-Type" => "text/plain"}, [error.message]]
    end
    end

    map '/ideas' do
    run IdeaAPI.new
    end
    run IdeaAPI.new
  2. @mattetti mattetti renamed this gist Dec 8, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @mattetti mattetti created this gist Dec 8, 2011.
    61 changes: 61 additions & 0 deletions rack_example.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    #########################################
    # Very basic rack application showing how to use a router based on the uri
    # and how to process requests based on the HTTP method used.
    #
    # Usage:
    # $ rackup rack_example.ru
    #
    # $ curl -X POST -d 'title=retire&body=I should retire in a nice island' localhost:9292/ideas
    # $ curl -X POST -d 'title=ask Satish for a gift&body=Find a way to get Satish to send me a gift' localhost:9292/ideas
    # $ curl localhost:9292/ideas
    #
    #########################################

    class Idea
    attr_accessor :title, :body, :created_at

    # Memory store, gets cleared as the process is restarted
    def self.store
    @ideas ||= []
    end

    class InvalidParams < StandardError; end

    # create an instance based on some passed params
    def initialize(params)
    raise InvalidParams, "You need to provide at least a title" unless params['title']
    self.title = params['title']
    self.body = params['body']
    self.created_at = Time.now
    end

    # Converts an instance into a string
    def to_s
    "#{title} at #{created_at.to_s}\n#{body}"
    end
    end

    class IdeaAPI
    def call(env)
    request = Rack::Request.new(env)
    case request.request_method
    when 'POST'
    begin
    idea = Idea.new(request.params)
    rescue Idea::InvalidParams => error
    [400, {"Content-Type" => "text/html"}, [error.message] ]
    else
    Idea.store << idea
    [200, {"Content-Type" => "text"}, ["Idea added, currently #{Idea.store.size} ideas are in memory!"]]
    end
    when 'GET'
    [200, {"Content-Type" => "text"}, [Idea.store.map{|idea, idx| idea.to_s }.join("\n\n") + "\n"]]
    else
    [404, {}, ["Did you get lost?"]]
    end
    end
    end

    map '/ideas' do
    run IdeaAPI.new
    end