Skip to content

Commit

Permalink
Some changes. Removed "video" key
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriozambon committed May 3, 2013
1 parent 80cfbdc commit 3886e5a
Show file tree
Hide file tree
Showing 4 changed files with 1,203 additions and 413 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
clima_tempo (2.0)
clima_tempo (2.2)
nokogiri (>= 1.5.9)

GEM
Expand Down
36 changes: 17 additions & 19 deletions lib/clima_tempo.rb
Expand Up @@ -5,44 +5,42 @@
class ClimaTempo
attr_reader :code

def initialize(option = {})
raise ArgumentError if option.empty?
def initialize(option)
raise TypeError unless option.kind_of? Hash
raise ArgumentError unless option.has_key? :code

@code = option[:code]
end

def now
html = request
page = request

values = html.xpath "//li[@class='dados-momento-li list-style-none']"
region = html.xpath "//a[@class='thumb-play-prev']"

now = {
:temperature => html.xpath("//span[@class='left temp-momento top10']").text,
:wind => wind_direction[prepare(values[0].text)],
:condition => prepare(values[1].text),
:pressure => prepare(values[2].text),
:intensity => prepare(values[3].text),
:moisture => prepare(values[4].text)
{
:temperature => page[:temperature].text,
:wind => wind[prepare(page[:data][0].text)],
:condition => prepare(page[:data][1].text),
:pressure => prepare(page[:data][2].text),
:intensity => prepare(page[:data][3].text),
:moisture => prepare(page[:data][4].text)
}

now.merge! :video => "http://www.climatempo.com.br#{region.first.attribute('href').value}" unless region.first.nil?

now
end

private
def request
request = Net::HTTP.get URI.parse("http://www.climatempo.com.br/previsao-do-tempo/cidade/#{@code}/empty")
request = Nokogiri::HTML request

Nokogiri::HTML request
{
:temperature => request.xpath("//span[@class='left temp-momento top10']"),
:data => request.xpath("//li[@class='dados-momento-li list-style-none']")
}
end

def prepare(value)
value.gsub! /^.+:\s*/, ""
end

def wind_direction
def wind
{
"N" => "Norte",
"S" => "Sul",
Expand Down
35 changes: 19 additions & 16 deletions spec/clima_tempo_spec.rb
@@ -1,46 +1,49 @@
require "spec_helper"

describe ClimaTempo do
it "missing type" do
expect { climatempo = ClimaTempo.new(:code) }.to raise_error(TypeError)
end

it "missing parameters" do
expect { climatempo = ClimaTempo.new }.to raise_error(ArgumentError)
expect { climatempo = ClimaTempo.new({}) }.to raise_error(ArgumentError)
end

context "values for the time" do
context "values from São Paulo" do
before do
fixture = File.open("spec/fixture/sao_paulo.html").read
FakeWeb.register_uri :any, "http://www.climatempo.com.br/previsao-do-tempo/cidade/558/empty", :body => fixture

@climatempo = ClimaTempo.new :code => 558
@clima_tempo = ClimaTempo.new :code => 558
end

it "object type" do
@climatempo.now.should be_an(Hash)
@clima_tempo.now.should be_an(Hash)
end

it "keys" do
hash = @climatempo.now
it "object keys" do
hash = @clima_tempo.now

hash.should have(7).items
hash.should have(6).items

hash.should have_key(:temperature)
hash.should have_key(:wind)
hash.should have_key(:condition)
hash.should have_key(:pressure)
hash.should have_key(:intensity)
hash.should have_key(:moisture)
hash.should have_key(:video)
end

it "values" do
hash = @climatempo.now
it "object values" do
hash = @clima_tempo.now

hash[:temperature].should == "24ºC"
hash[:wind].should == "Sudeste"
hash[:condition].should == "Muitas nuvens"
hash[:pressure].should == "1021 hPa"
hash[:intensity].should == "19 Km/h"
hash[:moisture].should == "57%"
hash[:video].should include("grande-sp")
hash[:temperature].should == "28ºC"
hash[:wind].should == "Norte"
hash[:condition].should == "Tempo firme"
hash[:pressure].should == "1017 hPa"
hash[:intensity].should == "11 Km/h"
hash[:moisture].should == "51%"
end
end
end

0 comments on commit 3886e5a

Please sign in to comment.