<commit_before># Copyright (C) 2016 <NAME> (<EMAIL>).
# Licensed under the terms of the MIT license. See LICENCE for details.

module Dashlane
    class Fetcher
        LATEST_URI = URI "https://www.dashlane.com/12/backup/latest"

        def self.fetch username, uki, http = Net::HTTP
            response = http.post_form LATEST_URI, {
                login: username,
                lock: "nolock",
                timestamp: 1,
                sharingTimestamp: 0,
                uki: uki
            }

            raise NetworkError if response.code != "200"

            parse_and_check_for_errors response.body
        end

        private

        def self.parse_and_check_for_errors json
            parsed = JSON.load json rescue raise InvalidResponseError.new "Invalid JSON object"

            if parsed.key? "error"
                raise UnknownError.new parsed["error"].fetch "message", "Unknown error"
            end

            if parsed["objectType"] == "message"
                case message = parsed["content"]
                when "Incorrect authentification"
                    raise AuthenticationError.new "Invalid username or password"
                else
                    raise UnknownError.new message
                end
            end

            # TODO: Do some integrity check to see if it's the actual vault we've got here!

            parsed
        end
    end
end<commit_msg>Don't need new in raise<commit_after># Copyright (C) 2016 <NAME> (<EMAIL>).
# Licensed under the terms of the MIT license. See LICENCE for details.

module Dashlane
    class Fetcher
        LATEST_URI = URI "https://www.dashlane.com/12/backup/latest"

        def self.fetch username, uki, http = Net::HTTP
            response = http.post_form LATEST_URI, {
                login: username,
                lock: "nolock",
                timestamp: 1,
                sharingTimestamp: 0,
                uki: uki
            }

            raise NetworkError if response.code != "200"

            parse_and_check_for_errors response.body
        end

        private

        def self.parse_and_check_for_errors json
            parsed = JSON.load json rescue raise InvalidResponseError, "Invalid JSON object"

            if parsed.key? "error"
                raise UnknownError, parsed["error"].fetch("message", "Unknown error")
            end

            if parsed["objectType"] == "message"
                case message = parsed["content"]
                when "Incorrect authentification"
                    raise AuthenticationError, "Invalid username or password"
                else
                    raise UnknownError, message
                end
            end

            # TODO: Do some integrity check to see if it's the actual vault we've got here!

            parsed
        end
    end
end
{'fraction_non_alphanumeric': 0.07140344706296166, 'fraction_numerical': 0.007738304607808653, 'mean_word_length': 1.6091743119266055, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1271629', 'n_tokens_mistral': 757, 'n_tokens_neox': 683, 'n_words': 232}