<commit_before>require "customer_scoring/version"
require 'unirest'
require 'vcr'
require 'webmock'
require 'net/http'

module CustomerScoring
  class Customer
    def self.score(income, zipcode, age)
      errors = []      

      if income != income.scan(/\d+/)[0]
        errors << "Invalid income: #{income}"
      end

      if zipcode != zipcode.scan(/^\d{5}/)[0]
          errors << "Invalid zip code: #{zipcode}"
      end

      if age == age.scan(/\d+/)[0]
        unless (0..130).include?(age.to_i)
          errors << "Invalid age: #{age}"
        end
      else 
        errors << "Invalid age: #{age}"
      end

      if errors.length > 0
        return errors
      else
        uri = URI.parse('http://yahoo.com/customer_scoring?'),
        params = {:income => "#{income}", :zipcode => "#{zipcode}", :age => "#{age}"}
        # uri = URI('http://yahoo.com/customer_scoring?income=#{URI.encode(income)}&zipcode=#{URI.encode(zipcode)}&age=#{URI.encode(age)}')
        api_response_hash = Net::HTTP.get(uri)

        # api_response_hash = Unirest.get("http://yahoo.com/customer_scoring?income=#{income}&zipcode=#{zipcode}&age=#{age}")
        http_response = api_response_hash.code
          if http_response == 200
            response_body = api_response_hash.body
            return OpenStruct.new(response_body)
          else
            if http_response.between?(400, 499)
                "#{http_response} error - Please verify your paramaters"
            elsif http_response.between?(500, 599)
                "#{http_response} error - Please try again.  If error continues contact administrator."
            else
                "Something isn't right... please try again."
            end
          end<commit_msg>ready to try vcr again<commit_after>require "customer_scoring/version"
require 'vcr'
require 'net/http'

module CustomerScoring
  class Customer
    def self.score(income, zipcode, age)
      errors = []      

      if income != income.scan(/\d+/)[0]
        errors << "Invalid income: #{income}"
      end

      if zipcode != zipcode.scan(/^\d{5}/)[0]
          errors << "Invalid zip code: #{zipcode}"
      end

      if age == age.scan(/\d+/)[0]
        unless (0..130).include?(age.to_i)
          errors << "Invalid age: #{age}"
        end
      else 
        errors << "Invalid age: #{age}"
      end

      if errors.length > 0
        return errors
      else
        # response = Net::HTTP.get_response(URI("https://www.yahoo.com/customer_scoring?income=#{"income"}&zipcode=#{"zipcode"}&age=#{"age"}"))
        response = Net::HTTP.get_response(URI("https://api.myjson.com/bins/4jbfc"))
        http_response_code = response.code.to_i
          if http_response_code == 200
            response_body = response.body
            temp_score = []
            score_hash = {}

            temp_score << response_body.scan(/\d(.....)\d/)[0][0]
            score_hash[:propensity] = temp_score[0]
            
            temp_score << response_body.scan(/\b[A-Z]/)
            score_hash[:ranking] = temp_score[1][0]

            return OpenStruct.new(score_hash)
          else
            if http_response_code.between?(400, 499)
                "#{http_response} error - Please verify your paramaters"
            elsif http_response_code.between?(500, 599)
                "#{http_response} error - Please try again.  If error continues contact administrator."
            else
                "Something isn't right... please try again."
            end
          end
{'fraction_non_alphanumeric': 0.14058506106219823, 'fraction_numerical': 0.015336552115876171, 'mean_word_length': 2.122340425531915, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3479235', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1032, 'n_words': 226}