{"absolute_url": "/hkairi/rackresctrictor-is-a-middleware-that-helps-you-managing-access-to-your-applications/", "blog_post": false, "code": "require 'ipaddr'\nmodule Rack\n\tclass Restrictor\n\n\t\tPAGE_CODE = <<-EOCODE\n\n\t\n\t\t\n\t\t\t Anauthorized access\n\t\t\n\t\t\n\t\t\t

Anauthorized access

\n\t\t\n\t\nEOCODE\n\n\tdef initialize(app, plage)\n\t\t@app = app\n\t\t@plage = IPAddr.new( plage.to_s )\n\t\tp @plage\n\tend\n\n\tdef call(env)\n\t\tif authorized?( env['REMOTE_ADDR'].to_s )\n\t\t\tstatus, headers, body = @app.call(env)\n\t\telse\n\t\t\tstatus, headers, body = \n\t\t\t401, {\"Content-Type\" => \"text/html\"}, PAGE_CODE\n\t\tend\n\n\t\t[status, headers, body]\n\tend\n\n\tprivate\n\n\tdef authorized?( ip )\n\t\t@plage.include?(IPAddr.new( ip ))\n\tend\n\n\tend\nend", "created": "2011-01-17T06:01:34", "description": null, "description_rendered": "None", "embed_url": "https://snipt.net/embed/de177ddf863eb9c7b69257258df36eb7/", "full_absolute_url": "/hkairi/rackresctrictor-is-a-middleware-that-helps-you-managing-access-to-your-applications/", "id": 22964, "lexer": "rb", "line_count": 42, "log_entries": [], "meta": null, "modified": "2019-06-24T08:58:13.142006", "publish_date": null, "raw_url": "/raw/de177ddf863eb9c7b69257258df36eb7/", "resource_uri": "/api/public/snipt/22964/", "slug": "rackresctrictor-is-a-middleware-that-helps-you-managing-access-to-your-applications", "stylized": "
 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42
require 'ipaddr'\nmodule Rack\n\tclass Restrictor\n\n\t\tPAGE_CODE = <<-EOCODE\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n\t"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n\t<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n\t\t<head>\n\t\t\t<title> Anauthorized access</title>\n\t\t</head>\n\t\t<body>\n\t\t\t<h1> Anauthorized access</h1>\n\t\t</body>\n\t</html>\nEOCODE\n\n\tdef initialize(app, plage)\n\t\t@app   = app\n\t\t@plage = IPAddr.new( plage.to_s )\n\t\tp @plage\n\tend\n\n\tdef call(env)\n\t\tif authorized?( env['REMOTE_ADDR'].to_s )\n\t\t\tstatus, headers, body = @app.call(env)\n\t\telse\n\t\t\tstatus, headers, body = \n\t\t\t401, {"Content-Type" => "text/html"}, PAGE_CODE\n\t\tend\n\n\t\t[status, headers, body]\n\tend\n\n\tprivate\n\n\tdef authorized?( ip )\n\t\t@plage.include?(IPAddr.new( ip ))\n\tend\n\n\tend\nend\n
\n
", "tags": [{"absolute_url": "/public/tag/ruby/", "id": 65, "name": "ruby", "resource_uri": "/api/public/tag/65/", "snipts": "/api/public/snipt/?tag=65"}, {"absolute_url": "/public/tag/server/", "id": 84, "name": "server", "resource_uri": "/api/public/tag/84/", "snipts": "/api/public/snipt/?tag=84"}, {"absolute_url": "/public/tag/middleware/", "id": 3761, "name": "middleware", "resource_uri": "/api/public/tag/3761/", "snipts": "/api/public/snipt/?tag=3761"}, {"absolute_url": "/public/tag/rack/", "id": 5600, "name": "rack", "resource_uri": "/api/public/tag/5600/", "snipts": "/api/public/snipt/?tag=5600"}, {"absolute_url": "/public/tag/managing/", "id": 9542, "name": "managing", "resource_uri": "/api/public/tag/9542/", "snipts": "/api/public/snipt/?tag=9542"}], "title": "Rack::Resctrictor is a middleware that helps you managing access to your applications.", "user": {"absolute_url": "/hkairi/", "email_md5": "8cacf128e1de347ca9f47b36be6f8a7d", "id": 5202, "resource_uri": "/api/public/user/5202/", "snipts": "/api/public/snipt/?user=5202", "snipts_count": 3, "username": "hkairi"}}