Class: Pipedrive::Base
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Pipedrive::Base
- Extended by:
- Forwardable
- Includes:
- HTTParty
- Defined in:
- lib/pipedrive/base.rb
Overview
Base class for setting HTTParty configurations globally
Direct Known Subclasses
Activity, ActivityType, Authorization, Currency, Deal, DealField, File, Filter, Goal, Note, Organization, OrganizationField, PermissionSet, Person, PersonField, Pipeline, Product, ProductField, PushNotification, Role, SearchResult, Stage, User, UserConnection, UserSetting
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .all(response = nil, options = {}, get_absolutely_all = false) ⇒ Object
-
.authenticate(token) ⇒ Hash
Sets the authentication credentials in a class variable.
-
.bad_response(response, params = {}) ⇒ Object
Examines a bad response and raises an appropriate exception.
- .create(opts = {}) ⇒ Object
- .find(id) ⇒ Object
- .find_by_name(name, opts = {}) ⇒ Object
- .new_list(attrs) ⇒ Object
- .resource_path ⇒ Object
Instance Method Summary collapse
-
#initialize(attrs = {}) ⇒ Pipedrive::Base
constructor
Create a new Pipedrive::Base object.
-
#update(opts = {}) ⇒ Boolean
Updates the object.
Constructor Details
#initialize(attrs = {}) ⇒ Pipedrive::Base
Create a new Pipedrive::Base object.
Only used internally
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/pipedrive/base.rb', line 34 def initialize(attrs = {}) if attrs['data'] struct_attrs = attrs['data'] if attrs['additional_data'] struct_attrs.merge!(attrs['additional_data']) end else struct_attrs = attrs end super(struct_attrs) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
26 27 28 |
# File 'lib/pipedrive/base.rb', line 26 def data @data end |
Class Method Details
.all(response = nil, options = {}, get_absolutely_all = false) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/pipedrive/base.rb', line 87 def all(response = nil, ={},get_absolutely_all=false) res = response || get(resource_path, ) if res.ok? data = res['data'].nil? ? [] : res['data'].map{|obj| new(obj)} if get_absolutely_all && res['additional_data']['pagination'] && res['additional_data']['pagination'] && res['additional_data']['pagination']['more_items_in_collection'] [:query] = [:query].merge({:start => res['additional_data']['pagination']['next_start']}) data += self.all(nil,,true) end data else bad_response(res,attrs) end end |
.authenticate(token) ⇒ Hash
Sets the authentication credentials in a class variable.
68 69 70 |
# File 'lib/pipedrive/base.rb', line 68 def authenticate(token) default_params :api_token => token end |
.bad_response(response, params = {}) ⇒ Object
Examines a bad response and raises an appropriate exception
75 76 77 78 79 80 81 |
# File 'lib/pipedrive/base.rb', line 75 def bad_response(response, params={}) puts params.inspect if response.class == HTTParty::Response raise HTTParty::ResponseError, response end raise StandardError, 'Unknown error' end |
.create(opts = {}) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/pipedrive/base.rb', line 101 def create( opts = {} ) res = post resource_path, :body => opts if res.success? res['data'] = opts.merge res['data'] new(res) else bad_response(res,opts) end end |
.find(id) ⇒ Object
111 112 113 114 |
# File 'lib/pipedrive/base.rb', line 111 def find(id) res = get "#{resource_path}/#{id}" res.ok? ? new(res) : bad_response(res,id) end |
.find_by_name(name, opts = {}) ⇒ Object
116 117 118 119 |
# File 'lib/pipedrive/base.rb', line 116 def find_by_name(name, opts={}) res = get "#{resource_path}/find", :query => { :term => name }.merge(opts) res.ok? ? new_list(res) : bad_response(res,{:name => name}.merge(opts)) end |
.new_list(attrs) ⇒ Object
83 84 85 |
# File 'lib/pipedrive/base.rb', line 83 def new_list( attrs ) attrs['data'].is_a?(Array) ? attrs['data'].map {|data| self.new( 'data' => data ) } : [] end |
.resource_path ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/pipedrive/base.rb', line 121 def resource_path # The resource path should match the camelCased class name with the # first letter downcased. Pipedrive API is sensitive to capitalisation klass = name.split('::').last klass[0] = klass[0].chr.downcase klass.end_with?('y') ? "/#{klass.chop}ies" : "/#{klass}s" end |
Instance Method Details
#update(opts = {}) ⇒ Boolean
Updates the object.
52 53 54 55 56 57 58 59 60 |
# File 'lib/pipedrive/base.rb', line 52 def update(opts = {}) res = put "#{resource_path}/#{id}", :body => opts if res.success? res['data'] = Hash[res['data'].map {|k, v| [k.to_sym, v] }] @table.merge!(res['data']) else false end end |