lib/services/reg_center.rb in Active-0.0.14 vs lib/services/reg_center.rb in Active-0.0.17

- old
+ new

@@ -4,28 +4,26 @@ class RegCenterError < StandardError; end class RegCenter < IActivity require 'nokogiri' require 'open-uri' + require 'digest/sha1' attr_accessor :asset_type_id # attr_reader :metadata_loaded # EXAMPLE Data hash # {:asset_id=>"A9EF9D79-F859-4443-A9BB-91E1833DF2D5", :substitution_url=>"1878023", :asset_type_name=>"Active.com Event Registration", # :asset_name=>"Fitness, Pilates Mat Class (16 Yrs. &amp; Up)", :url=>"http://www.active.com/page/Event_Details.htm?event_id=1878023", # :asset_type_id=>"EA4E860A-9DCD-4DAA-A7CA-4A77AD194F65", :xmlns=>"http://api.asset.services.active.com"} def initialize(data={}) - # need to hold on to original data @data = HashWithIndifferentAccess.new(data) || HashWithIndifferentAccess.new - @api_data_loaded = false @asset_type_id = "EA4E860A-9DCD-4DAA-A7CA-4A77AD194F65" - get_app_api end def source - :reg_center + :primary end def title if @data.has_key?("event") && @data["event"].has_key?("eventName") cleanup_reg_string(@data["event"]["eventName"]) @@ -71,31 +69,31 @@ channels = @data["event"]["channels"]["channel"] if channels.class==Array channels.each do |c| return c["channelName"] if c.has_key?("primaryChannel") && c["primaryChannel"]=="true" end + nil else #hash return channels["channelName"] if channels.has_key?("primaryChannel") && channels["primaryChannel"]=="true" + return nil end - #fallback - return category end end def address if @data.has_key?("event") && @data["event"].has_key?("eventAddress") && !@data["event"]["eventAddress"].blank? - @address = { + @address = validated_address({ :name => @data["event"]["eventLocation"], :address => @data["event"]["eventAddress"], :city => @data["event"]["eventCity"], :state => @data["event"]["eventState"], :zip => @data["event"]["eventZip"], :lat => @data["event"]["latitude"], :lng => @data["event"]["longitude"], :country => @data["event"]["eventCountry"] - } + } ) end end def start_date DateTime.parse @data["event"]["eventDate"] if @data.has_key?("event") && @data["event"].has_key?("eventDate") @@ -129,28 +127,52 @@ else #hash ret +="<div><b>" + eventDetail["eventDetailsName"] + ":</b> " + cleanup_reg_string(eventDetail["eventDetailsValue"]) + "</div>" end end - ret + return ret + elsif @data.has_key?("event") && @data["event"].has_key?("eventDescription") + return @data["event"]["eventDescription"] end end def cleanup_reg_string(input) input.gsub("\r","").gsub("\n","").gsub("\"","""").gsub("\342\200\234","""").gsub("\342\200\235","""") end + def contact_email + if @data.has_key?("event") && @data["event"].has_key?("eventContactEmail") + return @data["event"]["eventContactEmail"] + end + end + # EXAMPLE # lazy load the data for some_crazy_method # def some_crazy # return @some_crazy unless @some_crazy.nil? # @some_crazy = @data[:some_crazy_method_from_ats].split replace twist bla bla bla # end def self.find_by_id(id) #local id - return RegCenter.new({:id=>id}) + + search_hash = Digest::SHA1.hexdigest("reg_#{id}") + if Active.CACHE + cached_version = Active.CACHE.get(search_hash) + return cached_version if cached_version + end + + begin + doc = Nokogiri::XML(open("http://apij.active.com/regcenter/event/#{id}")) + reg = RegCenter.new(Hash.from_xml(doc.to_s)) + Active.CACHE.set(search_hash, reg) if Active.CACHE + rescue Exception => e + raise RegCenterError, "Couldn't find Reg Center activity with the id of #{id} - #{e.inspect}" + return + end + + return reg end private # def self.get_asset_metadata(id) # c = Savon::Client.new("http://api.amp.active.com/asset-service/services/AssetService?wsdl") @@ -180,20 +202,20 @@ # metadata = ATS.get_asset_metadata(@asset_id) # @data.merge! Hash.from_xml(metadata.to_hash[:get_asset_metadata_response][:out])["importSource"]["asset"] # @metadata_loaded=true # end - def get_app_api - puts "loading reg center api" - begin - doc = Nokogiri::XML(open("http://apij.active.com/regcenter/event/#{@data[:id]}")) - @data.merge! Hash.from_xml doc.to_s - @api_data_loaded=true - rescue - raise RegCenterError, "Couldn't find Reg Center activity with the id of #{id}" - return - end - end + # def get_app_api + # puts "loading reg center api" + # begin + # doc = Nokogiri::XML(open("http://apij.active.com/regcenter/event/#{@data[:id]}")) + # @data.merge! Hash.from_xml doc.to_s + # @api_data_loaded=true + # rescue + # raise RegCenterError, "Couldn't find Reg Center activity with the id of #{id}" + # return + # end + # end end # end ats end end