Commit be801c51 authored by Tonee Young's avatar Tonee Young
Browse files

Continue working on RSpec testing

parent 59c2a8f0
source 'https://rubygems.org'
gem 'date'
gem 'eventmachine', '~> 1.2.7'
gem 'faye-websocket'
gem 'guard-rspec'
# gem 'net/http'
gem 'json'
gem 'date'
gem 'pi_piper'
gem 'rspec'
# Trigger once day crew reaches 300 good ratings for the day & continue lighting once per hour for the remainder of the day ##
# Trigger once day crew reaches 300 good ratings for the day
# & continue lighting once per hour for the remainder of the day ##
# Zendesk API Documentation on Satisfaction Ratings:
# https://developer.zendesk.com/rest_api/docs/core/satisfaction_ratings
# Zendesk API Documentation on Satisfaction Ratings:
# # https://developer.zendesk.com/rest_api/docs/core/satisfaction_ratings
require 'net/http'
require 'json'
......@@ -22,9 +23,9 @@ class GoodRatingsCount
def fetch_ratings_from_zendesk
uri = compose_uri
req = compose_request(uri)
set_auth_creds(req)
add_auth_creds(req)
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(req)
end
res
......@@ -32,7 +33,7 @@ class GoodRatingsCount
def write_to_log_file(http_response)
puts "\nI'm checking the count of good ratings."
puts "It's currently: #{DateTime.now}"
puts "It's currently: #{Time.now}"
puts "Response code: #{http_response.code}"
puts "Response message: #{http_response.message} \n"
puts "Number of satisfaction ratings: #{json_response_body(http_response)['satisfaction_ratings'].count}"
......@@ -47,6 +48,7 @@ class GoodRatingsCount
end
private
def compose_uri
uri = URI(ZendeskSecrets::SATISFACTION_RATINGS_ENDPOINT)
# Satisfaction ratings endpoint format: https://{subdomain}.zendesk.com/api/v2/satisfaction_ratings.json
......@@ -58,13 +60,13 @@ class GoodRatingsCount
Net::HTTP::Get.new(uri)
end
def set_auth_creds(req)
def add_auth_creds(req)
req.basic_auth ZendeskSecrets::ZENDESK_USERNAME, ZendeskSecrets::ZENDESK_PASSWORD
end
def satisfaction_goal_reached?(http_response)
if json_response_body(http_response)['satisfaction_ratings'].count >= 300
return true
true
end
end
......@@ -76,27 +78,27 @@ class GoodRatingsCount
pin.off
end
def api_call_params
def api_call_params
{
'start_time' => today_at_5am_in_seconds,
'score' => 'good',
'sort_by' => 'created_at',
'sort_order' => 'asc',
'limit' => 1000 # change this count if you need to inspect the body
}
}
end
def today_at_5am_in_seconds
DateTime.now.to_date.strftime('%s').to_i + START_COUNT_AT_5AM_EST
end
def today_at_5am_in_seconds
Time.now.to_date.strftime('%s').to_i + START_COUNT_AT_5AM_EST
end
def pin
PiPiper::Pin.new(:pin => 17, :direction => :out)
end
def pin
PiPiper::Pin.new(pin: 17, direction: :out)
end
def json_response_body(http_response)
def json_response_body(http_response)
JSON.parse(http_response.body)
end
end
end
new_good_ratings_count = GoodRatingsCount.new
......
......@@ -3,19 +3,32 @@ require 'rspec'
describe GoodRatingsCount do
context '(smoke test) spec can find class' do
let(:new_rating) { GoodRatingsCount.new }
it 'GoodRatingsCount' do
@http_response = new_rating.fetch_ratings_from_zendesk
expect(GoodRatingsCount.new).not_to be_nil
end
end
# << THIS TEST DOESN'T WORK >>
context '#write_to_log_file:'do
context 'enough good ratings triggers pegacorn lighter' do
let(:new_rating) { GoodRatingsCount.new }
it 'affects stdout/log file' do
new_rating.fetchratings_writelog_managepi
expect(new_rating.write_to_log_file(http_response)).to \
change(stdout)
it 'NEEDS WORK -- 301 satisfaction ratings satisfies #satisfaction_goal_reached?' do
@http_response = new_rating.fetch_ratings_from_zendesk
@http_response['satisfaction_ratings'] = 301
new_rating.send(:json_response_body, @http_response)
puts new_rating.send(:satisfaction_goal_reached?, @http_response)
expect(new_rating.send(:satisfaction_goal_reached?, @http_response)).to be_truthy
end
end
context '#write_to_log_file:' do
let(:new_rating) { GoodRatingsCount.new }
it 'NEEDS WORK -- affects stdout/log file' do
@http_response = new_rating.fetch_ratings_from_zendesk
new_rating.fetchratings_writelog_managepi
message = new_rating.write_to_log_file(@http_response)
expect(new_rating.write_to_log_file(@http_response)).to \
change($stdout, message)
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment