Module: MotionModelResource::ApiWrapper::PublicClassMethods

Defined in:
lib/motion-model-resource/wrapper.rb

Instance Method Summary collapse

Instance Method Details

#build_model_with(json) ⇒ Object

Builds a model for given JSON object. Returns a new or presend model.



70
71
72
73
74
75
# 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

#buildModel(json) ⇒ Object



77
78
79
80
# 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

#fetch(site = nil, params = {}, &block) ⇒ Object

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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

#last_updateObject

Returns the last updated at or nil value of Model



13
14
15
16
# 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

#lastUpdateObject



18
19
20
21
# File 'lib/motion-model-resource/wrapper.rb', line 18

def lastUpdate
  NSLog "[DEPRECATED - lastUpdate] please use last_update instead."
  last_update
end

#save_model_with(json) ⇒ Object

Builds and update/create a model for given JSON object. Returns a new or presend model.



83
84
85
86
87
88
89
# 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

#update_models(json) ⇒ Object

Parses given JSON object and saves the founded models. Returns an array with models, or the founded model



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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

#updateModels(json) ⇒ Object



64
65
66
67
# File 'lib/motion-model-resource/wrapper.rb', line 64

def updateModels(json)
  NSLog "[DEPRECATED - updateModels] please use update_models instead."
  update_models json
end