{"id":16732376,"url":"https://github.com/danielberkompas/twiliomenu","last_synced_at":"2025-03-15T19:24:51.678Z","repository":{"id":3443997,"uuid":"4496889","full_name":"danielberkompas/twiliomenu","owner":"danielberkompas","description":"A gem for moving twilio code into the model","archived":false,"fork":false,"pushed_at":"2013-06-24T20:44:03.000Z","size":180,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-22T08:38:16.878Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danielberkompas.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-05-30T16:04:23.000Z","updated_at":"2022-04-10T06:34:33.000Z","dependencies_parsed_at":"2022-09-07T07:31:13.452Z","dependency_job_id":null,"html_url":"https://github.com/danielberkompas/twiliomenu","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielberkompas%2Ftwiliomenu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielberkompas%2Ftwiliomenu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielberkompas%2Ftwiliomenu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielberkompas%2Ftwiliomenu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielberkompas","download_url":"https://codeload.github.com/danielberkompas/twiliomenu/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243779158,"owners_count":20346668,"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":[],"created_at":"2024-10-12T23:44:08.397Z","updated_at":"2025-03-15T19:24:51.651Z","avatar_url":"https://github.com/danielberkompas.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Twiliomenu\n\nIf you've spent any time with Twilio, you know that you can end up with a lot of controller actions.  A lot of logic ends up in the controller, which isn't a recipe for DRY or maintainable code.\n\nThis gem allows you to move most of your [Twilio](http://twilio.com) code into your model rather than a mess of controller actions.\n\n### For Example\nSome of your twilio controllers might look like this:\n\n\tclass CallController \u003c ActionController::Base\n\t  def handler\n\t    # If the user has pressed digits on their keypad,\n\t    # process them.\n\t    if params[:Digits]\n\t      if params[:Digits] == 1\n\t        response = Twilio::Builder do |r|\n\t          r.redirect second_menu_path\n\t        end\n\t      elseif params[:Digits] == 2\n\t        response = Twilio::Builder do |r|\n\t          r.redirect third_menu_path\n\t        end\n\t      end\n\t    \n\t    # Otherwise, display the first menu\n\t    else  \n\t      response = Twilio::Builder do |r|\n\t        r.play \"/path-to-sound.mp3\"\n\t        r.say \"This is the handler menu.\"\n\t        r.gather do\n\t          r.say \"Press 1 to go to the second menu\"\n\t          r.say \"Press 2 to go to the third menu\"\n\t        end\n\t      end\n\t    end\n\t    \n\t    render text: response\n\t  end\n\t  \n\t  def second_menu\n\t    # Second menu code would go here ...\n\t  end\n\t  \n\t  def third_menu\n\t    # More code here ...\n\t  end\n\tend\n\nIck.  This design results in a lot logic residing in your controllers, and as the controller grows, it will difficult to follow.\n\nHere's how we'd refactor this code with twiliomenu.\n\n### Install the gem\n\n\tgem install twiliomenu\n\nOr inside your Gemfile:\n\t\n\tgem \"twiliomenu\"\n\nThen run `bundle install` to install it.\n\n### Call Model\nFirst, generate your model.\n\t\n\trails g model Call current_menu\n\n**Note** that you need to add a string field named \"current_menu\" to any model you wish to use this gem with.  Now, inside our **call_model.rb** we can write:\n\n\tclass Call \u003c ActiveRecord::Base\n\t  acts_as_menu # Pulls in the gem functionality\n\t  \n\t  menu :opening_menu do\n\t  \tplay \"/path-to-sound.mp3\"\n\t  \tsay \"This is the handler menu.\"\n\t  \tgather do\n\t  \t  prompt 1, \"Press 1 to go to the second menu\", menu: :second_menu\n\t  \t  prompt 2, \"Press 2 to go to the third menu\", menu: :third_menu\n\t  \tend\n\t  end\n\t  \n\t  menu :second_menu do\n\t    # Code for the second menu here ...\n\t  end\n\t  \n\t  menu :third_menu do\n\t    # Code for the third menu here ...\n\t  end\n\tend\n\t\nAnd then we can slim down our controller!\n\n\tclass CallController \u003c ActiveController::Base\n\t  def handler\n\t    @call = (params[:id]) ? Call.find(params[:id]) : Call.create\n\t    @call.process_twilio_response(params)\n\t    render text: @call.twiml\n\t  end\n\tend","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielberkompas%2Ftwiliomenu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielberkompas%2Ftwiliomenu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielberkompas%2Ftwiliomenu/lists"}