Skip to content

Commit

Permalink
added Remote#location_search, LocationResult collection model with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomash committed May 12, 2011
1 parent 1a514bc commit 2f07671
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/songkickr.rb
Expand Up @@ -6,6 +6,7 @@
require dir + '/songkickr/performance'
require dir + '/songkickr/artist'
require dir + '/songkickr/location'
require dir + '/songkickr/location_result'
require dir + '/songkickr/venue'
require dir + '/songkickr/event'
require dir + '/songkickr/setlist_item'
Expand Down
14 changes: 11 additions & 3 deletions lib/songkickr/location.rb
Expand Up @@ -3,9 +3,17 @@ class Location
attr_accessor :city, :lat, :lng

def initialize(location_hash)
@city = location_hash["city"]
@lat = location_hash["lat"]
@lng = location_hash["lng"]
if(location_hash["city"].is_a?(String)) # location in Event
@city = location_hash["city"]
@lat = location_hash["lat"]
@lng = location_hash["lng"]
elsif(location_hash["city"].is_a?(Hash)) # stand-alone Location
city_hash = location_hash["city"]
@city = city_hash["displayName"]
# some locations have a null lng, lat in city hash, but have non-null in metroArea hash
@lat = city_hash["lat"]
@lng = city_hash["lng"]
end
end
end
end
31 changes: 31 additions & 0 deletions lib/songkickr/location_result.rb
@@ -0,0 +1,31 @@
module Songkickr
class LocationResult
# TODO: very similar to event_result and artist_result,
# extract common stuff to module/superclass
attr_accessor :page, :total_entries, :results

def initialize(result_hash = {})
results_page = result_hash["resultsPage"]

if results_page
@page = results_page["page"]
@total_entries = results_page["totalEntries"]
@results = parse_results results_page["results"]
end
end


protected

def parse_results(results = {})
locations = []
if results.include?("location")
results["location"].each do |location|
locations << Songkickr::Location.new(location)
end
end

locations
end
end
end
12 changes: 11 additions & 1 deletion lib/songkickr/remote.rb
Expand Up @@ -50,7 +50,7 @@ def event(event_id)
# per_page
# page

def gigography (artist_id, query= {})
def gigography(artist_id, query= {})
result = self.class.get("/artists/#{artist_id}/gigography.json",:query=>query)
Songkickr::EventResult.new result
end
Expand Down Expand Up @@ -98,6 +98,16 @@ def concert_setlists(event_id)
Songkickr::ConcertSetlistResult.new result
end

# Parameters - http://www.songkick.com/developer/location-search
#
# location - 'geo:{lat,lng}' string
# query - name of location to search for

def location_search(query = {})
result = self.class.get("/search/locations.json",:query=>query)
Songkickr::LocationResult.new result
end

private

def extract_path_from_query(query = {})
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/locations_bar.json
@@ -0,0 +1 @@
{"resultsPage":{"results":{"location":[{"city":{"displayName":"Potters Bar","country":{"displayName":"UK"},"lng":-0.166667,"lat":51.6833},"metroArea":{"uri":"http:\/\/www.songkick.com\/metro_areas\/24426-uk-london?utm_source=4961&utm_medium=partner","displayName":"London","country":{"displayName":"UK"},"id":24426,"lng":-0.128,"lat":51.5078}},{"city":{"displayName":"Diamond Bar","country":{"displayName":"US"},"lng":-117.809,"lat":34.0286,"state":{"displayName":"CA"}},"metroArea":{"uri":"http:\/\/www.songkick.com\/metro_areas\/17835-us-los-angeles?utm_source=4961&utm_medium=partner","displayName":"Los Angeles","country":{"displayName":"US"},"id":17835,"lng":-118.376,"lat":34.0862,"state":{"displayName":"CA"}}},{"city":{"displayName":"Old Bar","country":{"displayName":"Australia"},"lng":null,"lat":null,"state":{"displayName":"NSW"}},"metroArea":{"uri":"http:\/\/www.songkick.com\/metro_areas\/26791-australia-newcastle?utm_source=4961&utm_medium=partner","displayName":"Newcastle","country":{"displayName":"Australia"},"id":26791,"lng":151.75,"lat":-32.9167,"state":{"displayName":"NSW"}}},{"city":{"displayName":"Bar Haven","country":{"displayName":"Canada"},"lng":null,"lat":null,"state":{"displayName":"NL"}},"metroArea":{"uri":"http:\/\/www.songkick.com\/metro_areas\/33223-canada-saint-johns?utm_source=4961&utm_medium=partner","displayName":"Saint John's","country":{"displayName":"Canada"},"id":33223,"lng":-52.6667,"lat":47.55,"state":{"displayName":"NL"}}},{"city":{"displayName":"Gold Bar","country":{"displayName":"Canada"},"lng":null,"lat":null,"state":{"displayName":"BC"}},"metroArea":{"uri":"http:\/\/www.songkick.com\/metro_areas\/27383-canada-prince-george?utm_source=4961&utm_medium=partner","displayName":"Prince George","country":{"displayName":"Canada"},"id":27383,"lng":-122.767,"lat":53.9167,"state":{"displayName":"BC"}}},{"city":{"displayName":"Bar Harbor","country":{"displayName":"US"},"lng":null,"lat":null,"state":{"displayName":"ME"}},"metroArea":{"uri":"http:\/\/www.songkick.com\/metro_areas\/78086-us-bar-harbor?utm_source=4961&utm_medium=partner","displayName":"Bar Harbor","country":{"displayName":"US"},"id":78086,"lng":null,"lat":null,"state":{"displayName":"ME"}}},{"city":{"displayName":"Marble Bar","country":{"displayName":"Australia"},"lng":null,"lat":null,"state":{"displayName":"WA"}},"metroArea":{"uri":"http:\/\/www.songkick.com\/metro_areas\/43434-australia-marble-bar?utm_source=4961&utm_medium=partner","displayName":"Marble Bar","country":{"displayName":"Australia"},"id":43434,"lng":null,"lat":null,"state":{"displayName":"WA"}}},{"city":{"displayName":"Jimble Bar Mining Centre","country":{"displayName":"Australia"},"lng":null,"lat":null,"state":{"displayName":"WA"}},"metroArea":{"uri":"http:\/\/www.songkick.com\/metro_areas\/42274-australia-jimble-bar-mining-centre?utm_source=4961&utm_medium=partner","displayName":"Jimble Bar Mining Centre","country":{"displayName":"Australia"},"id":42274,"lng":null,"lat":null,"state":{"displayName":"WA"}}}]},"totalEntries":8,"perPage":50,"page":1,"status":"ok"}}
4 changes: 4 additions & 0 deletions test/helper.rb
Expand Up @@ -23,6 +23,10 @@ def fixture_file(filename)
File.read(file_path)
end

def fixture_hash(filename)
Crack::JSON.parse(fixture_file(filename))
end

def stub_get(url, filename, status = nil)
options = {:body => fixture_file(filename)}
options.merge!({:status => status}) unless status.nil?
Expand Down
3 changes: 1 addition & 2 deletions test/songkickr/test_event.rb
Expand Up @@ -3,8 +3,7 @@
class TestArtist < Test::Unit::TestCase
context "Given an Event from fixture" do
setup do
fixture_hash = Crack::JSON.parse(fixture_file('event_7391451.json')) # extract to helper?
@event = Songkickr::Event.new(fixture_hash["resultsPage"]["results"]["event"])
@event = Songkickr::Event.new(fixture_hash('event_7391451.json')["resultsPage"]["results"]["event"])
end

should "properly load data from JSON" do
Expand Down
20 changes: 20 additions & 0 deletions test/songkickr/test_location.rb
@@ -0,0 +1,20 @@
require 'helper'

class TestLocation < Test::Unit::TestCase
context "Given Locations from fixture" do
setup do
@locations = Songkickr::LocationResult.new(fixture_hash('locations_bar.json'))
end

should "properly load data from JSON" do
assert_equal 8, @locations.results.size
assert_equal 8, @locations.total_entries

potters_bar = @locations.results.first
assert_equal "Potters Bar", potters_bar.city
assert_equal 51.6833, potters_bar.lat
assert_equal -0.166667, potters_bar.lng
end

end
end

0 comments on commit 2f07671

Please sign in to comment.