Class: Pipekit::Response
- Inherits:
-
Object
- Object
- Pipekit::Response
- Defined in:
- lib/pipekit/response.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
-
#fetch(key, default = nil, opts = {}) ⇒ Object
This is more complicated than it first seems as Pipedrive returns any custom field you might create (such as a new cohort in the Cohort field) as a meaningless Pipedrive ID so it returns something semantically meaningnless such as “8” when it means “April 2016”.
- #has_key?(key) ⇒ Boolean
-
#initialize(resource, data) ⇒ Response
constructor
A new instance of Response.
- #to_h ⇒ Object (also: #to_hash)
Constructor Details
#initialize(resource, data) ⇒ Response
Returns a new instance of Response.
4 5 6 7 |
# File 'lib/pipekit/response.rb', line 4 def initialize(resource, data) @resource = resource @data = data || {} end |
Instance Method Details
#==(other) ⇒ Object
9 10 11 12 |
# File 'lib/pipekit/response.rb', line 9 def ==(other) return false unless other.respond_to?(:to_h) to_h == other.to_h end |
#[](key) ⇒ Object
14 15 16 |
# File 'lib/pipekit/response.rb', line 14 def [](key) fetch(key) end |
#fetch(key, default = nil, opts = {}) ⇒ Object
This is more complicated than it first seems as Pipedrive returns any custom field you might create (such as a new cohort in the Cohort field) as a meaningless Pipedrive ID so it returns something semantically meaningnless such as “8” when it means “April 2016”
There are two ways this method gets around this to bring back the semantically meaningful result you’re looking for here, if you put in the config under “field_values” the IDs that Pipedrive assigns to your custom values (you’ll have to search the API to work out what these are) it will look them up there.
Otherwise you can plass the “find_value_on_pipedrive” flag and it will do a call to the Pipedrive API to look it up. This is off by default as it is obviously quite slow to call an API each time you want to fetch some data
Options:
find_value_on_pipedrive (default: false) - if set to true will look up using the Pipedrive API the actual value of field (rather than the Pipedrive ID)
choose_first_value (default: true) - if Pipedrive returns an array of values this will choose the first one rather than return the array
Examples:
Normally you can just use the square brackets alias to fetch responses as though this was a hash:
response[:resource] # returns: "Dave"
However if you find when doing this Pipedrive returns its meaningless ID
response[:cohort] # returns: 1234
then you can tell Pipedrive to fetch it manually
response.fetch(:cohort, find_value_on_pipedrive: true) # returns: "April
2016“
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/pipekit/response.rb', line 67 def fetch(key, default = nil, opts = {}) opts = { find_value_on_pipedrive: false, choose_first_value: true }.merge(opts) value = fetch_value(key, default) return value_from_pipedrive(key.to_s, value) if opts[:find_value_on_pipedrive] convert_value(key, value, opts) end |
#has_key?(key) ⇒ Boolean
79 80 81 |
# File 'lib/pipekit/response.rb', line 79 def has_key?(key) data.has_key? convert_key(key) end |
#to_h ⇒ Object Also known as: to_hash
18 19 20 21 22 23 24 |
# File 'lib/pipekit/response.rb', line 18 def to_h data.inject({}) do |result, (field, value)| field_name = Config.field_name(resource, field) result[field_name.to_sym] = Config.field_value(resource, field, value) result end end |