module LolcationClient::Interceptor
Public Class Methods
included(model)
click to toggle source
# File lib/lolcation_client/interceptor.rb, line 8 def self.included(model) model.send(:after_validation) do self.errors.add("lolcation_name", :blank) unless self.lolcation_name.present? if self.lolcation_address_street.present? self.errors.add("lolcation_address_neighborhood", :blank) unless self.lolcation_address_neighborhood.present? self.errors.add("lolcation_address_city" , :blank) unless self.lolcation_address_city.present? self.errors.add("lolcation_address_state" , :blank) unless self.lolcation_address_state.present? self.errors.add("lolcation_address_number" , :blank) unless self.lolcation_address_number.present? self.errors.add("lolcation_address_zipcode" , :blank) unless self.lolcation_address_zipcode.present? else self.errors.add("lolcation_latitude" , :blank) unless self.lolcation_latitude.present? self.errors.add("lolcation_longitude", :blank) unless self.lolcation_longitude.present? end end model.send(:after_save) 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.update_column(:lolcation_id , json.id) self.update_column(:lolcation_latitude , json.latitude) self.update_column(:lolcation_longitude, json.longitude) else json.errors.collect{|error| self.errors.add("lolcation_#{error.field}", error.message)} false end end end
Private Instance Methods
build_filter_fields()
click to toggle source
# File lib/lolcation_client/interceptor.rb, line 51 def build_filter_fields attributes = custom_fields.map {|attribute| {attribute => self.try(attribute)}} hash = {} attributes.each do |attribute| attribute.each do |key, value| hash[key] = value end end hash end
build_params()
click to toggle source
# File lib/lolcation_client/interceptor.rb, line 64 def build_params { localization: { latitude: self.try(:lolcation_latitude), longitude: self.try(:lolcation_longitude), name: self.try(:lolcation_name), address_street: self.try(:lolcation_address_street), address_neighborhood: self.try(:lolcation_address_neighborhood), address_city: self.try(:lolcation_address_city), address_state: self.try(:lolcation_address_state), address_number: self.try(:lolcation_address_number), address_zipcode: self.try(:lolcation_address_zipcode), filters: build_filter_fields, sandbox: sandbox? } }.to_json end
create_on_lolcation_server()
click to toggle source
# File lib/lolcation_client/interceptor.rb, line 82 def create_on_lolcation_server conn = Faraday.new(url: service_url) conn.post do |r| r.headers["X-Token"] = token r.headers["Content-Type"] = "application/json" r.body = build_params end end
custom_fields()
click to toggle source
# File lib/lolcation_client/interceptor.rb, line 47 def custom_fields self.class.try(:lolcation_custom_fields) || [] end
update_on_lolcation_server()
click to toggle source
# File lib/lolcation_client/interceptor.rb, line 91 def update_on_lolcation_server conn = Faraday.new(url: "#{service_url}/#{self.lolcation_id}") conn.put do |r| r.headers["X-Token"] = token r.headers["Content-Type"] = "application/json" r.body = build_params end end