Skip to content

Commit

Permalink
more "refactoring" - getting rid of last peachy leftovers
Browse files Browse the repository at this point in the history
  • Loading branch information
filip committed Jun 16, 2012
1 parent ba9968b commit 29edccf
Show file tree
Hide file tree
Showing 56 changed files with 408 additions and 384 deletions.
6 changes: 6 additions & 0 deletions Gemfile
@@ -0,0 +1,6 @@
source 'http://rubygems.org'

gem 'rspec'
gem 'oauth'
gem 'nokogiri'
gem 'will_paginate'
76 changes: 27 additions & 49 deletions lib/sevendigital/digestion_tract/api_response_digestor.rb
@@ -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
2 changes: 1 addition & 1 deletion lib/sevendigital/digestion_tract/chart_item_digestor.rb
Expand Up @@ -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

Expand Down
55 changes: 14 additions & 41 deletions lib/sevendigital/digestion_tract/digestor.rb
Expand Up @@ -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}"))
Expand All @@ -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)
Expand Down
20 changes: 9 additions & 11 deletions lib/sevendigital/management/artist_manager.rb
@@ -1,5 +1,3 @@
require "peachy"

module Sevendigital

# provides access to Artist related API methods (artist/*)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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


Expand Down
8 changes: 4 additions & 4 deletions lib/sevendigital/management/basket_manager.rb
Expand Up @@ -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


Expand Down
4 changes: 2 additions & 2 deletions lib/sevendigital/management/oauth_manager.rb
Expand Up @@ -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)
Expand Down
20 changes: 9 additions & 11 deletions lib/sevendigital/management/release_manager.rb
@@ -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 = {})
Expand All @@ -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
Expand Down

0 comments on commit 29edccf

Please sign in to comment.