Преглед изворни кода

Initial commit

tags/last-sinatra-version
Adrian Short пре 14 година
комит
27173076c9
1 измењених фајлова са 53 додато и 0 уклоњено
  1. +53
    -0
      app.rb

+ 53
- 0
app.rb Прегледај датотеку

@@ -0,0 +1,53 @@
require 'rubygems'
require 'sinatra'
require 'json'
require 'rest_client'

get '/' do
@wards = %w{ Cheam Sutton Stonecot }
haml :home
end

get '/wards' do
@postcode = params[:postcode].strip.upcase

url = "http://www.uk-postcodes.com/postcode/" + @postcode.gsub(/ /, '') + '.json'

result = RestClient.get(url)
result_ary = JSON.parse(result)
@district_name = result_ary['administrative']['district']['title']
@ward_name = result_ary['administrative']['ward']['title']
haml :wards
end

__END__

@@layout
!!!
%html
%head
%title Find My Candidates
%body
#main
%h1 Find My Candidates
= yield
#footer
%hr/
%p Design by Adrian Short
@@home
%form{ :method => 'get', :action => '/wards' }
%label{ :for => "postcode" }
Postcode
%input{ :type => 'text', :name => 'postcode', :size => 10 }
%input{ :type => 'submit', :value => "Find" }
- for ward in @wards
%p= ward
@@wards
%h2
#{@ward_name} Ward in #{@district_name}
%p
Your postcode is #{@postcode}

Loading…
Откажи
Сачувај