lolcation_client 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fca6dfbcc45730ced54f283c15c7f96fce7e47aa
4
- data.tar.gz: 5d0c0f2e0ad8e3fc6fc8675c62b964dd3173a0fd
3
+ metadata.gz: db6234d327df630f577bd8197571cc9d82bbf68f
4
+ data.tar.gz: 1cb4d0b5bea591cccf8a46d1130882f53bda1293
5
5
  SHA512:
6
- metadata.gz: 8cf99f41462384bfbb01da1304af23cb3ef6e4e1d686d2d0a6e74d52a6651a38de60ca83048652f79a13e15fec75d7a37761a4d25f2f0ced2e52770c914ac042
7
- data.tar.gz: 1c5890719812651813ba61a5f22a24a6191a17114ca1b4fec137e97c0cedeafcfcdd100e844d48a06cb490edb968b35bd4ab39cd55dd4e39602c050ae0fd4306
6
+ metadata.gz: 2ffbf03b2f04f554a9b5636f78f39317afe258e3bb736c643e2eea058a6414008f7c863e9bbdee02eca3777ba752c5ed7a1a932ace45eb3301fa217125e2527b
7
+ data.tar.gz: 25428aae5bdd403434efd1b653c18abeed5c8c686a7acaaf05cb1d0619bb4ccf6e6a5226e43eb5f9ee3699204aae22e82af7e982bf57ef414d9a51ce75e6bac9
@@ -10,6 +10,7 @@ class AddLolcationTo<%= table_name.camelize %> < ActiveRecord::Migration
10
10
  t.string :lolcation_address_city
11
11
  t.string :lolcation_address_state
12
12
  t.string :lolcation_address_number
13
+ t.string :lolcation_address_zipcode
13
14
  end
14
15
  end
15
16
  end
@@ -7,22 +7,23 @@ module LolcationClient
7
7
 
8
8
  def self.included(model)
9
9
  model.send(:after_validation) do
10
- if self.lolcation_id.present?
11
- response = patch_on_lolcation_server
10
+ if self.persisted?
11
+ response = update_on_lolcation_server
12
12
  else
13
- response = post_on_lolcation_server
13
+ response = create_on_lolcation_server
14
14
  end
15
15
 
16
- parsed_response = JSON.parse(response.body)
17
- if parsed_response["error"]
18
- self.errors.add(:base, parsed_response["error"])
16
+ json = JSON.parse(response.body, object_class: OpenStruct)
17
+
18
+ if json.error.present?
19
+ self.errors.add(:base, json.error)
19
20
  false
20
- elsif parsed_response['localization']
21
- self.lolcation_id = parsed_response["localization"]["objectId"]
22
- self.lolcation_latitude = parsed_response["localization"]["latitude"]
23
- self.lolcation_longitude = parsed_response["localization"]["longitude"]
21
+ elsif json.localization.present?
22
+ self.lolcation_id = json.localization.id
23
+ self.lolcation_latitude = json.localization.latitude
24
+ self.lolcation_longitude = json.localization.longitude
24
25
  else
25
- parsed_response.map {|error, message| self.errors.add("lolcation_#{error}", message[0])}
26
+ json.map {|error, message| self.errors.add("lolcation_#{error}", message[0])}
26
27
  false
27
28
  end
28
29
  end
@@ -34,9 +35,10 @@ module LolcationClient
34
35
  self.class.try(:lolcation_custom_fields) || []
35
36
  end
36
37
 
37
- def set_custom_fields
38
+ def build_custom_fields
38
39
  attributes = custom_fields.map {|attribute| {attribute => self.try(attribute)}}
39
- hash = {}
40
+ hash = {}
41
+
40
42
  attributes.each do |attribute|
41
43
  attribute.each do |key, value|
42
44
  hash[key] = value
@@ -46,40 +48,41 @@ module LolcationClient
46
48
  hash
47
49
  end
48
50
 
49
- def prepare_object_to_post
51
+ def build_params
50
52
  {
51
53
  localization: {
52
- latitude: self.try(:lolcation_latitude),
53
- longitude: self.try(:lolcation_longitude),
54
- name: self.try(:lolcation_name),
55
- address_street: self.try(:lolcation_address_street),
54
+ latitude: self.try(:lolcation_latitude),
55
+ longitude: self.try(:lolcation_longitude),
56
+ name: self.try(:lolcation_name),
57
+ address_street: self.try(:lolcation_address_street),
56
58
  address_neighborhood: self.try(:lolcation_address_neighborhood),
57
- address_city: self.try(:lolcation_address_city),
58
- address_state: self.try(:lolcation_address_state),
59
- address_number: self.try(:lolcation_address_number),
60
- custom_fields: set_custom_fields
59
+ address_city: self.try(:lolcation_address_city),
60
+ address_state: self.try(:lolcation_address_state),
61
+ address_number: self.try(:lolcation_address_number),
62
+ address_zipcode: self.try(:lolcation_address_zipcode),
63
+ #custom_fields: build_custom_fields
61
64
  },
62
65
  sandbox: sandbox?
63
- }
66
+ }.to_json
64
67
  end
65
68
 
66
- def post_on_lolcation_server
69
+ def create_on_lolcation_server
67
70
  url = LolcationClient::Configurations::URL
68
71
  conn = Faraday.new(url: url)
69
72
  conn.post do |r|
70
73
  r.headers["X-Token"] = token
71
74
  r.headers["Content-Type"] = "application/json"
72
- r.body = prepare_object_to_post.to_json
75
+ r.body = build_params
73
76
  end
74
77
  end
75
78
 
76
- def patch_on_lolcation_server
79
+ def update_on_lolcation_server
77
80
  url = LolcationClient::Configurations::URL
78
81
  conn = Faraday.new(url: "#{url}/#{self.lolcation_id}")
79
82
  conn.put do |r|
80
83
  r.headers["X-Token"] = token
81
84
  r.headers["Content-Type"] = "application/json"
82
- r.body = prepare_object_to_post.to_json
85
+ r.body = build_params
83
86
  end
84
87
  end
85
88
  end
@@ -3,8 +3,16 @@ module LolcationClient
3
3
  def lolcation_fields(options = {})
4
4
  self.class_eval do
5
5
  before_validation do
6
- fields = [:latitude, :longitude, :name, :address_street, :address_neighborhood,
7
- :address_city, :address_state, :address_number]
6
+ fields = [
7
+ :latitude,
8
+ :longitude,
9
+ :name,
10
+ :address_street,
11
+ :address_neighborhood,:address_city,
12
+ :address_state,
13
+ :address_number,
14
+ :address_zipcode,
15
+ ]
8
16
 
9
17
  fields.each do |field|
10
18
  self.send("lolcation_#{field}=", self.send(options[field] || "lolcation_#{field}"))
@@ -6,12 +6,12 @@ module LolcationClient
6
6
  include LolcationClient::Configurations
7
7
 
8
8
  def near_in(options = {})
9
- raise ArgumentError, 'You must pass latitude and longitude as params' unless options[:latitude] || options[:longitude]
9
+ raise ArgumentError, 'Latitude and Longitude is required' unless options[:latitude].present? || options[:longitude].present?
10
10
 
11
- parse_response(grab_objects(options), options)
11
+ process(do_post(options), options)
12
12
  end
13
13
 
14
- def parse_response(response, options)
14
+ def process(response, options)
15
15
  json = JSON.parse(response.body, object_class: OpenStruct)
16
16
 
17
17
  list = json['localizations']
@@ -23,7 +23,7 @@ module LolcationClient
23
23
 
24
24
  private
25
25
 
26
- def grab_objects(options = {})
26
+ def do_post(options = {})
27
27
  url = LolcationClient::Configurations::URL
28
28
  conn = Faraday.new(url: "#{url}/near-me")
29
29
  conn.post do |r|
@@ -1,3 +1,3 @@
1
1
  module LolcationClient
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lolcation_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Zaghi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-07-14 00:00:00.000000000 Z
12
+ date: 2017-08-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler