Class: BillForward::BillingEntity

Inherits:
Object
  • Object
show all
Defined in:
lib/bill_forward/billing_entity.rb

Direct Known Subclasses

InsertableEntity

Constant Summary collapse

@@payload_verbs =
['post', 'put']
@@no_payload_verbs =
['get', 'delete']
@@all_verbs =
@@payload_verbs + @@no_payload_verbs

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_params = nil, client = nil) ⇒ BillingEntity

Returns a new instance of BillingEntity.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bill_forward/billing_entity.rb', line 7

def initialize(state_params = nil, client = nil)
	raise AbstractInstantiateError.new('This abstract class cannot be instantiated!') if self.class == MutableEntity

	client = self.class.singleton_client if client.nil?
	state_params = {} if state_params.nil?

	TypeCheck.verifyObj(Client, client, 'client')
	TypeCheck.verifyObj(Hash, state_params, 'state_params')

	@_registered_entities = Hash.new
	@_registered_entity_arrays = Hash.new

	@_client = client
	# initiate with empty state params
	# use indifferent hash so 'id' and :id are the same
	@_state_params = HashWithIndifferentAccess.new
	# legacy Ruby gives us this 'id' chuff. we kinda need it back.
	@_state_params.instance_eval { undef id if defined? id }
	# populate state params now
	unserialize_all state_params
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *arguments, &block) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/bill_forward/billing_entity.rb', line 150

def method_missing(method_id, *arguments, &block)
	def camel_case_lower(operand)
		elements = operand.split('_')
		camel_cased = elements.join('_').camelize(:lower)

		camel_cased.gsub!(/Id/, 'ID') if elements.size > 1 && elements.last =~ /id=?\Z/

		camel_cased
	end

	qualified = camel_case_lower method_id.to_s

	if qualified != method_id.to_s
		return self.send(qualified.intern, *arguments)
	end

	# no call to super; our criteria is all keys.
	#setter
	if /^(\w+)=$/ =~ qualified
		return set_state_param($1, arguments.first)
	end
	#getter
	get_state_param(qualified)
end

Class Attribute Details

.resource_pathObject

Returns the value of attribute resource_path.



34
35
36
# File 'lib/bill_forward/billing_entity.rb', line 34

def resource_path
  @resource_path
end

Instance Attribute Details

#_clientObject

Returns the value of attribute _client.



5
6
7
# File 'lib/bill_forward/billing_entity.rb', line 5

def _client
  @_client
end

Class Method Details

._load(data) ⇒ Object



141
142
143
# File 'lib/bill_forward/billing_entity.rb', line 141

def _load(data)
	build_entity(JSON.parse(data))
end

.build_entity(entity) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bill_forward/billing_entity.rb', line 48

def build_entity(entity)
	if entity.is_a? Hash
		# either we are given a serialized entity
		# we must unserialize it

		# this entity should the same client as we do
		client = @_client

		return self.new(entity, client)
	end
	if entity.is_a? self
		# or we are given an already-constructed entity
		# just return it as-is

		# for consistency we might want to set this entity to use the same client as us. Let's not for now.
		return entity
	end
	if
		expectedClassName = self.name
		actualClassName = entity.class.name
		raise TypeError.new("Expected instance of either: 'Hash' or '#{expectedClassName}' at argument 'entity'. "+
			"Instead received: '#{actualClassName}'")
	end

	new_entity
end

.build_entity_array(entity_hashes) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/bill_forward/billing_entity.rb', line 40

def build_entity_array(entity_hashes)
	TypeCheck.verifyObj(Array, entity_hashes, 'entity_hashes')

	entity_hashes.map do |hash|
		self.build_entity(hash)
	end
end

.get_all(query_params = {}, custom_client = nil) ⇒ Object



137
138
139
# File 'lib/bill_forward/billing_entity.rb', line 137

def get_all(query_params = {}, custom_client = nil)
	self.request_many('get', '', query_params, custom_client)
end

.get_by_id(id, query_params = {}, custom_client = nil) ⇒ Object

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
135
# File 'lib/bill_forward/billing_entity.rb', line 127

def get_by_id(id, query_params = {}, custom_client = nil)
	raise ArgumentError.new("id cannot be nil") if id.nil?

	endpoint = sprintf('%s',
		ERB::Util.url_encode(id)
		)

	self.request_first('get', endpoint, query_params, custom_client)
end

.request_ambiguous(*args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bill_forward/billing_entity.rb', line 75

def request_ambiguous(*args)
	plurality = args.shift
	verb = args.shift
	endpoint = args.shift

	payload = nil;
	haspayload = @@payload_verbs.include?(verb)
	if (haspayload)
       			payload_typed = args.shift
       			payload = payload_typed.serialize
       		end

	query_params = args.shift
	custom_client = args.shift

	client = client.nil? \
	? singleton_client \
	: custom_client

	route = resource_path.path
	url_full = "#{route}/#{endpoint}"
	method = "#{verb}_#{plurality}"

	arguments = [url_full, query_params]
	arguments.insert(1, payload) if haspayload

	# puts verb

	client.send(method.intern, *arguments)
end

.request_first_heterotyped(*args) ⇒ Object



113
114
115
116
117
118
# File 'lib/bill_forward/billing_entity.rb', line 113

def request_first_heterotyped(*args)
	response_type = args.shift
	arguments = ['first']+args
	result = self.send(:request_ambiguous, *arguments)
	response_type.build_entity(result)
end

.request_many_heterotyped(*args) ⇒ Object



106
107
108
109
110
111
# File 'lib/bill_forward/billing_entity.rb', line 106

def request_many_heterotyped(*args)
	response_type = args.shift
	arguments = ['many']+args
	results = self.send(:request_ambiguous, *arguments)
	response_type.build_entity_array(results)
end

.singleton_clientObject



36
37
38
# File 'lib/bill_forward/billing_entity.rb', line 36

def singleton_client
	Client.default_client
end

Instance Method Details

#[](key) ⇒ Object



175
176
177
# File 'lib/bill_forward/billing_entity.rb', line 175

def [](key)
	method_missing(key)
end

#[]=(key, value) ⇒ Object



179
180
181
182
# File 'lib/bill_forward/billing_entity.rb', line 179

def []=(key, value)
	set_key = key.to_s+'='
	method_missing(set_key, value)
end

#_dump(_level) ⇒ Object



146
147
148
# File 'lib/bill_forward/billing_entity.rb', line 146

def _dump(_level)
	to_json
end

#camel_case_lower(operand) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/bill_forward/billing_entity.rb', line 151

def camel_case_lower(operand)
	elements = operand.split('_')
	camel_cased = elements.join('_').camelize(:lower)

	camel_cased.gsub!(/Id/, 'ID') if elements.size > 1 && elements.last =~ /id=?\Z/

	camel_cased
end

#serializeObject



228
229
230
# File 'lib/bill_forward/billing_entity.rb', line 228

def serialize
	to_json
end

#serialize_field(field) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/bill_forward/billing_entity.rb', line 188

def serialize_field(field)
	if field.is_a? BillingEntity
		serialize_field field.state_params
	elsif field.is_a? Array
		# return a transformed array, where each field has been serialized
		field.map do |iterand|
			serialize_field iterand
		end
	elsif field.is_a? Hash
		# the lengths I have to go to achieve immutability in Ruby 1.8.7
		clone = hash_with_type_at_top field
		clone.each do |key, value|
			clone[key] = serialize_field value
		end
		clone
	else
		field
	end
end

#state_paramsObject



184
185
186
# File 'lib/bill_forward/billing_entity.rb', line 184

def state_params
	@_state_params
end

#to_json(*a) ⇒ Object



214
215
216
# File 'lib/bill_forward/billing_entity.rb', line 214

def to_json(*a)
	to_ordered_hash.to_json
end

#to_ordered_hashObject Also known as: as_json



208
209
210
# File 'lib/bill_forward/billing_entity.rb', line 208

def to_ordered_hash
	serialize_field self
end

#to_sObject



223
224
225
226
# File 'lib/bill_forward/billing_entity.rb', line 223

def to_s
	parsed = to_unordered_hash
	JSON.pretty_generate(parsed)
end

#to_unordered_hashObject



218
219
220
221
# File 'lib/bill_forward/billing_entity.rb', line 218

def to_unordered_hash
	json_string = to_json.to_s
	JSON.parse(json_string)
end