Module: StarRezApi::ClassMethods

Defined in:
lib/starrez_api/star_rez_api.rb

Instance Method Summary collapse

Instance Method Details

#all(page_index = 1, page_size = 50) ⇒ Object



131
132
133
# File 'lib/starrez_api/star_rez_api.rb', line 131

def all(page_index = 1, page_size = 50)
  return find(:all, {:size => page_size, :page => page_index})
end

#build_query(hash) ⇒ Object



281
282
283
284
285
286
287
# File 'lib/starrez_api/star_rez_api.rb', line 281

def build_query(hash)
  query = Hash.new
  hash.keys.each do |attribute|
    query[net_camelize(attribute.to_s).to_sym] = self.send(attribute)
  end
  return query
end

#changedObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/starrez_api/star_rez_api.rb', line 70

def changed
  changed_attributes = Hash.new
  self.instance_variable_get("@original_hash").each do |k,v|
    k = k.to_s.gsub(/_/,'__').underscore
    current_value = self.send(k.to_sym)
    unless current_value.eql? v
      changed_attributes[k.to_sym] = [v, current_value]
    end
  end
  return changed_attributes
end

#changed?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/starrez_api/star_rez_api.rb', line 82

def changed?
  self.changed.size > 0
end

#class_nameObject



26
27
28
# File 'lib/starrez_api/star_rez_api.rb', line 26

def class_name
  self.name.gsub(/.*\:\:/,'')
end

#create(attribute_hash = {}, options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/starrez_api/star_rez_api.rb', line 105

def create(attribute_hash = {}, options ={})
  formatted_hash = Hash.new
  attribute_hash.each_pair { |column, value| formatted_hash[net_camelize(column.to_s).to_sym] = value }
  results = StarRezApi::post("#{StarRezApi::base_uri}/create/#{self.class_name}.xml", :body => formatted_hash)
  if results.code.eql? 200
    if options[:return].eql? :boolean
      return true
    elsif options[:return].eql? :response
      return results
    else
      response = XmlSimple.xml_in(results.body)
      new_id = response["#{self.class_name}ID"].first
      return self.find new_id
    end
  elsif results.code.eql? 400
    return false
  else
    if options[:return].eql? :response
      return results
    else
      return false
    end
  end
end

#find(entry, options = {}) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/starrez_api/star_rez_api.rb', line 209

def find(entry, options = {})
  options[:size] ||= 50
  options[:page] = options[:page].blank? || options[:page] == 1 ? 0 : options[:page] * options[:size]
  query_array = Array.new
  unless options[:conditions].blank?
    query_array << get_condition_string(options[:conditions]) 
  end
  if entry.is_a?(Symbol)
    get_url = "#{StarRezApi::base_uri}/select/#{self.class_name}.xml/"
  else
    get_url = "#{StarRezApi::base_uri}/select/#{self.class_name}.xml/#{entry}"
  end
  if entry.eql? :first        
    query_array << "_top=1"
  elsif entry.eql? :all
    query_array << "_pageIndex=#{options[:page]}"
    query_array << "_pageSize=#{options[:size]}"  
  end
  unless options[:fields].blank?
    fields = Array.new
    options[:fields].each { |f| fields << net_camelize(f.to_s) }
    query_array << "_fields=#{fields.join(',')}"
  end
  tables = Array.new
  unless options[:include].blank?
    options[:include].each { |t| tables << net_camelize(t.to_s)}
    query_array << "_relatedtables=#{tables.join(',')}"
  end
  unless options[:order].blank?
    order = Array.new
    options[:order].each { |o| order << (options[:order][o].eql? :desc) ? "#{net_camelize(o.to_s)}.desc" : "#{net_camelize(o.to_s)}"}
    query_array << "_orderby=#{order.join(',')}"
  end
  unless options[:limit].blank?
    query_array << "_top=#{options[:limit]}"
  end
  get_url += "?#{query_array.join('&')}"
  results = StarRezApi::get(get_url)
  if results.response.is_a?(Net::HTTPNotFound)
    return nil
  elsif results.code.eql? 403
    if options[:debug]
      return results
    else
      raise SecurityError, "Access Denied to API"
    end
  elsif options[:return].eql? :response
    return results
  elsif results.code.eql? 200
    ret = results["Results"][self.class_name]
  else
    return results
  end
  if ret.is_a?(Hash)
    self.populate_variables(ret,tables)
    if entry.eql? :all
      return [self]
    else        
      return self
    end
  else
    results = Array.new
    ret.each do |entry_hash|
      new_entry = self.clone
      new_entry.populate_variables(entry_hash,tables)
      results << new_entry
    end
    return results
  end
end

#firstObject



14
15
16
# File 'lib/starrez_api/star_rez_api.rb', line 14

def first
  results = find(:first)
end

#idObject



66
67
68
# File 'lib/starrez_api/star_rez_api.rb', line 66

def id
  self.send "#{self.class_name.underscore}_id"
end

#populate_variables(hash, related_tables = []) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/starrez_api/star_rez_api.rb', line 30

def populate_variables(hash, related_tables=[])
  self.instance_variable_set("@original_hash",hash)
  hash.each do |k,v|
    if related_tables.include? k.to_s
      children = Array.new
      v = [v] unless v.is_a? Array # This handles the instance of a single-child
      v.each do |child|
        new_child = self.clone
        new_child.populate_variables(child)
        children << new_child
      end
      new_k = k.to_s.gsub(/_/,'__').underscore.pluralize
      self.instance_variable_set("@#{new_k}", children)
      meta_def new_k do
        self.instance_variable_get("@#{new_k}")
      end
      meta_def "#{new_k}=" do |v|
        self.instance_variable_set("@#{new_k}",v)
      end
    elsif k.is_a?(Hash)
      # Ignore sub-objects
    else
      unless k.blank?
        k = k.to_s.gsub(/_/,'__').underscore
        self.instance_variable_set("@#{k}",v)
        meta_def k do
          self.instance_variable_get("@#{k}")
        end
        meta_def "#{k}=" do |v|
          self.instance_variable_set("@#{k}",v)
        end
      end
    end
  end
end

#saveObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/starrez_api/star_rez_api.rb', line 86

def save
  if self.changed?
    response = StarRezApi::post("#{StarRezApi::base_uri}/update/#{self.class_name}/#{self.id}", :body => self.build_query(self.changed))
    if response.code.eql? 200
      original_hash = self.instance_variable_get("@original_hash")
      self.build_query(self.changed).keys.each do |attribute|
        original_hash[attribute.to_s] = self.send(attribute.to_s.gsub(/_/,'__').underscore.to_sym)
      end
      self.instance_variable_set("@original_hash",original_hash)
      return true
    else
      return response
    end
  else
    warn "[WARN] Nothing to save"
    return false
  end
end

#urlObject



18
19
20
# File 'lib/starrez_api/star_rez_api.rb', line 18

def url
  "#{StarRezApi::base_uri}/select/#{self.name.gsub(/.*\:\:/,'').downcase}.xml/?_top=1"
end

#what_am_i?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/starrez_api/star_rez_api.rb', line 22

def what_am_i?
  "#{self.class.name}"
end