Module: LolcationClient::Interceptor

Includes:
Configurations
Defined in:
lib/lolcation_client/interceptor.rb

Class Method Summary collapse

Methods included from Configurations

#configs, #sandbox?, #service_url, #token

Class Method Details

.included(model) ⇒ Object



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

def self.included(model)
  model.send(:after_validation) do
    if self.lolcation_id.present?
      response = update_on_lolcation_server
    else
      response = create_on_lolcation_server
    end

    json = JSON.parse(response.body, object_class: OpenStruct)

    if json.success
      self.lolcation_id = json.id
      self.lolcation_latitude = json.latitude
      self.lolcation_longitude = json.longitude
      true
    else
      self.errors.add(:base, json.error)
      false
    end
  end
end