<commit_before>"""
Functionality from the NationBuilder People API.

classes:
    People
     -- Used to access the People API.

"""

from nb_api import NationBuilderApi
import urllib2
import json


class People(NationBuilderApi):

    """
    Used to get at the People API

    See http://nationbuilder.com/people_api for info on the data returned
    """

    def __init__(self, slug, token):
        super(People, self).__init__(slug, token)

    def get_person(self, person_id):
        """
        Retrieves a person's record from NationBuilder.

        Parameters:
            person_id - The person's NationBuilder ID.

        Returns:
            A person record, as a dict.
        """
        self._authorise()
        # we need it as a string...
        person_id = urllib2.quote(str(person_id))
        url = self.GET_PERSON_URL.format(person_id)
        headers, content = self.http.request(url, headers=self.HEADERS)
        self._check_response(headers, content, "Get Person", url)
        return json.loads(content)

    def update_person(self, person_id, update_str):
        """
        Update a person's record with arbitrary info.

        Parameters:
            person_id - the person's NB id.
            update_str - string of json representation of the modifications
                to be made to that person e.g.
                {
                    "person": {
                    "first_name": "Joe",
                    "email": "<EMAIL>",
                    "phone": "303-555-0841"}
                }

        Returns:
            the updated person record.
        """
        self._authorise()
        url = self.UPDATE_PERSON_URL.format(urllib2.quote(str(person_id)))
        header, content = self.http.request(url, method="PUT",
                                            body=update_str,
                                            headers=self.HEADERS)
        self._check_response(header, content,
                             "Update person with id %d" % person_id, url)
        return json.loads(content)

    def set_recruiter_id(self, person_id, recruiter_id):
        """
        Convenience method to set a recruiter id.

        Parameters:
            person_id : the NationBuilder ID of the person whose
                recruiter is being set
            recruiter_id : the NationBuilder ID of the recruiter

        Returns the updated person record.
        """
        update_string = """ { "person": {"recruiter_id":"""
        update_string += str(recruiter_id) + "}} "
        return self.update_person(person_id, update_string)

    def set_volunteer(self, person_id, volunteer=True):
        """Convenience method to make a person a volunteer.

        Parameters:
            person_id : NationBuilder ID of the person to make a volunteer
            volunteer : Set to True if they are a volunteer,
                False if they aren't. Defaults to True

        Returns the updated person record.
        """
        update_string = """ {"person":{ "is_volunteer": """
        if volunteer:
            update_string += 'true'
        else:
            update_string += 'false'
        update_string += '}}'
        return self.update_person(person_id, update_string)

    def match_person(self, **kwargs):
        """
        Match a person by criteria.

        This will return a person that matches one or more criteria exactly,
        and it is a unique match. If there is no match, or there are multiple
        matches, it will raise an exception.

        The keyword arguments can be 'email', 'first_name', 'last_name',
        'phone', or 'mobile'.
        """
        self._authorise()
        query_string = '&'.join([key + '=' + kwargs[key] for key in
                                 kwargs.keys])
        url = self.MATCH_PERSON_URL + query_string
        hdr, cnt = self.http.request(url, headers=self.HEADERS)
        self._check_response(hdr, cnt, "Match %s" % kwargs, url)
        return json.loads(cnt)

    def get_person_by_email(self, email):
        """Returns the first person that has a given email address.

        Parameters:
            email : the email address to look for.

        Returns: A person record or None if no match.<commit_msg>fixed match_person. updated some docs<commit_after>"""
Functionality from the NationBuilder People API.

classes:
    People
     -- Used to access the People API.

"""

from nb_api import NationBuilderApi
import urllib2
import json


class People(NationBuilderApi):

    """
    Used to get at the People API

    See http://nationbuilder.com/people_api for info on the data returned
    """

    def __init__(self, slug, token):
        super(People, self).__init__(slug, token)

    def get_person(self, person_id):
        """
        Retrieves a person's record from NationBuilder.

        Parameters:
            person_id - The person's NationBuilder ID.

        Returns:
            A person record, as a dict.
        """
        self._authorise()
        # we need it as a string...
        person_id = urllib2.quote(str(person_id))
        url = self.GET_PERSON_URL.format(person_id)
        headers, content = self.http.request(url, headers=self.HEADERS)
        self._check_response(headers, content, "Get Person", url)
        return json.loads(content)

    def update_person(self, person_id, update_str):
        """
        Update a person's record with arbitrary info.

        Note that not all fields in a person record can be written. 

        Parameters:
            person_id - the person's NB id.
            update_str - string of json representation of the modifications
                to be made to that person e.g.
                {
                    "person": {
                    "first_name": "Joe",
                    "email": "<EMAIL>",
                    "phone": "303-555-0841"}
                }

        Returns:
            the updated person record.
        """
        self._authorise()
        url = self.UPDATE_PERSON_URL.format(urllib2.quote(str(person_id)))
        header, content = self.http.request(url, method="PUT",
                                            body=update_str,
                                            headers=self.HEADERS)
        self._check_response(header, content,
                             "Update person with id %d" % person_id, url)
        return json.loads(content)

    def set_recruiter_id(self, person_id, recruiter_id):
        """
        Convenience method to set a recruiter id.

        Parameters:
            person_id : the NationBuilder ID of the person whose
                recruiter is being set
            recruiter_id : the NationBuilder ID of the recruiter

        Returns the updated person record.
        """
        update_string = """ { "person": {"recruiter_id":"""
        update_string += str(recruiter_id) + "}} "
        return self.update_person(person_id, update_string)

    def set_volunteer(self, person_id, volunteer=True):
        """Convenience method to make a person a volunteer.

        Parameters:
            person_id : NationBuilder ID of the person to make a volunteer
            volunteer : Set to True if they are a volunteer,
                False if they aren't. Defaults to True

        Returns the updated person record.
        """
        update_string = """ {"person":{ "is_volunteer": """
        if volunteer:
            update_string += 'true'
        else:
            update_string += 'false'
        update_string += '}}'
        return self.update_person(person_id, update_string)

    def match_person(self, **kwargs):
        """
        Match a person by criteria.

        This will return a person that matches one or more criteria exactly,
        and it is a unique match. If there is no match, or there are multiple
        matches, it will raise an exception.

        The keyword arguments can be 'email', 'first_name', 'last_name',
        'phone', or 'mobile'.
        """
        self._authorise()
        # turn the kwargs into url-style ones. k1=v1&k2=v2...
        keyvals = ['='.join((urllib2.quote(key), urllib2.quote(val)))
                   for key, val in kwargs.iteritems()]
        query_string = '&'.join(keyvals)
        url = self.MATCH_PERSON_URL + query_string
        hdr, cnt = self.http.request(url, headers=self.HEADERS)
        self._check_response(hdr, cnt, "Match %s" % kwargs, url)
        return json.loads(cnt)

    def get_person_by_email(self, email):
        """Returns the first person that has a given email address.

        Parameters:
            email : the email address to look for.

        Returns: A person record or None if no match.
{'fraction_non_alphanumeric': 0.08975688443369052, 'fraction_numerical': 0.0036870607212812537, 'mean_word_length': 1.9068988613529805, 'pattern_counts': {'":': 16, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3450928', 'n_tokens_mistral': 2366, 'n_tokens_neox': 2289, 'n_words': 749}