Module: ProsperWorks::ApiOperations::Connect

Included in:
Create, Delete, Find, List, Search, Update
Defined in:
lib/prosperworks/api_operations/connect.rb

Instance Method Summary collapse

Instance Method Details

#get_uri(api_name, id = nil) ⇒ Object



8
9
10
11
12
# File 'lib/prosperworks/api_operations/connect.rb', line 8

def get_uri(api_name, id = nil)
  url = client.base_url + "#{api_name}"
  url = url + "/#{id}" unless id.nil?
  URI.parse(url)
end

#handle_multiple_response(response) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/prosperworks/api_operations/connect.rb', line 69

def handle_multiple_response(response)
  result = handle_response(nil, response)
  if result.is_a?(ProsperWorks::Errors)
    # pass the error along
    result
  else
    result.map do |res|
      new(res)
    end
  end
end

#handle_response(entity, response) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/prosperworks/api_operations/connect.rb', line 39

def handle_response(entity, response)
  case response.code.to_i
  when 200
    json_object = JSON.parse(response.body)
    return json_object if json_object.is_a?(Array)


    json_object.each_pair do |key, value|
      if entity.respond_to?(key.to_sym)
        entity.send("#{key}=", value)
      end
    end
    entity
  when Errors::BadRequest.status_code
    Errors::BadRequest
  when Errors::Unauthorized.status_code
    Errors::Unauthorized
  when Errors::Forbidden.status_code
    Errors::Forbidden
  when Errors::NotFound.status_code
    Errors::NotFound
  when Errors::Unprocessable.status_code
    Errors::Unprocessable
  when Errors::RateLimit.status_code
    Errors::RateLimit
  else
    Errors::ServerError
  end
end

#send_request(http_type, uri, entity = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/prosperworks/api_operations/connect.rb', line 14

def send_request(http_type, uri, entity = nil)
  response = nil
  headers = client.headers

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.start do
    case http_type
    when "get"
      request = Net::HTTP::Get.new(uri.request_uri, headers)
    when "post"
      request = Net::HTTP::Post.new(uri.request_uri, headers)
      request.body = entity.to_json
    when "put"
      request = Net::HTTP::Put.new(uri.request_uri, headers)
      request.body = entity.to_json
    when "delete"
      request = Net::HTTP::Delete.new(uri.request_uri, headers)
    end
    response = http.request(request)
  end

  response
end