master
1require "rails_helper"
2
3describe Location do
4 describe ".from" do
5 it "returns the correct lat/long" do
6 VCR.use_cassette("geo-location-from-address") do
7 latitude, longitude = Location.
8 from("1301 16 Ave NW", "Calgary", "AB", "Canada")
9
10 expect(latitude).to be_within(0.1).of(51.0647815)
11 expect(longitude).to be_within(0.1).of(-114.0927691)
12 end
13 end
14 end
15
16 describe "#before_save" do
17 let(:latitude) { rand(90.0) }
18 let(:longitude) { rand(180.0) }
19
20 it "assigns a lat/long" do
21 allow(Location).to receive(:from).and_return([latitude, longitude])
22
23 location = Location.new(
24 address: "123 street sw",
25 city: "edmonton",
26 region: "alberta",
27 country: "canada",
28 )
29 location.save!
30
31 expect(location.latitude).to eql(latitude)
32 expect(location.longitude).to eql(longitude)
33 end
34 end
35
36 describe ".build_from_ip" do
37 it "returns a location from the ip address" do
38 VCR.use_cassette("geo-location-70.173.137.232") do
39 result = Location.build_from_ip("70.173.137.232")
40 expect(result).to be_instance_of(Location)
41 expect(result.address).to include("Las Vegas")
42 expect(result.city).to eql("Las Vegas")
43 expect(result.region).to eql("NV")
44 expect(result.country).to eql("US")
45 expect(result.postal_code).to start_with("8910")
46 expect(result.latitude).to be_within(0.2).of(36.1)
47 expect(result.longitude).to be_within(0.2).of(-115.1)
48 end
49 end
50
51 it "returns a location from the ip address" do
52 VCR.use_cassette("geo-location-127.0.0.1") do
53 result = Location.build_from_ip("127.0.0.1")
54 expect(result).to be_instance_of(Location)
55 end
56 end
57 end
58
59 describe "#coordinates" do
60 it "returns the lat/long" do
61 subject.latitude = rand(90.0)
62 subject.longitude = rand(180.0)
63 expect(subject.coordinates).to match_array([
64 subject.latitude,
65 subject.longitude
66 ])
67 end
68
69 it "returns an empty array" do
70 VCR.use_cassette("geo-location-build_from_ip.172.18.0.1") do
71 subject = Location.build_from_ip("172.18.0.1")
72 expect(subject.coordinates).to be_empty
73 end
74 end
75 end
76end