class Location

Discovers BBC location ids

Public Class Methods

new(io = STDOUT) click to toggle source
# File lib/bbc/location.rb, line 8
def initialize(io = STDOUT)
  @io = io
end

Public Instance Methods

find(place) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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