diff --git a/lib/songkickr.rb b/lib/songkickr.rb index 43881a7..bc986ae 100644 --- a/lib/songkickr.rb +++ b/lib/songkickr.rb @@ -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' diff --git a/lib/songkickr/location.rb b/lib/songkickr/location.rb index 2f114dc..9103c33 100644 --- a/lib/songkickr/location.rb +++ b/lib/songkickr/location.rb @@ -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 diff --git a/lib/songkickr/location_result.rb b/lib/songkickr/location_result.rb new file mode 100644 index 0000000..2dc1fe4 --- /dev/null +++ b/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 diff --git a/lib/songkickr/remote.rb b/lib/songkickr/remote.rb index 249e7cf..6a5a03f 100644 --- a/lib/songkickr/remote.rb +++ b/lib/songkickr/remote.rb @@ -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 @@ -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 = {}) diff --git a/test/fixtures/locations_bar.json b/test/fixtures/locations_bar.json new file mode 100644 index 0000000..c17f8ba --- /dev/null +++ b/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"}} \ No newline at end of file diff --git a/test/helper.rb b/test/helper.rb index 108975b..b6a10a3 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -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? diff --git a/test/songkickr/test_event.rb b/test/songkickr/test_event.rb index 17d0deb..0c010c1 100644 --- a/test/songkickr/test_event.rb +++ b/test/songkickr/test_event.rb @@ -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 diff --git a/test/songkickr/test_location.rb b/test/songkickr/test_location.rb new file mode 100644 index 0000000..dfd2248 --- /dev/null +++ b/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