Module: LinkedIn::RequestHelpers

Included in:
Client
Defined in:
lib/linked_in/request_helpers.rb

Instance Method Summary collapse

Instance Method Details

#delete(path, options = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/linked_in/request_helpers.rb', line 27

def delete(path, options={})
  path = "/v1#{path}"
  response = access_token.delete(path, options)
  raise_errors(response)
  response
end

#get(path, options = {}) ⇒ Object



5
6
7
8
9
10
# File 'lib/linked_in/request_helpers.rb', line 5

def get(path, options={})
  path = "/v1#{path}"
  response = access_token.get(path, options)
  raise_errors(response)
  response.body
end

#post(path, body = '', options = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/linked_in/request_helpers.rb', line 12

def post(path, body='', options={})
  path = "/v1#{path}"
  default_options = { 'Content-Type' => 'application/xml' }
  response = access_token.post(path, body, default_options.merge(options))
  raise_errors(response)
  response
end

#put(path, body, options = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/linked_in/request_helpers.rb', line 20

def put(path, body, options={})
  path = "/v1#{path}"
  response = access_token.put(path, body, options)
  raise_errors(response)
  response
end

#raise_errors(response) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/linked_in/request_helpers.rb', line 35

def raise_errors(response)
  # Even if the XML answer contains the HTTP status code, LinkedIn also sets this code
  # in the HTTP answer (thankfully).
  if response.code.to_i >= 400
    data = LinkedIn::Error.from_xml(response.body)
    message = "(#{response.code}): #{data.message} - #{data.code}"

    case response.code.to_i
    when 400
      raise BadRequest.new(data), message
    when 401
      raise Unauthorized.new(data), message
    when 403
      raise Forbidden.new(data), message
    when 404
      raise NotFound.new(data), message
    when 500
      raise InformLinkedIn, "LinkedIn had an internal error. Please let them know in the forum. #{message}"
    when 502..503
      raise Unavailable, message
    end
  end
end

#to_query(options) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/linked_in/request_helpers.rb', line 59

def to_query(options)
  query_string = options.inject([]) do |collection, opt|
    collection << "#{opt[0]}=#{opt[1]}"
    collection
  end * '&'
  URI.escape(query_string)
end

#to_uri(path, options) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/linked_in/request_helpers.rb', line 67

def to_uri(path, options)
  uri = URI.parse(path)

  if options && options != {}
    uri.query = to_query(options)
  end
  uri.to_s
end