Permalink
Browse files

Parse review data. Can now embed in page.

  • Loading branch information...
beaucollins committed Sep 8, 2010
1 parent cd89ae8 commit 1159183281a5e0572bf448a1bfb5fe24297c7147
View
@@ -12,13 +12,13 @@ begin
gemspec.email = "beaucollins@gmail.com"
gemspec.homepage = "http://github.com/beaucollins/power_reviews"
gemspec.authors = ["Beau Collins"]
- gemspec.add_runtime_dependency 'javan-whenever'
+ gemspec.add_runtime_dependency 'whenever'
gemspec.add_runtime_dependency 'fastercsv'
gemspec.add_development_dependency 'activesupport'
gemspec.add_development_dependency 'shoulda'
end
rescue LoadError
- puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
+ puts "Jeweler not available. Install it with: gem install jeweler"
end
View
@@ -1,4 +1,5 @@
require 'power_reviews/config'
+require 'power_reviews/review_data'
require 'power_reviews/feed_item'
require 'power_reviews/feed'
@@ -1,20 +1,31 @@
module PowerReviews
class Config
- def self.merchant_group_id
- @@merchant_group_id
- end
+ class << self
+ def merchant_group_id
+ @merchant_group_id
+ end
- def self.merchant_group_id=id
- @@merchant_group_id = id
- end
+ def merchant_group_id=id
+ @merchant_group_id = id
+ end
- def self.site_id
- @@site_id
- end
+ def site_id
+ @site_id
+ end
- def self.site_id=id
- @@site_id = id
+ def site_id=id
+ @site_id = id
+ end
+
+ def review_data_path
+ @review_data_path || 'public/system/pwr'
+ end
+
+ def review_data_path=path
+ @review_data_path = path
+ end
+
end
end
@@ -0,0 +1,89 @@
+require 'find'
+require 'rexml/document'
+
+module PowerReviews
+
+ class ReviewData
+
+ class << self
+
+ # retrieve the summary data for the given page_id
+ def summary(page_id)
+ document.summary(page_id)
+ end
+
+ protected
+
+ def document
+ @document ||= new
+ end
+
+ end
+
+
+ # initializes a new ReviewData document
+ def initialize(search_path=nil)
+ @search_path = search_path || PowerReviews::Config.review_data_path
+ end
+
+ def summary(page_id)
+ node = nil
+ summary_data.elements.each("//pageid") do |e|
+ node = e.parent if e.get_text == page_id
+ end
+ !node.nil? ? Summary.new(node) : nil
+ end
+
+ def summary_data
+ @summary_data ||= read_document('review_data_summary')
+ end
+
+ def product_data
+ @product_data ||= read_document('review_data_complete')
+ end
+
+ protected
+
+ def read_document(name)
+ REXML::Document.new(File.new(xml_documents[name]))
+ end
+
+ def xml_documents
+ return @files unless @files.nil?
+ files = {}
+
+ Find.find(@search_path) do |path|
+ bn = File.basename(path, '.xml')
+ files[bn] = path if ['review_data_complete', 'review_data_summary'].include?(bn)
+ end
+ @files = files
+ @files
+ end
+
+ class Summary
+
+ attr_reader :full_name, :average_rating, :reviews
+
+ def initialize(element)
+ @element = element
+ @full_name = text_for_element('name')
+ @average_rating = text_for_element('average_rating_decimal').to_s.to_f
+ @reviews = text_for_element('fullreviews').to_s.to_i
+ end
+
+ protected
+
+ def text_for_element(name)
+ element = nil
+ @element.each_element(name) { |e| element = e if element.nil? }
+ element.nil? ? '' : element.get_text
+ end
+
+
+ end
+
+
+ end
+
+
+end
View
@@ -14,36 +14,44 @@ class Sync
:ftp => Protocols::Ftp
}
- # Execute the necessary actions
- def self.execute
- self.start do |client, config|
+ class << self
+
+ # Execute the necessary actions
+ def execute
+ self.start do |client, config|
- zip_path = "#{RAILS_ROOT}/public/system/reviews.zip"
+ zip_path = "#{Rails.root}/public/system/reviews.zip"
- unless client.done?
- FileUtils.mkdir_p(File.dirname(zip_path))
- # we need the correct path to be setup
- client.copy_zip(zip_path)
- `cd #{File.dirname(zip_path)} && unzip -o #{zip_path}`
- client.done!
- end
+ unless client.done?
+ FileUtils.mkdir_p(File.dirname(zip_path))
+ # we need the correct path to be setup
+ client.copy_zip(zip_path)
+ `cd #{File.dirname(zip_path)} && unzip -o #{zip_path}`
+ client.done!
+ end
- # zip up the powerreviews data tell the client to store it
- data_path = "#{RAILS_ROOT}/tmp/review_data.csv"
- File.open(data_path, 'w') do |f|
- f.puts PowerReviews::Feed.process
- end
- client.copy_data_feed(data_path)
+ # zip up the powerreviews data tell the client to store it
+ data_path = "#{RAILS_ROOT}/tmp/review_data.csv"
+ File.open(data_path, 'w') do |f|
+ f.puts PowerReviews::Feed.process
+ end
+ client.copy_data_feed(data_path)
+ end
end
- end
- # Given the environment loads up the yaml file and creates a new instance
- def self.start
- config = YAML::load(File.open("#{RAILS_ROOT}/config/power_reviews.yml"))[RAILS_ENV] || {}
- syncer = PROTOCOLS[config.delete('protocol').intern].new(config)
- yield syncer, config
- syncer.cleanup
+ # Given the environment loads up the yaml file and creates a new instance
+ def start
+ config = self.configure
+ syncer = PROTOCOLS[config.delete('protocol').intern].new(config)
+ yield syncer, config
+ syncer.cleanup
+ end
+
+ def configure
+ @config ||= YAML::load(File.open("#{RAILS_ROOT}/config/power_reviews.yml"))[Rails.environment] || {}
+ end
+
end
end
@@ -1,12 +1,50 @@
module PowerReviews
module ViewHelpers
+
+ # generates hReview compatible HTML for the given page_id
+ # renders the content as HTML at render time instead of relying
+ # on the Power Reviews javascript library to render the review content
+ def power_reviews_hreview(page_id, &blk)
+
+ summary = PowerReviews::ReviewData.summary(page_id)
+
+ if block_given?
+ yield summary unless summary.nil?
+ else summary.nil?
+ <<-review
+ <div class='hreview-aggregate'>
+ <span class="item">
+ <span class="fn">#{summary.full_name}</span>
+ </span>
+ <span class="rating">
+ <span class="average">#{summary.average_rating}</span>
+ </span>
+ based on
+ <span class="count">#{summary.reviews}</span> user reviews.
+ </div>
+ review
+
+ end
+ end
+
+ # Includes the power reviews javascript, optionally provide the path to
+ # the javascript file if not installed in the default location
def power_reviews_javascript_tag(path_to_file = '/system/pwr/engine/js/full')
unless @power_reviews_javascript_needed.nil?
javascript_include_tag(path_to_file)
end
end
+ # Generates the javascript variables for a page that will be rendering
+ # PowerReviews elements
+ #
+ # Options:
+ #
+ # +:locale+ - default: en_US
+ # +:zip_location+ - where Power Reviews data is stored default: /system/
+ # +:pr_style_sheet - Location of custom Power Reviews stylesheet default: "" (empty string)
+ #
def power_reviews_javascript_variables(options = {})
options = {
:locale => 'en_US',
@@ -17,7 +55,14 @@ def power_reviews_javascript_variables(options = {})
options.collect { |key, val| "var pr_#{key} = '#{val}';" unless val.blank? }.compact!.join("\n")
end
end
-
+
+ # Generates JavaScript to display the Power Reviews "snippet" element. Will show a summary
+ # of the reviews for the given +page_id+.
+ #
+ # Arguments:
+ # +page_id+ - the identifier for the product
+ # +write_review_url+ - the url to link to a page where the user can author a review
+ # +review_ancor+ - the url or anchor that links to the full reviews. Default: "#ReviewHeader"
def power_reviews_snippet(page_id, write_review_url, review_anchor = '#ReviewHeader')
@power_reviews_javascript_needed = true
javascript_tag do
@@ -32,7 +77,12 @@ def power_reviews_snippet(page_id, write_review_url, review_anchor = '#ReviewHea
eos
end
end
-
+
+ # Displays the full reviews for the given +page_id+
+ #
+ # Arguments:
+ # +page_id+ - The identifier for the item to show reviews for
+ # +write_review_url+ - A url for a page where the user can write a review for the given product
def power_reviews_display(page_id, write_review_url)
@power_reviews_javascript_needed = true
javascript_tag do
@@ -47,6 +97,21 @@ def power_reviews_display(page_id, write_review_url)
end
end
+ # Generates the javascript that renders the PowerReviews authoring form.
+ #
+ # Arguments:
+ # +page_id+ - The id of the item being reviewd
+ #
+ # Options:
+ # +:locale+ default ""
+ # +:site_id+ default: PowerReviews::Config.site_id
+ # +:merchant_group_id+ default: PowerReviews::Config.merchant_group_id
+ # +:source+ default: 'web'
+ # +:merchant_user_id+ default: ''
+ # +:merchant_user_email+ default: ''
+ # +:promo_code+ default: ''
+ # +:style_sheet+ default: ''
+
def power_reviews_form(page_id, options = {})
@power_reviews_javascript_needed = true
options = {
Oops, something went wrong.

0 comments on commit 1159183

Please sign in to comment.