module MotionModelResource::ApiWrapper::PublicClassMethods
Public Instance Methods
buildModel(json)
click to toggle source
# File lib/motion-model-resource/wrapper.rb, line 77 def buildModel(json) NSLog "[DEPRECATED - buildModel] please use build_model_with instead." build_model_with json end
build_model_with(json)
click to toggle source
Builds a model for given JSON object. Returns a new or presend model.
# File lib/motion-model-resource/wrapper.rb, line 70 def build_model_with(json) return nil if json.is_a?(Array) model = where("id").eq(json["id"]).first || self.new model.wrap(json) end
fetch(site = nil, params = {}, &block)
click to toggle source
Loads the given URL and parse the JSON for new models. If the models are present, the model will update. If block given, the block will called, when the the models are saved. The model/s will be passed as an argument to the block.
# File lib/motion-model-resource/wrapper.rb, line 26 def fetch(site = nil, params = {}, &block) raise MotionModelResource::WrapperNotDefinedError.new "Wrapper is not defined!" unless self.respond_to?(:wrapper) raise MotionModelResource::URLNotDefinedError.new "Resource URL ist not defined! (#{name}.url)" if site.blank? && self.try(:url).blank? site = self.url if site.blank? BW::HTTP.get(site, params) do |response| models = [] if response.ok? && response.body.present? begin json = BW::JSON.parse(response.body.to_str) models = update_models(json) rescue BW::JSON::ParserError end end block.call(models) if block.present? && block.respond_to?(:call) end end
lastUpdate()
click to toggle source
# File lib/motion-model-resource/wrapper.rb, line 18 def lastUpdate NSLog "[DEPRECATED - lastUpdate] please use last_update instead." last_update end
last_update()
click to toggle source
Returns the last updated at or nil value of Model
# File lib/motion-model-resource/wrapper.rb, line 13 def last_update return unless columns.include? :updated_at order{|one, two| two.updated_at <=> one.updated_at}.first.try(:updated_at) end
save_model_with(json)
click to toggle source
Builds and update/create a model for given JSON object. Returns a new or presend model.
# File lib/motion-model-resource/wrapper.rb, line 83 def save_model_with(json) return nil if json.is_a?(Array) model = build_model_with(json) model.try :save model end
updateModels(json)
click to toggle source
# File lib/motion-model-resource/wrapper.rb, line 64 def updateModels(json) NSLog "[DEPRECATED - updateModels] please use update_models instead." update_models json end
update_models(json)
click to toggle source
Parses given JSON object and saves the founded models. Returns an array with models, or the founded model
# File lib/motion-model-resource/wrapper.rb, line 48 def update_models(json) if json.is_a?(Array) model_ids = [] for json_part in json model = save_model_with(json_part) model_ids << "#{model.id}".to_i if model.present? end where(:id).in model_ids else model = save_model_with(json) return nil if model.blank? find("#{model.id}".to_i) end end