Class: Location
Overview
Discovers BBC location ids
Instance Method Summary collapse
- #find(place) ⇒ Object
-
#initialize(io = STDOUT) ⇒ Location
constructor
A new instance of Location.
- #load(place) ⇒ Object
- #url(term) ⇒ Object
Methods included from ShellColors
blue, cyan, green, light_green, paint, red, white, yellow
Constructor Details
#initialize(io = STDOUT) ⇒ Location
Returns a new instance of Location.
8 9 10 |
# File 'lib/bbc/location.rb', line 8 def initialize(io = STDOUT) @io = io end |
Instance Method Details
#find(place) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/bbc/location.rb', line 12 def find(place) fail 'Please use a longer search term' if place.length < 3 @io.print "Searching for #{place}..." load(place) end |
#load(place) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/bbc/location.rb', line 25 def load(place) begin raw = open(url(place), 'UserAgent' => AUNTIE::USER_AGENT).read data = JSON.parse(raw) rescue raise 'Unable to download location data' end @io.print "\r" if data.empty? @io.puts "No locations found matching '#{place}' " else @io.puts sprintf '%-42s %-10s', 'Place', 'ID' data.each do |e| name = e['fullName'].split(',') place = light_green(name[0] + ',') + green(name[1]) id = cyan e['id'] @io.puts sprintf '%-60s %-10s', place, id end end end |
#url(term) ⇒ Object
20 21 22 23 |
# File 'lib/bbc/location.rb', line 20 def url(term) escaped = URI.encode(term) "http://www.bbc.co.uk/locator/default/en-GB/autocomplete.json?search=#{escaped}&filter=domestic&ptrt=/" end |