<commit_before># mapit.rb:
# Client interface for MaPit
#
# Copyright (c) 2010 UK Citizens Online Democracy. All rights reserved.
# WWW: http://www.mysociety.org
#
# $Id: rabxresttorb.pl,v 1.3 2010-06-08 15:00:04 louise Exp $
#

require 'config'
require 'rabx'
require 'json'
require 'net/http'
require 'uri'

module MySociety
  
  module MaPit
  
    def MaPit.do_call_rest_rabx(*params)
      base_url = MySociety::Config.get("MAPIT_URL")
      return MySociety::RABX.call_rest_rabx(base_url, params)
    end
    
    # Call the new MaPit, parse returned JSON
    def MaPit.call(function, params, options={})
      response = self.do_call(function, params, options)
      return :bad_request if response.code == '400'
      return :not_found if response.code == '404'
      return JSON.parse(response.body)
    end
    
    BAD_POSTCODE = 2001        #    String is not in the correct format for a postcode. 
    POSTCODE_NOT_FOUND = 2002        #    The postcode was not found in the database. 
    AREA_NOT_FOUND = 2003        #    The area ID refers to a non-existent area. 

    def MaPit.get_voting_areas(postcode, generation = nil)

      #* MaPit.get_voting_areas POSTCODE [GENERATION]
      #
      #  Return voting area IDs for POSTCODE. If GENERATION is given, use that,
      #  otherwise use the current generation.
<commit_msg>Map errors in getting data or parsing it to a service_unavailable error message.<commit_after># mapit.rb:
# Client interface for MaPit
#
# Copyright (c) 2010 UK Citizens Online Democracy. All rights reserved.
# WWW: http://www.mysociety.org
#
# $Id: rabxresttorb.pl,v 1.3 2010-06-08 15:00:04 louise Exp $
#

require 'config'
require 'rabx'
require 'json'
require 'net/http'
require 'uri'

module MySociety
  
  module MaPit
  
    def MaPit.do_call_rest_rabx(*params)
      base_url = MySociety::Config.get("MAPIT_URL")
      return MySociety::RABX.call_rest_rabx(base_url, params)
    end
    
    # Call the new MaPit, parse returned JSON
    def MaPit.call(function, params, options={})
      begin
        response = self.do_call(function, params, options)
      rescue Timeout::Error
        return :service_unavailable
      rescue
        return :service_unavailable
      end
      return :bad_request if response.code == '400'
      return :not_found if response.code == '404'
      return :service_unavailable if response.code == '503'
      begin
        json = JSON.parse(response.body)
      rescue
        return :service_unavailable
      end
      return json
    end
    
    BAD_POSTCODE = 2001        #    String is not in the correct format for a postcode. 
    POSTCODE_NOT_FOUND = 2002        #    The postcode was not found in the database. 
    AREA_NOT_FOUND = 2003        #    The area ID refers to a non-existent area. 

    def MaPit.get_voting_areas(postcode, generation = nil)

      #* MaPit.get_voting_areas POSTCODE [GENERATION]
      #
      #  Return voting area IDs for POSTCODE. If GENERATION is given, use that,
      #  otherwise use the current generation.

{'fraction_non_alphanumeric': 0.08390918065153011, 'fraction_numerical': 0.025995393221454426, 'mean_word_length': 2.8287153652392947, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2704208', 'n_tokens_mistral': 1069, 'n_tokens_neox': 968, 'n_words': 295}