Class: Pipekit::Request
- Inherits:
-
Object
- Object
- Pipekit::Request
- Includes:
- HTTParty
- Defined in:
- lib/pipekit/request.rb
Constant Summary collapse
- PIPEDRIVE_URL =
"https://api.pipedrive.com/v1"
- DEFAULT_PAGINATION_LIMIT =
500
Instance Attribute Summary collapse
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
- #_get(uri, query, result) ⇒ Object
-
#get(id = "", query = {}) ⇒ Object
Public: Pipedrive GET API call - does a GET request to the Pipedrive API based on the resource passed in the initialiser.
- #get_request(uri, query, start = 0) ⇒ Object
-
#initialize(resource) ⇒ Request
constructor
A new instance of Request.
- #options(query: {}, body: {}) ⇒ Object
- #pagination_limit ⇒ Object
-
#parse_body(body) ⇒ Object
Replaces custom fields with their Pipedrive ID if the ID is defined in the configuration.
- #post(data) ⇒ Object
- #put(id, data) ⇒ Object
- #response_from(response_data) ⇒ Object
-
#search_by_field(field:, value:) ⇒ Object
Public: Pipedrive /searchField API call.
- #search_by_field_query(field = nil, value = nil) ⇒ Object
- #uri(id = "") ⇒ Object
Constructor Details
Instance Attribute Details
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
66 67 68 |
# File 'lib/pipekit/request.rb', line 66 def resource @resource end |
Instance Method Details
#_get(uri, query, result) ⇒ Object
68 69 70 71 |
# File 'lib/pipekit/request.rb', line 68 def _get(uri, query, result) return result.response unless result.fetch_next_request? _get(uri, query, result + get_request(uri, query, result.next_start)) end |
#get(id = "", query = {}) ⇒ Object
Public: Pipedrive GET API call - does a GET request to the Pipedrive API based on the resource passed in the initialiser
id - If the resource being searched for has an id query - An optional query string start - The offset with which to start the query
As long as “request_all_pages” is not set to false in the config this will recursively call ‘#get` until all the pages of the request have been fetched from pipedrive Pipedrive until everything available has been received
53 54 55 56 |
# File 'lib/pipekit/request.rb', line 53 def get(id = "", query = {}) uri = uri(id) _get(uri, query, get_request(uri, query)) end |
#get_request(uri, query, start = 0) ⇒ Object
73 74 75 76 |
# File 'lib/pipekit/request.rb', line 73 def get_request(uri, query, start = 0) response = self.class.get(uri, (query: {limit: pagination_limit, start: start}.merge(query))) Result.new(resource.singular, response) end |
#options(query: {}, body: {}) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/pipekit/request.rb', line 86 def (query: {}, body: {}) { query: query.merge(api_token: Config.fetch(:api_token)), body: parse_body(body) } end |
#pagination_limit ⇒ Object
114 115 116 |
# File 'lib/pipekit/request.rb', line 114 def pagination_limit Config.fetch(:pagination_limit, DEFAULT_PAGINATION_LIMIT) end |
#parse_body(body) ⇒ Object
Replaces custom fields with their Pipedrive ID if the ID is defined in the configuration
So if the body looked like this with a custom field called middle_name:
{ middle_name: “Dave” }
And it has a Pipedrive ID (“123abc”), this will put in this custom ID
{ “123abc”: “Dave” }
meaning you don’t have to worry about the custom IDs
106 107 108 109 110 111 112 |
# File 'lib/pipekit/request.rb', line 106 def parse_body(body) body.reduce({}) do |result, (field, value)| value = Config.field_value_id(resource.singular, field, value) field = Config.field_id(resource.singular, field) result.tap { |result| result[field] = value } end end |
#post(data) ⇒ Object
62 63 64 |
# File 'lib/pipekit/request.rb', line 62 def post(data) response_from self.class.post(uri, (body: data)) end |
#put(id, data) ⇒ Object
58 59 60 |
# File 'lib/pipekit/request.rb', line 58 def put(id, data) response_from self.class.put(uri(id), (body: data)) end |
#response_from(response_data) ⇒ Object
78 79 80 |
# File 'lib/pipekit/request.rb', line 78 def response_from(response_data) Result.response(resource.singular, response_data) end |
#search_by_field(field:, value:) ⇒ Object
Public: Pipedrive /searchField API call.
type - Type of the field:
:person - person fields
:deal - deal fields
field - The name of the field.
If it's the custom field the id of the field should be stored in `config/pipedrive.yml`.
value - The value of the field.
Examples
search_by_field(field: :cohort, value: 119)
search_by_field(field: :github_username, value: "octocat")
Returns an array of Response objects or throws a ResourceNotFoundError if 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
36 37 38 39 40 |
# File 'lib/pipekit/request.rb', line 36 def search_by_field(field:, value:) query = search_by_field_query(field, value) get_request("/searchResults/field", query).response end |
#search_by_field_query(field = nil, value = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/pipekit/request.rb', line 118 def search_by_field_query(field = nil, value = nil) { field_type: "#{resource.singular}Field", field_key: Config.field_id(resource.singular, field), return_item_ids: true, term: Config.field_value_id(resource.singular, field, value), exact_match: 1 } end |
#uri(id = "") ⇒ Object
82 83 84 |
# File 'lib/pipekit/request.rb', line 82 def uri(id = "") "/#{resource.pluralized}/#{id}".chomp("/") end |