require 'net/https' require 'openssl' require 'json' require 'uri' module CRP class ReviewPoster NEW_REVIEW_PATH = "/api/v1/Review/newReview" USERNAME_REVIEW_CODE = 1 DESCRIPTION_REVIEW_CODE = 2 IMAGE_REVIEW_CODE = 3 def initialize(host = ENV['CRP_HOST'], client = ENV['CRP_CLIENT'], key = ENV['CRP_KEY']) raise ArgumentError, "Missing CRP host" if host.nil? || host.strip.empty? raise ArgumentError, "Missing CRP client" if client.nil? || client.strip.empty? raise ArgumentError, "Missing CRP key" if key.nil? || key.strip.empty? @host = host @client = client @key = key end # Submits a new username review to CRP. def new_username_review(internal_review_id, gender, username, age) new_review(USERNAME_REVIEW_CODE, username, internal_review_id, gender, username, age) end # Submits a new description review to CRP. def new_description_review(description, internal_review_id, gender, username, age) new_review(DESCRIPTION_REVIEW_CODE, description, internal_review_id, gender, username, age) end # Submits a new image review to CRP. def new_image_review(image_url, internal_review_id, gender, username, age) new_review(IMAGE_REVIEW_CODE, image_url, internal_review_id, gender, username, age) end # Submits a new review to CRP. def new_review(review_type, review_content, internal_review_id, gender, username, age) uri = new_review_url(review_type, review_content, internal_review_id, gender, username, age) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE # Host certificate does not cover the subdomain request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) ReviewResponse.new(response) end # Builds a new URL needed to submit a profile review to CRP. def new_review_url(review_type, review_content, internal_review_id, gender, username, age) arguments = [{ reviewType: review_type, reviewContent: review_content, internalReviewId: internal_review_id, gender: GenderMapper.to_code(gender), username: username, age: age }].to_json signature = sign_arguments(arguments) query = URI.encode_www_form(client: @client, arguments: arguments, signature: signature) URI::HTTPS.build({host: @host, path: NEW_REVIEW_PATH, query: query}) end private def sign_arguments(arguments) digest = OpenSSL::Digest::Digest.new("sha1") OpenSSL::HMAC.hexdigest(digest, @key, arguments) end end end