{"id":16918886,"url":"https://github.com/namusyaka/pendragon","last_synced_at":"2025-03-17T07:31:25.452Z","repository":{"id":10187721,"uuid":"12276463","full_name":"namusyaka/pendragon","owner":"namusyaka","description":"Toolkit for implementing HTTP Router in Ruby","archived":false,"fork":false,"pushed_at":"2016-11-26T13:05:20.000Z","size":178,"stargazers_count":50,"open_issues_count":0,"forks_count":5,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-05-19T07:43:41.400Z","etag":null,"topics":["algorithm","httprouter","router","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/namusyaka.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-08-21T17:16:03.000Z","updated_at":"2022-06-12T02:17:30.000Z","dependencies_parsed_at":"2022-09-13T18:13:32.097Z","dependency_job_id":null,"html_url":"https://github.com/namusyaka/pendragon","commit_stats":null,"previous_names":["namusyaka/howl-router"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/namusyaka%2Fpendragon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/namusyaka%2Fpendragon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/namusyaka%2Fpendragon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/namusyaka%2Fpendragon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/namusyaka","download_url":"https://codeload.github.com/namusyaka/pendragon/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221673943,"owners_count":16861740,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","httprouter","router","ruby"],"created_at":"2024-10-13T19:42:06.279Z","updated_at":"2024-10-27T12:14:00.458Z","avatar_url":"https://github.com/namusyaka.png","language":"Ruby","funding_links":[],"categories":["Middlewares"],"sub_categories":[],"readme":"# Pendragon\n\n[![Build Status](https://travis-ci.org/namusyaka/pendragon.svg?branch=master)](https://travis-ci.org/namusyaka/pendragon) [![Gem Version](https://badge.fury.io/rb/pendragon.svg)](http://badge.fury.io/rb/pendragon)\n\nPendragon provides an HTTP router and its toolkit for use in Rack. As a Rack application, it makes it easy to define complicated routing. \nAlgorithms of the router are used in [Padrino](https://github.com/padrino/padrino-framework) and [Grape](https://github.com/ruby-grape/grape), it's fast, flexible and robust.\n\n*If you want to use in Ruby-1.9, you can do it by using [mustermann19](https://github.com/namusyaka/mustermann19).*\n\n\n```ruby\nPendragon.new do\n  get('/') { [200, {}, ['hello world']] }\n  namespace :users do\n    get('/',    to: -\u003e { [200, {}, ['User page index']] })\n    get('/:id', to: -\u003e (id) { [200, {}, [id]] })\n    get('/:id/comments') { |id| [200, {}, [User.find_by(id: id).comments.to_json]] }\n  end\nend\n```\n\n## Router Patterns\n\n|Type  |Description  |Note  |\n|---|---|---|\n|[liner](https://github.com/namusyaka/pendragon/blob/master/lib/pendragon/liner.rb)  |Linear search, Optimized Mustermann patterns | |\n|[realism](https://github.com/namusyaka/pendragon/blob/master/lib/pendragon/realism.rb)  |First route is detected by union regexps (Actually, O(1) in ruby level), routes since the first time will be retected by linear search | this algorithm is using in Grape |\n|[radix](https://github.com/namusyaka/pendragon-radix)  |Radix Tree, not using Mustermann and regexp| requires C++11 |\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'pendragon'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install pendragon\n\n\n## Usage\n\n### Selects router pattern\n\nYou can select router pattern as following code.\n\n```ruby\n# Gets Linear router class by passing type in `Pendragon.[]`\nPendragon[:linear] #=\u003e Pendragon::Linear\n\n# Specify :type to construction of Pendragon.\nPendragon.new(type: :linear) { ... }\n```\n\n### Registers a route\n\nIt has some methods to register a route. For example, `#get`, `#post` and `#delete` are so.\nThis section introduces all those methods.\n\n#### `route(method, path, **options, \u0026block)`\n\n\nThe method is the basis of the registration method of all.\nIn comparison with other registration methods, one argument is increased.\n\n```ruby\nPendragon.new do\n  route('GET', ?/){ [200, {}, ['hello']] }\nend\n```\n\n#### `get(path, **options, \u0026block)`, `post`, `delete`, `put` and `head`\n\nBasically the usage is the same with `#route`.\nYou may as well use those methods instead of `#route` because those methods are easy to understand.\n\n```ruby\nPendragon.new do\n  get   (?/) { [200, {}, ['hello']] }\n  post  (?/) { [200, {}, ['hello']] }\n  delete(?/) { [200, {}, ['hello']] }\n  put   (?/) { [200, {}, ['hello']] }\n  head  (?/) { [200, {}, ['hello']] }\nend\n```\n\n### Mounts Rack Application\n\nYou can easily mount your rack application onto Pendragon.\n\n*Please note that pendragon distinguishes between processing Proc and Rack Application.*\n\n```ruby\nclass RackApp\n  def call(env)\n    puts env #=\u003e rack default env\n    [200, {}, ['hello']]\n  end\nend\n\nPendragon.new do\n  get '/ids/:id', to: -\u003e (id) { p id } # Block parameters are available\n  get '/rack/:id', to: RackApp.new # RackApp#call will be called, `id` is not passed and `env` is passed instead.\nend\n```\n\n### Halt\n\nYou can halt to processing by calling `throw :halt` inside your route.\n\n```ruby\nPendragon.new do\n  get ?/ do\n    throw :halt, [404, {}, ['not found']]\n    [200, {}, ['failed to halt']]\n  end\nend\n```\n\n### Cascading\n\nA route can punt to the next matching route by using `X-Cascade` header.\n\n```ruby\npendragon = Pendragon.new do\n  foo = 1\n  get ?/ do\n    [200, { 'X-Cascade' =\u003e 'pass' }, ['']]\n  end\n\n  get ?/ do\n    [200, {}, ['goal!']]\n  end\nend\n\nenv = Rack::MockRequest.env_for(?/)\npendragon.call(env) #=\u003e [200, {}, ['goal!']]\n```\n\n## Contributing\n\n1. fork the project.\n2. create your feature branch. (`git checkout -b my-feature`)\n3. commit your changes. (`git commit -am 'commit message'`)\n4. push to the branch. (`git push origin my-feature`)\n5. send pull request.\n\n## License\n\nthe MIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnamusyaka%2Fpendragon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnamusyaka%2Fpendragon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnamusyaka%2Fpendragon/lists"}