Class: BlueStateDigital::Contribution

Inherits:
ApiDataModel show all
Defined in:
lib/blue_state_digital/contribution.rb

Defined Under Namespace

Classes: ContributionExternalIdMissingException, ContributionSaveFailureException, ContributionSaveValidationException

Constant Summary collapse

FIELDS =
[
  :external_id,
  :prefix,:firstname,:middlename,:lastname,:suffix,
  :transaction_dt,:transaction_amt,:cc_type_cd,:gateway_transaction_id,
  :contribution_page_id,:stg_contribution_recurring_id,:contribution_page_slug,
  :outreach_page_id,:source,:opt_compliance,
  :addr1,:addr2,:city,:state_cd,:zip,:country,
  :phone,:email,
  :employer,:occupation,
  :custom_fields
]

Constants inherited from ApiDataModel

ApiDataModel::FIELD

Instance Attribute Summary

Attributes inherited from ApiDataModel

#connection

Instance Method Summary collapse

Methods inherited from ApiDataModel

#initialize, #to_hash

Constructor Details

This class inherits a constructor from BlueStateDigital::ApiDataModel

Instance Method Details

#as_json(options = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/blue_state_digital/contribution.rb', line 36

def as_json(options={})
  fields_to_exclude = []
  fields_to_exclude << :contribution_page_id if contribution_page_id.nil?
  fields_to_exclude << :contribution_page_slug if contribution_page_slug.nil?
  super(options.merge({except: fields_to_exclude}))
end

#saveObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/blue_state_digital/contribution.rb', line 43

def save
  begin
    if connection
      response = connection.perform_request( 
        '/contribution/add_external_contribution',
        {accept: 'application/json'},
        'POST',
        [self].to_json
      )
      begin
        response = JSON.parse(response)  
      rescue
        raise ContributionSaveFailureException.new(response)
      end
      if(response['summary']['missing_ids']>0)
        raise ContributionExternalIdMissingException.new
      elsif(response['summary']['failures']>0)
        raise ContributionSaveValidationException.new(response['errors'])
      end 
    else
      raise NoConnectionException.new
    end
    #TODO shouldn't we be returning true or false and set the errors as in ActiveModel?
    true
  rescue => e
    raise e
  end
end