@@ -16,33 +16,43 @@ def street
16
16
end
17
17
18
18
def city
19
- context_part ( 'place' )
19
+ data_part ( 'place' ) || context_part ( 'place' )
20
20
end
21
21
22
22
def state
23
- context_part ( 'region' )
23
+ data_part ( 'region' ) || context_part ( 'region' )
24
24
end
25
25
26
26
def state_code
27
- value = context_part ( 'region' , 'short_code' )
27
+ if id_matches_name? ( data [ 'id' ] , 'region' )
28
+ value = data [ 'properties' ] [ 'short_code' ]
29
+ else
30
+ value = context_part ( 'region' , 'short_code' )
31
+ end
32
+
28
33
value . split ( '-' ) . last unless value . nil?
29
34
end
30
35
31
36
def postal_code
32
- context_part ( 'postcode' )
37
+ data_part ( 'postcode' ) || context_part ( 'postcode' )
33
38
end
34
39
35
40
def country
36
- context_part ( 'country' )
41
+ data_part ( 'country' ) || context_part ( 'country' )
37
42
end
38
43
39
44
def country_code
40
- value = context_part ( 'country' , 'short_code' )
45
+ if id_matches_name? ( data [ 'id' ] , 'country' )
46
+ value = data [ 'properties' ] [ 'short_code' ]
47
+ else
48
+ value = context_part ( 'country' , 'short_code' )
49
+ end
50
+
41
51
value . upcase unless value . nil?
42
52
end
43
53
44
54
def neighborhood
45
- context_part ( 'neighborhood' )
55
+ data_part ( 'neighborhood' ) || context_part ( 'neighborhood' )
46
56
end
47
57
48
58
def address
@@ -51,8 +61,16 @@ def address
51
61
52
62
private
53
63
64
+ def id_matches_name? ( id , name )
65
+ id =~ Regexp . new ( name )
66
+ end
67
+
68
+ def data_part ( name )
69
+ data [ 'text' ] if id_matches_name? ( data [ 'id' ] , name )
70
+ end
71
+
54
72
def context_part ( name , key = 'text' )
55
- ( context . detect { |c | c [ 'id' ] =~ Regexp . new ( name ) } || { } ) [ key ]
73
+ ( context . detect { |c | id_matches_name? ( c [ 'id' ] , name ) } || { } ) [ key ]
56
74
end
57
75
58
76
def context
0 commit comments