Sha256: f4b7f15e999ef61dde6caa748ba9528d31483500a4c90d8086a3e721f60cb0f6

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

module CareerBuilder

  class Request

    RESUME_SERVICE_ENDPOINT_URL = 'http://ws.careerbuilder.com/resumes/resumes.asmx'

    attr_reader :options, :client

    def initialize(client, options = {})
      @client, @options = client, options
    end

    def perform
      validate_options if defined?(self.class.const_get(:VALID_OPTIONS))
    end

    private

    def validate_options
      raise ArgumentError, "Invalid options #{invalid_options}" unless valid_options?
    end

    def invalid_options
      (options.keys - self.class.const_get(:VALID_OPTIONS))
    end

    def valid_options?
      invalid_options.empty?
    end

    def session_token
      client.session_token
    end

    def parse_terrible_response(response)
      xml_body = Nokogiri::XML(response.body) # not sure why I have to do it this way
      inner_xml = xml_body.children.first
      inner_xml.text
    end

    CUSTOM_KEY_TRANSFORMS = {
      :resume_id => "ResumeID",
      :account_did => "AccountDID"
    }

    def transform_key(key)
      if custom_transform = CUSTOM_KEY_TRANSFORMS[key]
        custom_transform
      else
        key.to_s.camelize
      end
    end

    def transform_key_value_to_tag(key, value)
      "<#{transform_key(key)}>#{value}</#{transform_key(key)}>"
    end

    def transform_options_to_xml(options)
      elements = []

      # let's make sure SessionToken is always at the top of the request
      if session_key = options.delete(:session_token)
        elements << transform_key_value_to_tag(:session_token, session_token)
      end

      options.sort_by { |k, v| k.to_s }.each do |key, value|
        elements << transform_key_value_to_tag(key, value)
      end

      elements.join
    end

    def perform_request(method, packet_contents)
      parse_terrible_response Net::HTTP.post_form(URI.parse(RESUME_SERVICE_ENDPOINT_URL + "/#{method}"), 'Packet' => "<Packet>#{packet_contents}</Packet>")
    rescue Errno::ECONNRESET
      warn "The connection was reset, retrying"
      retry
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
career_builder-0.1.1 lib/career_builder/request.rb
career_builder-0.1.0 lib/career_builder/request.rb