{"id":18355368,"url":"https://github.com/midwire/my_zipcode_gem","last_synced_at":"2025-04-06T12:32:00.472Z","repository":{"id":1462362,"uuid":"1698762","full_name":"midwire/my_zipcode_gem","owner":"midwire","description":"A Ruby gem to handle all things zipcode.","archived":false,"fork":false,"pushed_at":"2020-04-10T21:15:19.000Z","size":63,"stargazers_count":38,"open_issues_count":5,"forks_count":15,"subscribers_count":5,"default_branch":"develop","last_synced_at":"2024-04-13T22:28:10.298Z","etag":null,"topics":["city","county","jquery-widgets","ruby","states","zipcode","zipcode-gem"],"latest_commit_sha":null,"homepage":"","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/midwire.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2011-05-03T23:15:07.000Z","updated_at":"2024-03-25T14:37:08.000Z","dependencies_parsed_at":"2022-07-07T12:44:30.881Z","dependency_job_id":null,"html_url":"https://github.com/midwire/my_zipcode_gem","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwire%2Fmy_zipcode_gem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwire%2Fmy_zipcode_gem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwire%2Fmy_zipcode_gem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwire%2Fmy_zipcode_gem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/midwire","download_url":"https://codeload.github.com/midwire/my_zipcode_gem/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247484342,"owners_count":20946384,"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":["city","county","jquery-widgets","ruby","states","zipcode","zipcode-gem"],"created_at":"2024-11-05T22:06:41.597Z","updated_at":"2025-04-06T12:32:00.092Z","avatar_url":"https://github.com/midwire.png","language":"Ruby","readme":"# My Zipcode Gem\n\n[![MIT-LICENSE](https://img.shields.io/github/license/midwire/my_zipcode_gem.svg)](https://github.com/midwire/my_zipcode_gem/blob/master/MIT-LICENSE)\n\nSimple gem to handle zipcode lookups and related functionality.  `rake zipcodes:update` will automatically download and update your local zipcode database.  Generates three models for extended zipcode functionality.\n\n## Versions\n\n* 0.1.x is for Rails versions less than 4.x\n* 0.2.x is for Rails 4 and above\n\n## Installation\n\nAdd the following line to your Gemfile:\n\n```ruby\ngem 'my_zipcode_gem'\n```\n\nRun:\n\n```bash\nrake bundle install\n```\n\nGenerate the models and populate the data:\n\n```bash\nrails g my_zipcode_gem:models\n# here you may need to run \"bundle install\" again for dependencies\nrake db:migrate\nrake zipcodes:update # this will take some time, be patient\n```\n\nYou should now have three new tables and three new models, Zipcode, State, County.\n\n## Usage\n\n```ruby\nzipcode = Zipcode.find_by_code '66206'\nzipcode.state.abbr    # =\u003e 'KS'\nzipcode.city          # =\u003e 'Shawnee Mission'\nzipcode.county.name   # =\u003e 'Johnson'\nzipcode.lat.to_s      # =\u003e '38.959356', it is actually a BigDecimal object converted to_s for documentation.\nzipcode.lon.to_s      # =\u003e '-94.716155', ditto\nzipcode.geocoded?  # =\u003e true, most if not all should be pre-geocoded.\n```\n\nYou can also look for a zipcode from a city and state:\n\n```ruby\nZipcode.find_by_city_state \"Shawnee Mission\", \"KS\"\n```\n\nYou can use State and County objects as follows:\n\n```ruby\nstate = State.find_by_abbr \"MO\"\nstate.cities.count    # =\u003e 963\nstate.cities          # gives you a sorted array of all cities for the state\nstate.zipcodes.count  # =\u003e 1195\n...\ncounty = state.counties.first\ncounty.cities.count   # =\u003e 5\ncounty.cities         # gives you a sorted array of all cities for the county\ncounty.zipcodes.count # =\u003e 5\n```\n\n### Automatic JQuery/AJAX lookup\n\nYou can have a user enter a zipcode and automatically lookup their city, state and county.\n\nPut something like this in your view:\n\n```ruby\nf.text_field :zip, size: 5, maxlength: 5, class: 'zipcode_interactive'\nf.text_field :city, size: 20, maxlength: 60, readonly: true\nf.text_field(:state, size: 2, maxlength: 2, readonly: true)\nf.text_field(:county, size: 20, maxlength: 60, readonly: true)\n```\n\nThen add this to your application.js, but remember to replace `mycontrollername` with your own controller.\n\n```javascript\n$(document).ready(function() {\n  // Interactive Zipcodes\n  $('input.zipcode_interactive').blur(function(data) {\n    var elem_id = $(this).attr(\"id\");\n    var base_id = elem_id.substring(0, elem_id.lastIndexOf(\"_\"));\n    $.get(\"/mycontrollername/get_zip_data/\" + this.value, {},\n    function(data) {\n      var zipcode = $.parseJSON(data);\n      var city = $('#' + base_id + '_city');\n      var state = $('#' + base_id + '_state');\n      var county = $('#' + base_id + '_county');\n      if (zipcode.err) {\n        alert(zipcode.err);\n      } else {\n        city.val(zipcode.city);\n        state.val(zipcode.state)\n        county.val(zipcode.county)\n      }\n    })\n  });\n});\n```\n\nYou will also need a controller method similar to this, which will return the data to your form:\n\n```ruby\ndef get_zip_data\n  @zipcode = Zipcode.find_by_code(params[:code], include: [:county, :state])\n  if @zipcode\n    @counties = County.find(:all, conditions: [ \"state_id = ?\", @zipcode.county.state_id ])\n    data = {\n      'state' =\u003e @zipcode.state.abbr,\n      'county' =\u003e @zipcode.county.name,\n      'city' =\u003e @zipcode.city.titleize\n    }\n    render text: data.to_json\n  else\n    if params[:code].blank?\n      return true\n    else\n      if params[:code].is_zipcode?\n        data = {\n          'err' =\u003e \"Could not find Zipcode [#{params[:code]}].  If this is a valid zipcode please notify support \u003csupport@mydomain.com\u003e, so we can update our database.\"\n        }\n      else\n        data = {\n          'err' =\u003e \"[#{params[:code]}] is not a valid Zipcode.\"\n        }\n      end\n      render text: data.to_json\n    end\n  end\nend\n```\n\nAnd define a route for the AJAX function in routes.rb:\n\n```ruby\nget 'mycontrollername/get_zip_data/:code', controller: 'mycontrollername', action: 'get_zip_data'\n```\n\nThat's about it.\n\nPlease report any errors or [issues](https://github.com/midwire/my_zipcode_gem/issues).\n\n## LOG\n\n### 05/03/2011:\n\nInitial Release\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmidwire%2Fmy_zipcode_gem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmidwire%2Fmy_zipcode_gem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmidwire%2Fmy_zipcode_gem/lists"}