Module: Procore::Requestable

Included in:
Client
Defined in:
lib/procore/requestable.rb

Overview

Module which defines HTTP verbs GET, POST, PUT, PATCH and DELETE. Is included in Client. Has support for Idempotency Tokens on POST and PATCH.

Examples:

Using #get:

client.get("my_open_items", per_page: 5)

Using #post:

client.post("projects", name: "New Project")

Constant Summary collapse

HTTP_EXCEPTIONS =
[
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Procore::OAuthError,
  RestClient::Exceptions::Timeout,
  RestClient::ServerBrokeConnection,
].freeze

Instance Method Summary collapse

Instance Method Details

#delete(path, version: nil, query: {}, options: {}) ⇒ Response

Examples:

Usage

client.delete("users/1", query: {}, options: {})

Parameters:

  • path (String)

    URL path

  • version (String) (defaults to: nil)

    API version

  • query (Hash) (defaults to: {})

    Query options to pass along with the request

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options:):

  • :company_id (String)

Returns:



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/procore/requestable.rb', line 226

def delete(path, version: nil, query: {}, options: {})
  full_path = full_path(path, version)

  Util.log_info(
    "API Request Initiated",
    path: full_path,
    method: "DELETE",
    headers: headers(options),
    query: query.to_s,
  )

  with_response_handling do
    RestClient::Request.execute(
      method: :delete,
      url: full_path,
      headers: headers.merge(params: query),
      timeout: Procore.configuration.timeout,
    )
  end
end

#get(path, version: nil, query: {}, options: {}) ⇒ Response

Examples:

Usage

client.get("my_open_items", query: { per_page: 5, filter: {} })

Parameters:

  • path (String)

    URL path

  • version (String) (defaults to: nil)

    API version

  • query (Hash) (defaults to: {})

    Query options to pass along with the request

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options:):

  • :company_id (Hash)

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/procore/requestable.rb', line 30

def get(path, version: nil, query: {}, options: {})
  full_path = full_path(path, version)

  Util.log_info(
    "API Request Initiated",
    path: full_path,
    method: "GET",
    query: query.to_s,
  )

  with_response_handling do
    RestClient::Request.execute(
      method: :get,
      url: full_path,
      headers: headers(options).merge(params: query),
      timeout: Procore.configuration.timeout,
    )
  end
end

#patch(path, version: nil, body: {}, options: {}) ⇒ Response

Examples:

Usage

client.patch(
  "users/1",
  body: { name: "Updated" },
  options: { idempotency_token: "key", company_id: 1 },
)

Parameters:

  • path (String)

    URL path

  • version (String) (defaults to: nil)

    API version

  • body (Hash) (defaults to: {})

    Body parameters to send with the request

  • options (Hash) (defaults to: {})

    Extra request options

Options Hash (options:):

  • :idempotency_token (String)

    | :company_id

Returns:



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/procore/requestable.rb', line 130

def patch(path, version: nil, body: {}, options: {})
  full_path = full_path(path, version)

  Util.log_info(
    "API Request Initiated",
    path: full_path,
    method: "PATCH",
    body: body.to_s,
  )

  with_response_handling(request_body: body) do
    RestClient::Request.execute(
      method: :patch,
      url: full_path,
      payload: payload(body),
      headers: headers(options),
      timeout: Procore.configuration.timeout,
    )
  end
end

#post(path, version: nil, body: {}, options: {}) ⇒ Response

Examples:

Usage

client.post(
  "users",
  body: { name: "New User" },
  options: { idempotency_token: "key", company_id: 1 },
)

Parameters:

  • path (String)

    URL path

  • version (String) (defaults to: nil)

    API version

  • body (Hash) (defaults to: {})

    Body parameters to send with the request

  • options (Hash) (defaults to: {})

    Extra request options

Options Hash (options:):

  • :idempotency_token (String)

    | :company_id

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/procore/requestable.rb', line 64

def post(path, version: nil, body: {}, options: {})
  full_path = full_path(path, version)

  Util.log_info(
    "API Request Initiated",
    path: full_path,
    method: "POST",
    body: body.to_s,
  )

  with_response_handling(request_body: body) do
    RestClient::Request.execute(
      method: :post,
      url: full_path,
      payload: payload(body),
      headers: headers(options),
      timeout: Procore.configuration.timeout,
    )
  end
end

#put(path, version: nil, body: {}, options: {}) ⇒ Response

Examples:

Usage

client.put("dashboards/1/users", body: [1,2,3], options: { company_id: 1 })

Parameters:

  • path (String)

    URL path

  • version (String) (defaults to: nil)

    API version

  • body (Hash) (defaults to: {})

    Body parameters to send with the request

  • options (Hash) (defaults to: {})

    Extra request options

Options Hash (options:):

  • :idempotency_token (String)

    | :company_id

Returns:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/procore/requestable.rb', line 95

def put(path, version: nil, body: {}, options: {})
  full_path = full_path(path, version)

  Util.log_info(
    "API Request Initiated",
    path: full_path,
    method: "PUT",
    body: body.to_s,
  )

  with_response_handling(request_body: body) do
    RestClient::Request.execute(
      method: :put,
      url: full_path,
      payload: payload(body),
      headers: headers(options),
      timeout: Procore.configuration.timeout,
    )
  end
end

#sync(path, version: nil, body: {}, options: {}) ⇒ Response

Examples:

Usage

client.sync(
  "projects/sync",
  body: {
    updates: [
     { id: 1, name: "Update 1" },
     { id: 2, name: "Update 2" },
     { id: 3, name: "Update 3" },
     ...
     ...
     { id: 5055, name: "Update 5055" },
    ]
  },
  options: { batch_size: 500, company_id: 1 },
)

Parameters:

  • path (String)

    URL path

  • version (String) (defaults to: nil)

    API version

  • body (Hash) (defaults to: {})

    Body parameters to send with the request

  • options (Hash) (defaults to: {})

    Extra request options

Options Hash (options:):

  • :company_id (String | Integer)

    | :batch_size

Returns:



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/procore/requestable.rb', line 174

def sync(path, version: nil, body: {}, options: {})
  full_path = full_path(path, version)

  batch_size = options[:batch_size] ||
    Procore.configuration.default_batch_size

  if batch_size > 1000
    batch_size = 1000
  end

  Util.log_info(
    "API Request Initiated",
    path: full_path,
    method: "SYNC",
    batch_size: batch_size,
  )

  groups = body[:updates].each_slice(batch_size).to_a

  responses = groups.map do |group|
    batched_body = body.merge(updates: group)
    with_response_handling(request_body: batched_body) do
      RestClient::Request.execute(
        method: :patch,
        url: full_path,
        payload: payload(batched_body),
        headers: headers(options),
        timeout: Procore.configuration.timeout,
      )
    end
  end

  Procore::Response.new(
    body: responses.reduce({}) do |combined, response|
      combined.deep_merge(response.body) { |_, v1, v2| v1 + v2 }
    end.to_json,
    headers: responses.map(&:headers).inject({}, &:deep_merge),
    code: 200,
    request: responses.last&.request,
    request_body: body,
  )
end