lib/pipekit/request.rb in pipekit-1.0.2 vs lib/pipekit/request.rb in pipekit-1.2.0
- old
+ new
@@ -33,18 +33,13 @@
# it couldn't find anything.
#
# This also uses the "request_all_pages" config option when set to do
# multiple requests, getting around Pipedrive's pagination
def search_by_field(field:, value:)
- query = {field_type: "#{resource}Field",
- field_key: Config.field_id(resource, field),
- return_item_ids: true,
- term: Config.field_value_id(resource, field, value),
- exact_match: 1
- }
+ query = search_by_field_query(field, value)
- get_request("/searchResults/field", query).response(resource)
+ get_request("/searchResults/field", query).response
end
# Public: Pipedrive GET API call - does a GET request to the Pipedrive API
# based on the resource passed in the initialiser
#
@@ -67,22 +62,20 @@
def post(data)
response_from self.class.post(uri, options(body: data))
end
- private
-
attr_reader :resource
def _get(uri, query, result)
- return result.response(resource) unless result.fetch_next_request?
+ return result.response unless result.fetch_next_request?
_get(uri, query, result + get_request(uri, query, result.next_start))
end
def get_request(uri, query, start = 0)
response = self.class.get(uri, options(query: {limit: pagination_limit, start: start}.merge(query)))
- Result.new(response)
+ Result.new(resource, response)
end
def response_from(response_data)
Result.response(resource, response_data)
end
@@ -111,17 +104,27 @@
# { "123abc": "Dave" }
#
# meaning you don't have to worry about the custom IDs
def parse_body(body)
body.reduce({}) do |result, (field, value)|
- field = Config.field_id(resource, field)
value = Config.field_value_id(resource, field, value)
+ field = Config.field_id(resource, field)
result.tap { |result| result[field] = value }
end
end
def pagination_limit
Config.fetch(:pagination_limit, DEFAULT_PAGINATION_LIMIT)
+ end
+
+ def search_by_field_query(field = nil, value = nil)
+ {
+ field_type: "#{resource}Field",
+ field_key: Config.field_id(resource, field),
+ return_item_ids: true,
+ term: Config.field_value_id(resource, field, value),
+ exact_match: 1
+ }
end
end
end