Sha256: 5c3c4cb16acd3debda9ca9c432786df09ce7dd75a53a699fd0095d9a01b1c464

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

describe "Client" do
  let(:client) { GmapsTz::Client.new(:key => "my_key") }

  class FakeResponse
    attr_accessor :body

    def initialize(body)
      self.body = body
    end
  end

  describe ".time_zone_in" do
    let(:mocked_response_ok) do
      body = {
        "dstOffset" => 28880,
        "rawOffset" => 28880,
        "timeZoneId" => "America/Los_Angeles",
        "timeZoneName" => "Pacific Daylight Time",
        "status" => "OK"
      }.to_json
      FakeResponse.new(body)
    end

    let(:mocked_response_over_limit) do
      body = { "status" => "OVER_QUERY_LIMIT" }.to_json
      FakeResponse.new(body)
    end

    let(:time) { Time.parse("18-10-1988") }

    it "pings the right API url" do
      Net::HTTP.should_receive(:get_response)
        .with(URI("https://maps.googleapis.com/maps/api/timezone/json?sensor=false&key=my_key&location=1.0%2C2.0&timestamp=#{time.to_i}"))
        .and_return(mocked_response_ok)
      client.time_zone_in("1.0", "2.0", time)
    end

    it "doesn't append key if not set" do
      client = GmapsTz::Client.new()
      Net::HTTP.should_receive(:get_response)
        .with(URI("https://maps.googleapis.com/maps/api/timezone/json?sensor=false&location=1.0%2C2.0&timestamp=#{time.to_i}"))
        .and_return(mocked_response_ok)
      client.time_zone_in("1.0", "2.0", time)
    end

    it "returns a hash with the result when success" do
      Net::HTTP.stub(:get_response).and_return(mocked_response_ok)
      response = client.time_zone_in("1.0", "2.0", time)
      response.should == mocked_response_ok
    end

    it "raises the corresponding exception when it fails" do
      Net::HTTP.stub(:get_response).and_return(mocked_response_over_limit)
      expect {
        client.time_zone_in("1.0", "2.0", time)
      }.to raise_error(GmapsTz::OverQueryLimitError)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gmaps_tz-0.0.1 spec/client_spec.rb