Skip to content

Instantly share code, notes, and snippets.

@whereisciao
Created October 30, 2012 16:09
Show Gist options
  • Save whereisciao/3981206 to your computer and use it in GitHub Desktop.
Save whereisciao/3981206 to your computer and use it in GitHub Desktop.
Example script for Surveygizmo classes
class Campaign < Hashie::Mash
include SurveyConnection
def survey_id
fetch("survey_id")
end
def campaign_id
fetch("id")
end
def contacts(options = {})
@contacts ||= begin
fetch_collection(:contacts, survey_id, campaign_id).collect do |c|
c["survey_id"] = survey_id
c["campaign_id"] = campaign_id
Contact.new(c)
end
ensure
[]
end
end
def delivery_progress
@delivery_progress ||= begin
contacts.inject({}) do |memo, contact|
memo[contact.esubscriberstatus] ||= 0
memo[contact.esubscriberstatus] += 1
memo
end
end
end
end
class Contact < Hashie::Mash
include SurveyConnection
end
Ice Cream Survey (474874)
Midwest (713764)
Sent=1
New England (713768)
Sent=1
Mexico (713772)
Sent=1
France (713776)
Best Power Ranger Survey (473929)
Kids from the 1990s (713764)
Sent=1
Kids from the 2000s (713768)
Sent=1
Kids from the 1980s (713772)
Sent=1
class Survey
include SurveyConnection
attr_reader :survey_id, :name
def initialize(survey_id, name="")
@survey_id = survey_id
@name = name
end
def survey_campaigns(options = {})
@campaigns ||= begin
fetch_collection(:survey_campaigns, survey_id).collect do |c|
c["survey_id"] = survey_id
Campaign.new(c)
end
end
end
end
module SurveyConnection
def client
Surveygizmo
end
def fetch_collection(name, *arguments)
data = []
arguments << {:page => 0}
begin
arguments.last[:page] += 1
response = client.send(name, *arguments)
data = data.concat response
end while response.page < response.total_pages
return data
end
end
surveys = []
surveys << Survey.new(474874, "Ice Cream Survey")
surveys << Survey.new(473929, "Best Power Ranger Survey")
surveys << Survey.new(244534, "MEME Knowledge Survey")
surveys.each do |survey|
puts "#{survey.name} (#{survey.survey_id})"
survey.survey_campaigns.each do |campaign|
puts "\t#{campaign.name} (#{campaign.id})"
puts "\t\t#{campaign.delivery_progress.to_a.collect{|c| c.join("=")}.join("\t")}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment