Module: Zanox::Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id.



142
143
144
# File 'lib/zanox.rb', line 142

def id
  @id
end

Instance Method Details

#find(*args) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/zanox.rb', line 266

def find(*args)
  item_name = self.name.split('::').last
  options = args.last.is_a?(Hash) ? args.pop : {}
  
  puts "Arguments: " + args.inspect if $DEBUG
  puts "Options: " + options.inspect if $DEBUG
  
  if(item_name=="Profile")
    find_other(args, options)
  else
    case args.first
      when :all then find_every(options)
      when nil  then find_every(options)
      else           find_other(args, options)
    end
  end
  
end

#find_every(options) ⇒ 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/zanox.rb', line 150

def find_every(options)
  items = []
  class_name = self.name.split('::').last
  
  if(self.respond_to?(:pluralize))
    api_method = 'get'+self.pluralize
  end
  
  if(class_name=='Program' && options.has_key?(:adspaceId))
    api_method = "getProgramsByAdspace"
  end
  
  response = Zanox::API.request(api_method, options)
  
  class_name.sub!(/\b\w/) { $&.downcase }
  
  m1_name = (class_name+'Items').to_sym
  m2_name = (class_name+'Item').to_sym
  
  if(response.respond_to?(m1_name) && response.items!=0)
    items_object = response.method(m1_name).call
    if(items_object.respond_to?(m2_name))
      item_object = items_object.method(m2_name).call
      if(item_object.is_a?(Array))
        item_list = item_object
      else
        item_list = [item_object]
      end
      [*item_object].each do |item|
        items.push self.new(item)
      end
    end
  else
    # found nothing handling
    #return items
  end
  
  return items
  
end

#find_other(args, options) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
# File 'lib/zanox.rb', line 191

def find_other(args, options)
  ids = []
  queries = []
  items = []
  args.each do |arg|
    self.is_key?(arg) ? ids.push(arg) : queries.push(arg)
  end

  puts "found ids: "+ids.to_s if $DEBUG
  puts "found query strings: "+queries.to_s if $DEBUG

  class_name = self.name.split('::').last
  api_method = ''

  if(ids.size==0 && queries.size==0)
    api_method = 'get'+class_name
    unless Zanox::API.secret_key.nil?
      response = Zanox::API.request(api_method, options)
      class_name.sub!(/\b\w/) { $&.downcase }
      item_method = (class_name+'Item').to_sym
      if(response.respond_to?(item_method))
        item = self.new(response.method(item_method).call)
        items.push item
      end
    end
    
  end

  if(ids.size>0)
    
    api_method = 'get'+class_name
    
    ids.each do |id|
      options.merge!(self.key_symbol=>id)
      response = Zanox::API.request(api_method, options)
      
      class_name.sub!(/\b\w/) { $&.downcase }
      item_method = (class_name+'Item').to_sym
      
      if(response.respond_to?(item_method))
        item = self.new(response.method(item_method).call)
        items.push item
      end
    end
  end

  if(queries.size>0)
    api_method = 'search'+self.pluralize
    class_name.sub!(/\b\w/) { $&.downcase }
    queries.each do |query|
      options.merge!(:query=>query)
      response = Zanox::API.request(api_method, options)
      m1_name = (class_name+'Items').to_sym
      m2_name = (class_name+'Item').to_sym

      if(response.respond_to?(m1_name))
        items_object = response.method(m1_name).call
        if(items_object.respond_to?(m2_name))
          item_object = items_object.method(m2_name).call
          if(item_object.is_a?(Array))
            item_list = item_object
          else
            item_list = [item_object]
          end
          [*item_object].each do |item|
            items.push self.new(item)
          end
        end
      end
    end
  end
  
  return items
end

#new(item = nil) ⇒ Object



144
145
146
147
148
# File 'lib/zanox.rb', line 144

def new(item=nil)
  item.extend(self)
  item.id= item.xmlattr_id
  return item
end