summaryrefslogtreecommitdiff
path: root/spec/models/location_spec.rb
blob: 8b162c46d20a1d5b263f294af83e6f7ae90c3108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require "rails_helper"

describe Location do
  describe ".from" do
    it "returns the correct lat/long" do
      VCR.use_cassette("geo-location-from-address") do
        latitude, longitude = Location.
          from("1301 16 Ave NW", "Calgary", "AB", "Canada")

        expect(latitude).to be_within(0.1).of(51.0647815)
        expect(longitude).to be_within(0.1).of(-114.0927691)
      end
    end
  end

  describe "#before_save" do
    let(:latitude) { rand(90.0) }
    let(:longitude) { rand(180.0) }

    it "assigns a lat/long" do
      allow(Location).to receive(:from).and_return([latitude, longitude])

      location = Location.new(
        address: "123 street sw",
        city: "edmonton",
        region: "alberta",
        country: "canada",
      )
      location.save!

      expect(location.latitude).to eql(latitude)
      expect(location.longitude).to eql(longitude)
    end
  end

  describe ".build_from_ip" do
    it "returns a location from the ip address" do
      VCR.use_cassette("geo-location-70.173.137.232") do
        result = Location.build_from_ip("70.173.137.232")
        expect(result).to be_instance_of(Location)
        expect(result.address).to include("Las Vegas")
        expect(result.city).to eql("Las Vegas")
        expect(result.region).to eql("NV")
        expect(result.country).to eql("US")
        expect(result.postal_code).to start_with("8910")
        expect(result.latitude).to be_within(0.2).of(36.1)
        expect(result.longitude).to be_within(0.2).of(-115.1)
      end
    end

    it "returns a location from the ip address" do
      VCR.use_cassette("geo-location-127.0.0.1") do
        result = Location.build_from_ip("127.0.0.1")
        expect(result).to be_instance_of(Location)
      end
    end
  end

  describe "#coordinates" do
    it "returns the lat/long" do
      subject.latitude = rand(90.0)
      subject.longitude = rand(180.0)
      expect(subject.coordinates).to match_array([
        subject.latitude,
        subject.longitude
      ])
    end

    it "returns an empty array" do
      VCR.use_cassette("geo-location-build_from_ip.172.18.0.1") do
        subject = Location.build_from_ip("172.18.0.1")
        expect(subject.coordinates).to be_empty
      end
    end
  end
end