require 'httparty' module CampMinder class CampMinder include HTTParty attr_accessor :camp_id, :token @camp_id = '' @token = '' def initialize(camp_id, token) @camp_id = camp_id @token = token end def login(username, password) request_body = { i: username, p: password, c: 2, apikey: generate_api_key, callback: 'a' } response = CampMinderParty.get( "https://webapi.campminder.com/api/security/user/GetRemoteLoginRedirect", :query => request_body, :headers => { "Content-type" => 'application/javascript' } ) response.parsed_response end def add_new_lead(household1, household2, camper, lead_date, lead_source) return {:Success => false, :message => 'Missing required info.'} unless household1["Parent1"] request_body = { Household1: household1, Household2: household2, Camper: camper, LeadDate: lead_date, LeadSource: lead_source } response = CampMinderParty.post( "https://webapi.campminder.com/api/entity/person/camper/camper/addnewcamper", :body => request_body.to_json, :headers => { 'Authorization-Token' => generate_api_key, "Content-type" => 'application/json' } ) response.parsed_response end def add_custom_field_data(person_id, field_data) request_body = [{ Data: field_data, EntityTypeID: 1, ObjectID: person_id }] response = CampMinderParty.post( "https://webapi.campminder.com/api/entity/customfield/modifyentitydatafields", :body => request_body.to_json, :headers => { 'Authorization-Token' => generate_api_key, "Content-type" => 'application/json' } ) response.parsed_response end def get_telegraph_reports response = CampMinderParty.get( "https://webapi.campminder.com/api/customdata/telegraph/getsavedreports", :headers => { 'Authorization-Token' => generate_api_key, "Content-type" => 'application/json' } ) response.parsed_response end def run_report(report_id) response = CampMinderParty.get( "https://webapi.campminder.com/api/customdata/telegraph/runsavedreportcsv?reportID=#{report_id}", :headers => { 'Authorization-Token' => generate_api_key, "Content-type" => 'application/json' } ) response.parsed_response end def retrieve_person_id(email) response = CampMinderParty.get( "https://webapi.campminder.com/api/entity/person/getpersonidfromemail?email=#{email}", :headers => { 'Authorization-Token' => generate_api_key, "Content-type" => 'application/json' } ) response.parsed_response end def return_api_key generate_api_key end private def generate_api_key now = Time.now.utc now = now.strftime("%Y-%m-%dT%H:%M:%S.%LZ") digested = Digest::SHA1.hexdigest("#{@camp_id}#{now}#{@token}") "#{@camp_id}#{now}#{digested}" end end class CampMinderParty include HTTParty parser JSONPParser end end