class ElectricProfile::Client
Attributes
data[R]
error[R]
successful[R]
Public Class Methods
new()
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 9 def initialize raise ArgumentError, 'Customer token is required' unless @customer_token = ENV['ELECTRIC_PROFILE_CUSTOMER_TOKEN'] raise ArgumentError, 'API URL is required' unless @api_url = ENV['ELECTRIC_PROFILE_API_URL'] @http_headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Accept-Charset' => 'utf-8', 'User-Agent' => 'electric_profile_ruby', 'Panel-Services-Auth-Token' => @customer_token } end
Public Instance Methods
create_profiler(profiler_attributes)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 60 def create_profiler(profiler_attributes) uri = URI(@api_url + 'profiler') http_request = Net::HTTP::Post.new uri, @http_headers http_request.body = { "data" => profiler_attributes }.to_json perform_request(uri, http_request) end
create_question(question_attributes)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 34 def create_question(question_attributes) uri = URI(@api_url + 'question') http_request = Net::HTTP::Post.new uri, @http_headers http_request.body = { "data" => question_attributes }.to_json perform_request(uri, http_request) end
create_reward(reward_attributes)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 88 def create_reward(reward_attributes) uri = URI(@api_url + 'reward') http_request = Net::HTTP::Post.new uri, @http_headers http_request.body = { "data" => reward_attributes }.to_json perform_request(uri, http_request) end
fetch_profiler(profiler_id)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 54 def fetch_profiler(profiler_id) uri = URI(@api_url + 'profiler?id=' + profiler_id) http_request = Net::HTTP::Get.new uri, @http_headers perform_request(uri, http_request) end
fetch_profilers(page_size = 99999)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 48 def fetch_profilers(page_size = 99999) uri = URI(@api_url + "profiler?PageSize=#{page_size}") http_request = Net::HTTP::Get.new uri, @http_headers perform_request(uri, http_request) end
fetch_question(question_id)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 28 def fetch_question(question_id) uri = URI(@api_url + 'question?id=' + question_id) http_request = Net::HTTP::Get.new uri, @http_headers perform_request(uri, http_request) end
fetch_questions(page_size = 99999)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 22 def fetch_questions(page_size = 99999) uri = URI(@api_url + "question?PageSize=#{page_size}") http_request = Net::HTTP::Get.new uri, @http_headers perform_request(uri, http_request) end
fetch_respondent_profiler(profiler_id, user_id, locale)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 74 def fetch_respondent_profiler(profiler_id, user_id, locale) raise ArgumentError, 'Survey token is required' unless survey_token = ENV['ELECTRIC_PROFILE_SURVEY_TOKEN'] @http_headers['Panel-Services-Auth-Token'] = survey_token uri = URI("#{@api_url}respondentProfiler?profilerId=#{profiler_id}&userId=#{user_id}&locale=#{locale}") http_request = Net::HTTP::Get.new uri, @http_headers perform_request(uri, http_request) end
fetch_reward(reward_id)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 82 def fetch_reward(reward_id) uri = URI(@api_url + 'reward?id=' + reward_id) http_request = Net::HTTP::Get.new uri, @http_headers perform_request(uri, http_request) end
queue_reward_delivery(reward_id)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 102 def queue_reward_delivery(reward_id) raise ArgumentError, 'Reward ID is required' unless reward_id uri = URI(@api_url + 'reward/' + reward_id + '/queueDelivery') http_request = Net::HTTP::Put.new uri, @http_headers perform_request(uri, http_request) end
update_profiler(profiler_attributes)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 67 def update_profiler(profiler_attributes) uri = URI(@api_url + 'profiler') http_request = Net::HTTP::Put.new uri, @http_headers http_request.body = { "data" => profiler_attributes }.to_json perform_request(uri, http_request) end
update_question(question_attributes)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 41 def update_question(question_attributes) uri = URI(@api_url + 'question') http_request = Net::HTTP::Put.new uri, @http_headers http_request.body = { "data" => question_attributes }.to_json perform_request(uri, http_request) end
update_reward(reward_attributes)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 95 def update_reward(reward_attributes) uri = URI(@api_url + 'reward') http_request = Net::HTTP::Put.new uri, @http_headers http_request.body = { "data" => reward_attributes }.to_json perform_request(uri, http_request) end
Private Instance Methods
perform_request(uri, http_request)
click to toggle source
# File lib/electric_profile_ruby/client.rb, line 110 def perform_request(uri, http_request) use_ssl = @api_url.include? "https" http_response = Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http| http.request http_request end case http_response when Net::HTTPSuccess, Net::HTTPRedirection @data = JSON.parse http_response.body @successful = true else @error = "#{http_response.code}: #{http_response.body}" @successful = false end return @successful end