Module: CareerBuilder::Client::Request

Included in:
CareerBuilder::Client
Defined in:
lib/career_builder/client/request.rb

Constant Summary collapse

RESUME_SERVICE_ENDPOINT_URL =
'http://ws.careerbuilder.com/resumes/resumes.asmx'
CUSTOM_KEY_TRANSFORMS =
{
  :resume_id => "ResumeID",
  :account_did => "AccountDID"
}

Instance Method Summary collapse

Instance Method Details

#auth_request(meth, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/career_builder/client/request.rb', line 7

def auth_request(meth, options = {})
  if !authenticated? && !authenticate
    raise InvalidCredentials
  end

  options.merge!(:session_token => session_token)

  request meth, options
end

#parse_terrible_response(response) ⇒ Object



35
36
37
38
39
# File 'lib/career_builder/client/request.rb', line 35

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

#request(meth, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/career_builder/client/request.rb', line 17

def request(meth, options = {})
  uri = URI.parse(RESUME_SERVICE_ENDPOINT_URL + "/#{meth}")

  options_as_xml = transform_options_to_xml(options)

  form_data = { 'Packet' => "<Packet>#{options_as_xml}</Packet>" }

  req = Net::HTTP::Post.new(uri.path)
  req.form_data = form_data
  http = Net::HTTP.new(uri.host, uri.port)
  http.set_debug_output $stderr if @debug
  response = http.start { |http|
    http.request(req)
  }

  parse_terrible_response(response)
end

#transform_key(key) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/career_builder/client/request.rb', line 46

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

#transform_key_value_to_tag(key, value) ⇒ Object



54
55
56
# File 'lib/career_builder/client/request.rb', line 54

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

#transform_options_to_xml(options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/career_builder/client/request.rb', line 58

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