StarRezApi 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/starrez_api.rb CHANGED
@@ -19,4 +19,5 @@ end
19
19
  # Load our Plugin files
20
20
  require 'starrez_api/object'
21
21
  require 'starrez_api/star_rez_api'
22
- require 'starrez_api/star_rez_report'
22
+ require 'starrez_api/star_rez_report'
23
+ require 'starrez_api/star_rez_account'
@@ -0,0 +1,137 @@
1
+ require 'httparty'
2
+ require 'xmlsimple'
3
+
4
+
5
+ class StarRezAccount
6
+ include HTTParty
7
+ base_uri STARREZ_CONFIG['base_uri']
8
+ headers 'StarRezUsername' => STARREZ_CONFIG['username'], 'StarRezPassword' => STARREZ_CONFIG['password']
9
+
10
+ attr_accessor :name, :results, :total_amount, :total_tax_amount, :total_tax_amount2, :entry_id
11
+
12
+ def self.get_balance(*args)
13
+ options = args.extract_options!
14
+ entry = args[0] || @entry_id
15
+ charge_group = args[1] || options[:charge_group]
16
+ if entry.blank?
17
+ raise IOError, "Must include an Entry ID to search"
18
+ end
19
+ url = "#{base_uri}/accounts/getbalance/#{entry}/#{charge_group}"
20
+ response = get(url)
21
+ if options[:return].eql? :response
22
+ return response
23
+ else
24
+ if response.code.eql? 404
25
+ raise ArgumentError, "Invalid Entry ID"
26
+ elsif response.code.eql? 403
27
+ raise SecurityError, 'Access Denied to API'
28
+ elsif response.code.eql? 200
29
+ doc = Hpricot(response)
30
+ account = StarRezAccount.new
31
+ account.name = doc.at("Entry").at("title").inner_html
32
+ account.total_amount = "%.2f" % doc.at("totalamount").inner_html
33
+ account.total_tax_amount = "%.2f" % doc.at("totaltaxamount").inner_html
34
+ account.total_tax_amount2 = "%.2f" % doc.at("totaltaxamount2").inner_html
35
+ account
36
+ else
37
+ return false
38
+ end
39
+ end
40
+ end
41
+
42
+ def self.create_transaction(entry,amount,conditions={},options={})
43
+ # TODO: Get this working at some point, create_payment is good enough for now.
44
+ end
45
+
46
+ def self.create_payment(entry,amount,conditions={},options={})
47
+ conditions[:description] ||= "Deposit"
48
+ url = "#{base_uri}/accounts/createpayment/#{entry}"
49
+ charge_groups_string = ""
50
+ if conditions[:charge_groups].any?
51
+ break_up_total = conditions[:charge_groups].collect { |g| g[:amount] }.reduce(&:+)
52
+ raise ArgumentError, "Payment amount and charge group breakup amounts must be equal" unless break_up_total == amount
53
+ conditions[:charge_groups].each do |charge_group|
54
+ if charge_group[:id].present?
55
+ charge_groups_string += %(<BreakUp ChargeGroupID="#{charge_group[:id]}">)
56
+ elsif charge_group[:name].present?
57
+ charge_groups_string += %(<BreakUp ChargeGroup="#{charge_group[:name]}">)
58
+ else
59
+ raise ArgumentError, "Charge group ID or name must be provided"
60
+ end
61
+ raise ArgumentError, "Amount must be provided for payment breakup" if charge_group[:amount].blank?
62
+ charge_groups_string += %(<Amount>#{charge_group[:amount]}</Amount>)
63
+ if charge_group[:comments].present?
64
+ charge_groups_string += %(<TransactionComments>#{charge_group[:comments]}</TransactionComments>)
65
+ end
66
+ if charge_group[:tag].present?
67
+ charge_groups_string += %(<TransactionTag>#{charge_group[:tag]}</TransactionTag>)
68
+ end
69
+ charge_groups_string += %(<TransactionExternalID>#{charge_group[:external_id]}</TransactionExternalID>) if charge_group[:external_id].present?
70
+ charge_groups_string += %(</BreakUp>)
71
+ end
72
+ else
73
+ raise ArgumentError, "At least one charge group must be provided in :charge_groups"
74
+ end
75
+
76
+ payment_xml = <<XML
77
+ <Payment>
78
+ <TransactionTypeEnum>Payment</TransactionTypeEnum>
79
+ <PaymentTypeID>8</PaymentTypeID>
80
+ <Description>#{conditions[:description]}</Description>
81
+ <Amount>#{amount}</Amount>
82
+ #{charge_groups_string}
83
+ </Payment>
84
+ XML
85
+
86
+ response = post(url, :body => payment_xml)
87
+ if options[:return].eql? :response
88
+ return response
89
+ else
90
+ if response.code.eql? 409
91
+ raise ArgumentError, "Duplicate Transaction Found"
92
+ elsif response.code.eql? 404
93
+ raise ArgumentError, "Invalid Entry ID"
94
+ elsif response.code.eql? 403
95
+ raise SecurityError, 'Access Denied to API'
96
+ elsif response.code.eql? 400
97
+ raise ArgumentError, "Bad Request"
98
+ elsif response.code.eql? 200
99
+ doc = Hpricot(response.body)
100
+ doc.search("paymentid").inner_html
101
+ else
102
+ return false
103
+ end
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ def self.get_condition_string(conditions)
110
+ queries = Array.new
111
+ if conditions.is_a?(Hash)
112
+ conditions.each_pair do |column, value|
113
+ query = column.to_s.camelize
114
+ if value.is_a?(Hash)
115
+ query += "[_operator%3D#{value.keys.first.to_s}]=#{self.parse_value(value[value.keys.first])}"
116
+ else
117
+ query += "=#{self.parse_value(value)}"
118
+ end
119
+ queries << query
120
+ end
121
+ return queries.join('&')
122
+ else
123
+ raise ArgumentError, "Condition needs to be a hash of values, Please review the source code"
124
+ end
125
+ end
126
+
127
+ #Just a quick method used in get_condition_string that would have been repeated
128
+ #Just takes the array and converts it into a formatted string for StarRezAPI
129
+ def self.parse_value(values)
130
+ if values.is_a?(Array)
131
+ return URI::encode(values.join(','))
132
+ else
133
+ return URI::encode(values.to_s)
134
+ end
135
+ end
136
+
137
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: StarRezApi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-08-26 00:00:00.000000000Z
13
+ date: 2011-10-31 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: httparty
17
- requirement: &70211000760360 !ruby/object:Gem::Requirement
17
+ requirement: &70239052881360 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 0.7.4
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70211000760360
25
+ version_requirements: *70239052881360
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: xml-simple
28
- requirement: &70211000759360 !ruby/object:Gem::Requirement
28
+ requirement: &70239052880820 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,9 +33,9 @@ dependencies:
33
33
  version: 1.0.12
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70211000759360
37
- description: This gem that allows the user access to the StarRez REST Web Services
38
- and Reporting API
36
+ version_requirements: *70239052880820
37
+ description: This gem that allows the user access to the StarRez REST Web Services,
38
+ Reporting, and Accounts API
39
39
  email: dreedy@housing.siu.edu
40
40
  executables: []
41
41
  extensions: []
@@ -43,6 +43,7 @@ extra_rdoc_files: []
43
43
  files:
44
44
  - lib/starrez.yml
45
45
  - lib/starrez_api/object.rb
46
+ - lib/starrez_api/star_rez_account.rb
46
47
  - lib/starrez_api/star_rez_api.rb
47
48
  - lib/starrez_api/star_rez_report.rb
48
49
  - lib/starrez_api.rb