Class: CapsuleCRM::Opportunity

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations, Associations::BelongsTo, Associations::HasMany, Virtus
Defined in:
lib/capsule_crm/opportunity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#milestoneObject

Returns the value of attribute milestone.



24
25
26
# File 'lib/capsule_crm/opportunity.rb', line 24

def milestone
  @milestone
end

#ownerObject

Returns the value of attribute owner.



24
25
26
# File 'lib/capsule_crm/opportunity.rb', line 24

def owner
  @owner
end

Class Method Details

.all(options = {}) ⇒ Object

Public: Get all opportunities from Capsule. The list can be restricted and/or paginated with various query parameters sent through the options hash.

options - The Hash of allowed query parameters for Capsule (default: {}):

:milestone      - The String milestone name
:lastmodified   - The Date when the opportunity was last modified
:tag            - The String tag to search for
:start          - The Integer first record to be returned in pagination.
The results start with an index of 1
:limit          - The Integer maximum number of matching records to be
returned

Examples

CapsuleCRM::Opportunity.all

CapsuleCRM::Opportunity.all(start: 10, limit: 20)

Returns a ResultsProxy of opportunities



94
95
96
97
98
99
100
# File 'lib/capsule_crm/opportunity.rb', line 94

def self.all(options = {})
  init_collection(
    CapsuleCRM::Connection.get(
      '/api/opportunity', options
    )['opportunities']['opportunity']
  )
end

.create(attributes = {}) ⇒ Object

Public: Create a new opportunity in capsulecrm

attributes - The Hash of opportunity attributes (default: {}):

:name                 - The String opportunity name
:description          - The String opportunity description
:currency             - The String currency code
:value                - The Float opportunity (financial) value
:duration_basis       - The String duration basis
:duration             - The Integer duration (for opportunities
with a repeating (not FIXED) duratin basis
:party_id             - The Integer party id
:milestone_id         - The Integer milestone id
:expected_close_date  - The DateTime when the opportunity
is expected to be closed
:actual_close_date    - The DateTime when the opportunity
was actually closed
:probability          - The Float probability that this
opportunity will be won

Examples

CapsuleCRM::opportunity.create(name: ‘Test’, milestone_id: 1)

Returns a CapsuleCRM::opportunity



156
157
158
# File 'lib/capsule_crm/opportunity.rb', line 156

def self.create(attributes = {})
  new(attributes).tap(&:save)
end

.create!(attributes = {}) ⇒ Object

Public: Create a new opportunity in capsulecrm and raise a CapsuleCRM::Errors::InvalidRecord error if not possible

attributes - The Hash of opportunity attributes (default: {}):

:name                 - The String opportunity name
:description          - The String opportunity description
:currency             - The String currency code
:value                - The Float opportunity (financial) value
:duration_basis       - The String duration basis
:duration             - The Integer duration (for opportunities
with a repeating (not FIXED) duratin basis
:party_id             - The Integer party id
:milestone_id         - The Integer milestone id
:expected_close_date  - The DateTime when the opportunity
is expected to be closed
:actual_close_date    - The DateTime when the opportunity
was actually closed
:probability          - The Float probability that this
opportunity will be won

Examples

CapsuleCRM::opportunity.create!(name: ‘Test’, milestone_id: 1)

Returns a CapsuleCRM



185
186
187
# File 'lib/capsule_crm/opportunity.rb', line 185

def self.create!(attributes = {})
  new(attributes).tap(&:save!)
end

.deleted(since) ⇒ Object

Public: Get all deleted opportunities since the specified date

since - The Date to start checking for deleted opportunities

Examples

CapsuleCRM::Opportunity.deleted(1.week.ago)

Returns a ResultsProxy of opportunities



111
112
113
114
115
116
117
# File 'lib/capsule_crm/opportunity.rb', line 111

def self.deleted(since)
  init_collection(
    CapsuleCRM::Connection.get(
      '/api/opportunity/deleted', since: since
    )['deletedOpportunities']['deletedOpportunity']
  )
end

.find(id) ⇒ Object

Public: Find an opportunity by id

id - The Integer ID

Examples

CapsuleCRM::Opportunity.find(id)

Returns a CapsuleCRM::Opportunity



128
129
130
# File 'lib/capsule_crm/opportunity.rb', line 128

def self.find(id)
  new CapsuleCRM::Connection.get("/api/opportunity/#{id}")['opportunity']
end

Instance Method Details

#attributes=(attributes) ⇒ Object

Public: Set the attributes of a opportunity

attributes - The Hash of attributes (default: {}):

:name                 - The String opportunity name
:description          - The String opportunity description
:currency             - The String currency code
:value                - The Float opportunity (financial) value
:duration_basis       - The String duration basis
:duration             - The Integer duration (for opportunities
with a repeating (not FIXED) duratin basis
:party_id             - The Integer party id
:milestone_id         - The Integer milestone id
:expected_close_date  - The DateTime when the opportunity
is expected to be closed
:actual_close_date    - The DateTime when the opportunity
was actually closed
:probability          - The Float probability that this
opportunity will be won

Examples

CapsuleCRM::Opportunity.new

Returns a CapsuleCRM::Opportunity



68
69
70
71
72
# File 'lib/capsule_crm/opportunity.rb', line 68

def attributes=(attributes)
  CapsuleCRM::HashHelper.underscore_keys!(attributes)
  super(attributes)
  self
end

#destroyObject

Public: Delete the opportunity in capsule

Examples

opportunity.destroy

Return the CapsuleCRM::Opportunity



333
334
335
336
# File 'lib/capsule_crm/opportunity.rb', line 333

def destroy
  self.id = nil if CapsuleCRM::Connection.delete("/api/opportunity/#{id}")
  self
end

#new_record?Boolean

Public: Determine whether this CapsuleCRM::opportunity is a new record or not

Returns a Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/capsule_crm/opportunity.rb', line 235

def new_record?
  !id
end

#persisted?Boolean

Public: Determine whether or not this CapsuleCRM::opportunity has already been persisted to capsulecrm

Returns a Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/capsule_crm/opportunity.rb', line 243

def persisted?
  !new_record?
end

#saveObject

Public: If the opportunity already exists in capsule then update them, otherwise create a new opportunity

Examples

opportunity = CapsuleCRM::Opportunity.new(name: ‘Test’, milestone_id: 1) opportunity.save

opportunity = CapsuleCRM::Opportunity.find(1) opportunity.name = ‘Another Test’ opportunity.save

Returns a CapsuleCRM::opportunity



202
203
204
205
206
207
208
# File 'lib/capsule_crm/opportunity.rb', line 202

def save
  if valid?
    new_record? ? create_record : update_record
  else
    false
  end
end

#save!Object

Public: If the opportunity already exists in capsule then update them, otherwise create a new opportunity. If the opportunity is not valid then a CapsuleCRM::Errors::RecordInvalid exception is raised

Examples

opportunity = CapsuleCRM::Opportunity.new(name: ‘Test, milestone_id: 1) opportunity.save

opportunity = CapsuleCRM::Opportunity.find(1) opportunity.name = ‘Another test’ opportunity.save

Returns a CapsuleCRM::opportunity



224
225
226
227
228
229
230
# File 'lib/capsule_crm/opportunity.rb', line 224

def save!
  if valid?
    new_record? ? create_record : update_record
  else
    raise CapsuleCRM::Errors::RecordInvalid.new(self)
  end
end

#to_capsule_jsonObject

Public: Build a hash of attributes and camelize the keys for capsule

Examples

opportunity.to_capsule_json

Returns a Hash



318
319
320
321
322
323
324
# File 'lib/capsule_crm/opportunity.rb', line 318

def to_capsule_json
  {
    opportunity: CapsuleCRM::HashHelper.camelize_keys(
      attributes.dup.delete_if { |key, value| value.blank? }
    )
  }.stringify_keys
end

#update_attributes(attributes = {}) ⇒ Object

Public: Update the opportunity in capsule

attributes - The Hash of opportunity attributes (default: {}):

:name                 - The String opportunity name
:description          - The String opportunity description
:currency             - The String currency code
:value                - The Float opportunity (financial) value
:duration_basis       - The String duration basis
:duration             - The Integer duration (for opportunities
with a repeating (not FIXED) duratin basis
:party_id             - The Integer party id
:milestone_id         - The Integer milestone id
:expected_close_date  - The DateTime when the opportunity
is expected to be closed
:actual_close_date    - The DateTime when the opportunity
was actually closed
:probability          - The Float probability that this
opportunity will be won

Examples

opportunity = CapsuleCRM::Opportunity.find(1) opportunity.update_attributes name: ‘A New Name’

Returns a CapsuleCRM::opportunity



271
272
273
274
# File 'lib/capsule_crm/opportunity.rb', line 271

def update_attributes(attributes = {})
  self.attributes = attributes
  save
end

#update_attributes!(attributes = {}) ⇒ Object

Public: Update the opportunity in capsule. If the person is not valid then a CapsuleCRM::Errors::RecordInvalid exception will be raised

attributes - The Hash of opportunity attributes (default: {}):

:name                 - The String opportunity name
:description          - The String opportunity description
:currency             - The String currency code
:value                - The Float opportunity (financial) value
:duration_basis       - The String duration basis
:duration             - The Integer duration (for opportunities
with a repeating (not FIXED) duratin basis
:party_id             - The Integer party id
:milestone_id         - The Integer milestone id
:expected_close_date  - The DateTime when the opportunity
is expected to be closed
:actual_close_date    - The DateTime when the opportunity
was actually closed
:probability          - The Float probability that this
opportunity will be won

Examples

opportunity = CapsuleCRM::Opportunity.find(1) opportunity.update_attributes! name: ‘A New Name’

> CapsuleCRM::Opportunity

opportunity.update_attributes! name: nil

> CapsuleCRM::Errors::RecordInvalid

Returns a CapsuleCRM::opportunity



306
307
308
309
# File 'lib/capsule_crm/opportunity.rb', line 306

def update_attributes!(attributes = {})
  self.attributes = attributes
  save!
end