Permalink
Browse files

more "refactoring" - getting rid of last peachy leftovers

  • Loading branch information...
1 parent ba9968b commit 29edccf0ad54026cdd9e1bb17a209b89f14b33f9 filip committed Jun 16, 2012
Showing with 408 additions and 384 deletions.
  1. +6 −0 Gemfile
  2. +27 −49 lib/sevendigital/digestion_tract/api_response_digestor.rb
  3. +1 −1 lib/sevendigital/digestion_tract/chart_item_digestor.rb
  4. +14 −41 lib/sevendigital/digestion_tract/digestor.rb
  5. +9 −11 lib/sevendigital/management/artist_manager.rb
  6. +4 −4 lib/sevendigital/management/basket_manager.rb
  7. +2 −2 lib/sevendigital/management/oauth_manager.rb
  8. +9 −11 lib/sevendigital/management/release_manager.rb
  9. +1 −1 lib/sevendigital/management/tag_manager.rb
  10. +3 −3 lib/sevendigital/management/track_manager.rb
  11. +2 −2 lib/sevendigital/management/user_card_manager.rb
  12. +4 −4 lib/sevendigital/management/user_manager.rb
  13. +12 −4 lib/sevendigital/model/api_response.rb
  14. +1 −1 lib/sevendigital/model/release.rb
  15. +1 −1 lib/sevendigital/pager.rb
  16. +0 −28 lib/sevendigital/proxy_police.rb
  17. +1 −2 spec/api_operator_cached_spec.rb
  18. +3 −3 spec/api_operator_spec.rb
  19. +26 −22 spec/digestion_tract/api_response_digestor_spec.rb
  20. +3 −3 spec/digestion_tract/artist_digestor_spec.rb
  21. +2 −2 spec/digestion_tract/basket_digestor_spec.rb
  22. +2 −2 spec/digestion_tract/basket_item_digestor_spec.rb
  23. +4 −4 spec/digestion_tract/chart_digestor_spec.rb
  24. +1 −1 spec/digestion_tract/digestor_spec.rb
  25. +2 −2 spec/digestion_tract/download_url_digestor_spec.rb
  26. +2 −2 spec/digestion_tract/format_digestor_spec.rb
  27. +2 −2 spec/digestion_tract/label_digestor_spec.rb
  28. +2 −2 spec/digestion_tract/locker_digestor_spec.rb
  29. +2 −2 spec/digestion_tract/locker_release_digestor_spec.rb
  30. +2 −2 spec/digestion_tract/locker_track_digestor_spec.rb
  31. +2 −2 spec/digestion_tract/oauth_access_token_digestor_spec.rb
  32. +2 −2 spec/digestion_tract/oauth_request_token_digestor_spec.rb
  33. +3 −3 spec/digestion_tract/pager_digestor_spec.rb
  34. +4 −4 spec/digestion_tract/price_digestor_spec.rb
  35. +16 −2 spec/digestion_tract/release_digestor_spec.rb
  36. +3 −3 spec/digestion_tract/tag_digestor_spec.rb
  37. +3 −3 spec/digestion_tract/track_digestor_spec.rb
  38. +3 −3 spec/digestion_tract/user_card_digestor_spec.rb
  39. +3 −3 spec/digestion_tract/user_digestor_spec.rb
  40. +86 −0 spec/integration/artist_spec.rb
  41. +34 −0 spec/integration/release_spec.rb
  42. +1 −1 spec/{ → integration}/sevendigital_spec.yml
  43. +32 −11 spec/management/artist_manager_spec.rb
  44. +4 −4 spec/management/basket_manager_spec.rb
  45. +4 −4 spec/management/oauth_manager_spec.rb
  46. +13 −13 spec/management/release_manager_spec.rb
  47. +1 −1 spec/management/tag_manager_spec.rb
  48. +4 −4 spec/management/track_manager_spec.rb
  49. +2 −2 spec/management/user_card_manager_spec.rb
  50. +4 −4 spec/management/user_manager_spec.rb
  51. +3 −3 spec/model/api_response_spec.rb
  52. +0 −7 spec/peachy_spec_helper_patch.rb
  53. +0 −70 spec/proxy_police_spec.rb
  54. +0 −12 spec/sevendigital_spec.rb
  55. +8 −9 spec/spec_helper.rb
  56. +23 −0 spec/test-xml/objects/nested_release_list.xml
View
@@ -0,0 +1,6 @@
+source 'http://rubygems.org'
+
+gem 'rspec'
+gem 'oauth'
+gem 'nokogiri'
+gem 'will_paginate'
@@ -1,71 +1,49 @@
+require 'nokogiri'
+
module Sevendigital
#@private
class ApiResponseDigestor < Digestor # :nodoc:
def default_element_name; :response end
- def from_xml_doc(xml_node)
- return from_invalid_xml unless xml_node
-
- status = get_optional_attribute(xml_node, "status")
-
- return from_ok_response(xml_node) if status == 'ok'
-
- return from_error_response(xml_node) if status == 'error'
-
- from_invalid_xml
- end
-
- def from_invalid_xml
- response = ApiResponse.new
-
- response.error_code = 10000
- response.error_message = 'Invalid 7digital API response'
-
- response
- end
-
- def from_error_response(xml_node)
-
- error_node = get_required_node(xml_node, "error")
-
+ def from_http_response(http_response)
response = ApiResponse.new
+ response.headers = http_response.header
+ response.content = http_response.body.to_s
- response.error_code = get_required_attribute(error_node, "code").to_i
- response.error_message = get_required_value(error_node, "errorMessage")
+ parse_xml_doc(response.content, response)
+ if response.error_code >= 10000 && !http_response.is_a?(Net::HTTPSuccess)
+ response.error_code = Integer(http_response.code)
+ response.error_message= http_response.body.to_s
+ end
response
end
- def from_ok_response(response_content)
- response = ApiResponse.new
-
- response.error_code = 0
- response.content = response_content
+ def parse_xml_doc(xml, response)
- response
- end
+ xml_doc = Nokogiri::XML(xml)
- def from_http_response(http_response)
- if http_response.is_a?(Net::HTTPSuccess) then
- response = from_xml_nokogiri(http_response.body.to_s)
+ response_node = xml_doc.at_xpath("./response")
+ response_status = nil
+ response_status = get_optional_attribute(response_node, "status") if response_node
+ puts xml.inspect
+ if response_status == 'ok' then
+ response.error_code = 0
else
- response = from_invalid_http_response(http_response)
+ if response_status == 'error'
+ error_node = get_required_node(response_node, "error")
+ response.error_code = get_required_attribute(error_node, "code").to_i
+ response.error_message = get_required_value(error_node, "errorMessage")
+ else
+ response.error_code = 10001
+ response.error_message = 'Invalid 7digital API response'
+ end
end
- response.headers = http_response.header
- response
- end
-
- def from_invalid_http_response(http_response)
- response = ApiResponse.new
- response.error_code = Integer(http_response.code)
- response.error_message= http_response.body.to_s
-
- response
end
-
+
end
end
@@ -8,7 +8,7 @@ def default_list_element_name; :chart end
def from_proxy(chart_item_proxy)
- from_xml_nokogiri(chart_item_proxy.to_s)
+ from_xml_string(chart_item_proxy.to_s)
end
@@ -10,28 +10,11 @@ def initialize(api_client)
@api_client = api_client
end
- def from_xml(xml_or_proxy, element_name = default_element_name)
- from_proxy(ProxyPolice.ensure_is_proxy(xml_or_proxy, element_name))
- end
-
- def from_xml_nokogiri(xml, element_name = default_element_name)
+ def from_xml_string(xml, element_name = default_element_name)
xml_doc = Nokogiri::XML(xml)
- puts xml_doc.inspect
- puts "XML #{element_name}"
- puts xml_doc.at_xpath("./#{element_name}").inspect
from_xml_doc(xml_doc.at_xpath("./#{element_name}"))
end
- def list_from_proxy(object_list_proxy)
- make_sure_not_eating_nil(object_list_proxy)
- list = []
- return list if object_list_proxy.kind_of?(Peachy::SimpleContent)
- if object_list_proxy.send(default_element_name) then
- object_list_proxy.send(default_element_name).each { |object_proxy| list << from_proxy(object_proxy) }
- end
- paginate_results(object_list_proxy, list)
- end
-
def list_from_xml_string(xml, list_element_name = default_list_element_name)
xml_doc = Nokogiri::XML(xml)
list_from_xml_doc(xml_doc.at_xpath("./#{list_element_name}"))
@@ -41,43 +24,33 @@ def list_from_xml_doc(list_node)
make_sure_eating_nokogiri_node(list_node)
list = []
list_node.xpath("./#{default_element_name}").each { |node| list << from_xml_doc(node)}
- paginate_results_nokogiri(list_node, list)
+ paginate_results(list_node, list)
end
#nested parsing for api methods that return standard object inside containers with no additional (useful) information
#e.g. tagged_item.artist, recommendation.release, search_result.track, etc
- def nested_list_from_xml(xml_or_proxy, container_element_name, list_element_name = default_list_element_name)
- nested_list_from_proxy(ProxyPolice.ensure_is_proxy(xml_or_proxy, list_element_name), container_element_name)
+ def nested_list_from_xml_string(xml, container_element_name, list_element_name = default_list_element_name)
+ xml_doc = Nokogiri::XML(xml)
+ nested_list_from_xml_doc(xml_doc.at_xpath("./#{container_element_name}"), list_element_name)
end
- def nested_list_from_proxy(object_list_proxy, container_element_name)
- make_sure_not_eating_nil(object_list_proxy)
+ def nested_list_from_xml_doc(list_node, list_element_name = default_list_element_name, element_name = default_element_name)
+ puts list_element_name
+ puts element_name
+ puts list_node.inspect
+ make_sure_eating_nokogiri_node(list_node)
list = []
- if object_list_proxy.send(container_element_name) then
- object_list_proxy.send(container_element_name).each do
- |object_proxy| list << from_proxy(object_proxy.send(default_element_name))
- end
- end
- return paginate_results(object_list_proxy, list)
+ list_node.xpath("./#{list_element_name}/#{element_name}").each { |node| list << from_xml_doc(node)}
+ paginate_results(list_node, list)
end
- def paginate_results(xml_results, list)
- pager = @api_client.pager_digestor.from_xml(xml_results)
+ def paginate_results(results_xml_node, list)
+ pager = @api_client.pager_digestor.from_xml_doc(results_xml_node)
return list if !pager
pager.paginate_list(list)
end
- def paginate_results_nokogiri(xml_results, list)
- pager = @api_client.pager_digestor.from_xml_doc(xml_results)
- return list if !pager
- pager.paginate_list(list)
- end
-
- def make_sure_not_eating_nil(xml)
- raise DigestiveProblem, "There's nothing i can digest" unless xml
- end
-
def make_sure_eating_nokogiri_node(xml)
raise DigestiveProblem, "There's nothing i can digest" unless xml
raise DigestiveProblem, "I'm not eating this! It's not a Nokogiri XML node.'" unless xml.kind_of?(Nokogiri::XML::Node)
@@ -1,5 +1,3 @@
-require "peachy"
-
module Sevendigital
# provides access to Artist related API methods (artist/*)
@@ -12,7 +10,7 @@ class ArtistManager < Manager
# @return [Artist]
def get_details(artist_id, options={})
api_response = @api_client.make_api_request(:GET, "artist/details", {:artistId => artist_id}, options)
- @api_client.artist_digestor.from_xml(api_response.content.artist)
+ @api_client.artist_digestor.from_xml_doc(api_response.item_xml("artist"))
end
# calls *artist/releases* API method and returns Release array
@@ -22,7 +20,7 @@ def get_details(artist_id, options={})
# @return [[Artist]]
def get_releases(artist_id, options={})
api_response = @api_client.make_api_request(:GET, "artist/releases", {:artistId => artist_id}, options)
- @api_client.release_digestor.list_from_xml(api_response.content.releases)
+ @api_client.release_digestor.list_from_xml_doc(api_response.item_xml("releases"))
end
# calls *artist/toptracks* API method and returns Track array
@@ -32,7 +30,7 @@ def get_releases(artist_id, options={})
# @return [Array<Track>]
def get_top_tracks(artist_id, options={})
api_response = @api_client.make_api_request(:GET, "artist/topTracks", {:artistId => artist_id}, options)
- @api_client.track_digestor.list_from_xml(api_response.content.tracks)
+ @api_client.track_digestor.list_from_xml_doc(api_response.item_xml("tracks"))
end
# calls *artist/similar* API method and returns Artist array
@@ -42,7 +40,7 @@ def get_top_tracks(artist_id, options={})
# @return [Array<Artist>]
def get_similar(artist_id, options={})
api_response = @api_client.make_api_request(:GET, "artist/similar", {:artistId => artist_id}, options)
- @api_client.artist_digestor.list_from_xml(api_response.content.artists)
+ @api_client.artist_digestor.list_from_xml_doc(api_response.item_xml("artists"))
end
# calls *artist/byTag/top* API method and returns Artist array
@@ -52,7 +50,7 @@ def get_similar(artist_id, options={})
# @return [Array<Artist>]
def get_top_by_tag(tags, options={})
api_response = @api_client.make_api_request(:GET, "artist/byTag/top", {:tags => tags}, options)
- @api_client.artist_digestor.nested_list_from_xml(api_response.content.tagged_results, :tagged_item, :tagged_results)
+ @api_client.artist_digestor.nested_list_from_xml_doc(api_response.item_xml("taggedResults"), :taggedItem, :artist)
end
# calls *artist/search* API method and returns Artist array
@@ -62,7 +60,7 @@ def get_top_by_tag(tags, options={})
# @return [Array<Artist>]
def search(query, options={})
api_response = @api_client.make_api_request(:GET, "artist/search", {:q => query}, options)
- @api_client.artist_digestor.nested_list_from_xml(api_response.content.search_results, :search_result, :search_results)
+ @api_client.artist_digestor.nested_list_from_xml_doc(api_response.item_xml("searchResults"), :searchResult, :artist)
end
# calls *artist/browse* API method and returns Artist array
@@ -72,15 +70,15 @@ def search(query, options={})
# @return [Array<Artist>]
def browse(letter, options={})
api_response = @api_client.make_api_request(:GET, "artist/browse", {:letter => letter}, options)
- @api_client.artist_digestor.list_from_xml(api_response.content.artists)
+ @api_client.artist_digestor.list_from_xml_doc(api_response.item_xml("artists"))
end
# calls *artist/chart* API method and returns Artist array
#
# @param [Hash] options optional hash of additional API parameters, e.g. page_size => 50, etc
def get_chart(options={})
api_response = @api_client.make_api_request(:GET, "artist/chart", {}, options)
- @api_client.chart_item_digestor.list_from_xml(api_response.content.chart)
+ @api_client.chart_item_digestor.list_from_xml_doc(api_response.item_xml("chart"))
end
# calls *artist/tags* API method and returns Tag array
@@ -90,7 +88,7 @@ def get_chart(options={})
# @return [Array<Tag>]
def get_tags(artist_id, options={})
api_response = @api_client.make_api_request(:GET, "artist/tags", {:artistId => artist_id}, options)
- @api_client.tag_digestor.list_from_xml(api_response.content.tags)
+ @api_client.tag_digestor.list_from_xml_doc(api_response.item_xml("tags"))
end
@@ -4,23 +4,23 @@ class BasketManager < Manager
def get(basket_id, options={})
api_response = @api_client.make_api_request(:GET, "basket", {:basketId => basket_id}, options)
- @api_client.basket_digestor.from_xml(api_response.content.basket)
+ @api_client.basket_digestor.from_xml_doc(api_response.item_xml("basket"))
end
def create(options={})
api_response = @api_client.make_api_request(:GET, "basket/create", {}, options)
- @api_client.basket_digestor.from_xml(api_response.content.basket)
+ @api_client.basket_digestor.from_xml_doc(api_response.item_xml("basket"))
end
def add_item(basket_id, release_id, track_id=nil, options={})
api_response = @api_client.make_api_request(:GET, "basket/addItem", {:basketId => basket_id, :releaseId => release_id, :trackId => track_id}, options)
- @api_client.basket_digestor.from_xml(api_response.content.basket)
+ @api_client.basket_digestor.from_xml_doc(api_response.item_xml("basket"))
end
def remove_item(basket_id, item_id, options={})
api_response = @api_client.make_api_request(:GET, "basket/removeItem", {:basketId => basket_id, :itemId => item_id}, options)
- @api_client.basket_digestor.from_xml(api_response.content.basket)
+ @api_client.basket_digestor.from_xml_doc(api_response.item_xml("basket"))
end
@@ -4,12 +4,12 @@ class OAuthManager < Manager
def get_request_token
api_response = @api_client.make_signed_api_request(:GET, "oauth/requestToken", {})
- @api_client.oauth_request_token_digestor.from_xml(api_response.content.oauth_request_token, :oauth_request_token)
+ @api_client.oauth_request_token_digestor.from_xml_doc(api_response.item_xml("oauth_request_token"), :oauth_request_token)
end
def get_access_token(request_token)
api_response = @api_client.make_signed_api_request(:GET, "oauth/accessToken", {}, {}, request_token)
- @api_client.oauth_access_token_digestor.from_xml(api_response.content.oauth_access_token, :oauth_access_token)
+ @api_client.oauth_access_token_digestor.from_xml_doc(api_response.item_xml("oauth_access_token"), :oauth_access_token)
end
def authorise_request_token(username, password, request_token)
@@ -1,23 +1,21 @@
-require "peachy"
-
module Sevendigital
class ReleaseManager < Manager
def get_details(release_id, options = {})
api_response = @api_client.make_api_request(:GET, "release/details", {:releaseId => release_id}, options)
- @api_client.release_digestor.from_xml(api_response.content.release)
+ @api_client.release_digestor.from_xml_doc(api_response.item_xml("release"))
end
def get_tracks(release_id, options = {})
options[:page_size] ||= 100
api_response = @api_client.make_api_request(:GET, "release/tracks", {:releaseId => release_id}, options)
- @api_client.track_digestor.list_from_xml(api_response.content.tracks)
+ @api_client.track_digestor.list_from_xml_doc(api_response.item_xml("tracks"))
end
def get_chart(options={})
api_response = @api_client.make_api_request(:GET, "release/chart", {}, options)
- @api_client.chart_item_digestor.list_from_xml(api_response.content.chart)
+ @api_client.chart_item_digestor.list_from_xml_doc(api_response.item_xml("chart"))
end
def get_by_date(from_date = nil, to_date = nil, options = {})
@@ -26,30 +24,30 @@ def get_by_date(from_date = nil, to_date = nil, options = {})
parameters[:toDate] = to_date.strftime("%Y%m%d") if to_date
api_response = @api_client.make_api_request(:GET, "release/byDate", parameters, options)
- @api_client.release_digestor.list_from_xml(api_response.content.releases)
+ @api_client.release_digestor.list_from_xml_doc(api_response.item_xml("releases"))
end
def get_recommendations(release_id, options = {})
api_response = @api_client.make_api_request(:GET, "release/recommend", {:releaseId => release_id}, options)
- @api_client.release_digestor.nested_list_from_xml(api_response.content.recommendations, :recommended_item, :recommendations)
+ @api_client.release_digestor.nested_list_from_xml_doc(api_response.item_xml("recommendations"), :recommendedItem, :release)
end
def get_top_by_tag(tags, options = {})
api_response = @api_client.make_api_request(:GET, "release/byTag/top", {:tags => tags}, options)
- @api_client.release_digestor.nested_list_from_xml( \
- api_response.content.tagged_results, :tagged_item, :tagged_results )
+ @api_client.release_digestor.nested_list_from_xml_doc( \
+ api_response.item_xml("taggedResults"), :taggedItem, :release )
end
def search(query, options={})
api_response = @api_client.make_api_request(:GET, "release/search", {:q => query}, options)
- @api_client.release_digestor.nested_list_from_xml(api_response.content.search_results, :search_result, :search_results)
+ @api_client.release_digestor.nested_list_from_xml_doc(api_response.item_xml("searchResults"), :searchResult, :release)
end
def get_tags(release_id, options = {})
api_response = @api_client.make_api_request(:GET, "release/tags", {:releaseId => release_id}, options)
- @api_client.tag_digestor.list_from_xml(api_response.content.tags)
+ @api_client.tag_digestor.list_from_xml_doc(api_response.item_xml("tags"))
end
end
Oops, something went wrong.

0 comments on commit 29edccf

Please sign in to comment.