<commit_before>require 'connector/base'
require 'oauth'
require 'oauth/signature/rsa/sha1'
require 'model/company'
require 'model/attendee'
require 'model/invoice'
require 'nokogiri'
require 'logger'
require 'renderer'
require 'monkey-date'


module Connector
  class Xero < Base
    attr_writer :access_token
    attr_accessor :date, :settlement

    def self.logger
      @@logger ||= Logger.new("xero-#{Date.today}.log")
    end

    def self.logger=(logger)
      @@logger = logger
    end

    def logger
      self.class.logger
    end

    def initialize(consumer_key=nil, secret_key=nil, options={})
      @consumer_key = consumer_key
      @secret_key = secret_key
      @options = options
      @renderer = Renderer::Hml.new
      self.date = Date.today
      self.settlement = 15
    end

    def access_token
      return @access_token if @access_token
      consumer = OAuth::Consumer.new(@consumer_key, @secret_key, @options)
      @access_token = OAuth::AccessToken.new(consumer, @consumer_key, @secret_key)
    end

    def put_invoice(invoice)
      uri = 'https://api.xero.com/api.xro/2.0/Invoice'
      invoice_as_xml = create_invoice(invoice)
      logger.info "send #{invoice_as_xml}"
      response = access_token.put(uri, invoice_as_xml)
      logger.info "get #{response.code}, #{response.body}"

      invoice.invoice_id = parse_invoice_response(response)
      invoice
    end

    def put_contact(company)
      uri = 'https://api.xero.com/api.xro/2.0/Contact'
      xml = create_contact(company)
      logger.info "send #{xml}"
      response = access_token.put(uri, xml)
      logger.info "get #{response.code}, #{response.body}"

      company.invoicing_id = parse_contact_response(response)
      company      
    end

    # parse response and return xpath content for /Response/Invoices/Invoice/InvoiceNumber
    def parse_invoice_response(response)
      case Integer(response.code)
        when 200 then
          extract_invoice_id(response)
        else
          fail!(response)
      end
    end

    # parse response and return xpath content for /Response/Invoices/Invoice/InvoiceNumber
    def parse_contact_response(response)
      case Integer(response.code)
        when 200 then
          extract_contact_id(response)
        else
          fail!(response)
      end
    end

    def extract_invoice_id(response)
      doc = Nokogiri::XML(response.body)
      doc.xpath('/Response/Invoices/Invoice/InvoiceNumber').first.content
    end

    def extract_contact_id(response)
      doc = Nokogiri::XML(response.body)
      doc.xpath('/Response/Contacts/Contact/ContactID').first.content
    end

    def fail!(response)
      doc = Nokogiri::XML(response.body)
      messages = doc.xpath('//Message').to_a.map { |element| element.content }.uniq
      raise Problem, messages.join(', ')
    end

    def create_invoice(invoice)
      @renderer.render('xero/invoice.xml.haml', :invoice => invoice)
    end

    def create_contact(company)<commit_msg>extract parse_response, that yields response when HttpSuccess<commit_after>require 'connector/base'
require 'oauth'
require 'oauth/signature/rsa/sha1'
require 'model/company'
require 'model/attendee'
require 'model/invoice'
require 'nokogiri'
require 'logger'
require 'renderer'
require 'monkey-date'


module Connector
  class Xero < Base
    attr_writer :access_token
    attr_accessor :date, :settlement

    def self.logger
      @@logger ||= Logger.new("xero-#{Date.today}.log")
    end

    def self.logger=(logger)
      @@logger = logger
    end

    def logger
      self.class.logger
    end

    def initialize(consumer_key=nil, secret_key=nil, options={})
      @consumer_key = consumer_key
      @secret_key = secret_key
      @options = options
      @renderer = Renderer::Hml.new
      self.date = Date.today
      self.settlement = 15
    end

    def access_token
      return @access_token if @access_token
      consumer = OAuth::Consumer.new(@consumer_key, @secret_key, @options)
      @access_token = OAuth::AccessToken.new(consumer, @consumer_key, @secret_key)
    end

    def put_invoice(invoice)
      uri = 'https://api.xero.com/api.xro/2.0/Invoice'
      invoice_as_xml = create_invoice(invoice)
      logger.info "send #{invoice_as_xml}"
      response = access_token.put(uri, invoice_as_xml)
      logger.info "get #{response.code}, #{response.body}"

      invoice.invoice_id = parse_invoice_response(response)
      invoice
    end

    def put_contact(company)
      uri = 'https://api.xero.com/api.xro/2.0/Contact'
      xml = create_contact(company)
      logger.info "send #{xml}"
      response = access_token.put(uri, xml)
      logger.info "get #{response.code}, #{response.body}"

      company.invoicing_id = parse_contact_response(response)
      company      
    end

    # parse response and return xpath content for /Response/Invoices/Invoice/InvoiceNumber
    def parse_response(response)
      case Integer(response.code)
        when 200 then
          return yield(response)
        else
          fail!(response)
      end
    end

    # parse response and return xpath content for /Response/Invoices/Invoice/InvoiceNumber
    def parse_invoice_response(response)
      parse_response(response) {|r| return extract_invoice_id(r)}
    end

    # parse response and return xpath content for /Response/Invoices/Invoice/InvoiceNumber
    def parse_contact_response(response)
      parse_response(response) {|r| return extract_contact_id(r)}
    end

    def extract_invoice_id(response)
      doc = Nokogiri::XML(response.body)
      doc.xpath('/Response/Invoices/Invoice/InvoiceNumber').first.content
    end

    def extract_contact_id(response)
      doc = Nokogiri::XML(response.body)
      doc.xpath('/Response/Contacts/Contact/ContactID').first.content
    end

    def fail!(response)
      doc = Nokogiri::XML(response.body)
      messages = doc.xpath('//Message').to_a.map { |element| element.content }.uniq
      raise Problem, messages.join(', ')
    end

    def create_invoice(invoice)
      @renderer.render('xero/invoice.xml.haml', :invoice => invoice)
    end

    def create_contact(company)
{'fraction_non_alphanumeric': 0.10610252237591537, 'fraction_numerical': 0.0037428803905614322, 'mean_word_length': 3.062128222075347, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '473554', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1916, 'n_words': 394}