Sha256: 9fe701631b5e2824f55a5a33f08fe5a1bbe9fcf7c6f1a43a0c94245d93b8f531

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

# 1. Create service

require 'net/http'

class OpenWeatherMapService
  attr_reader :url

  def initialize
    @url = 'http://api.openweathermap.org/data/2.5/weather'
  end

  def quote location, units
    quote_url = "#{url}?q=#{location}%20nj&units=#{units}"

    uri = URI.parse(URI.escape(quote_url))

    Net::HTTP.get(uri)
  end
end

# 2. Create service mock

require 'webmock_method'
require 'json'

class OpenWeatherMapService
  extend WebmockMethod

  webmock_method :quote, [:location, :units], lambda { |_|
    File.open "#{File.dirname(__FILE__)}/stubs/templates/quote_response.json.erb"
  }, /#{WebmockMethod.url}/
end

# 3. Test service mock

# Make sure we don't hit external service: when stub is commented, test should fail
WebMock.disable_net_connect!(allow_localhost: true)

describe OpenWeatherMapService do
  describe "#quote" do
    it "gets the quote" do
      result = JSON.parse(subject.quote("plainsboro, nj", "imperial"))

      expect(result['sys']['country']).to eq("United States of America")
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
webmock_method-1.2.1 spec/open_weather_map_service_spec.rb
webmock_method-1.2.0 spec/open_weather_map_service_spec.rb
webmock_method-1.1.1 spec/open_weather_map_service_spec.rb
webmock_method-1.1.0 spec/open_weather_map_service_spec.rb