Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenWeather API requires API Key #5

Open
yoshprogrammer opened this issue Sep 10, 2016 · 0 comments
Open

OpenWeather API requires API Key #5

yoshprogrammer opened this issue Sep 10, 2016 · 0 comments

Comments

@yoshprogrammer
Copy link

Hello,

First off, great book.

Second, OpenWeather's API changed so you need an API key now. Unfortunately, at least from my testing, the API does not return the same results as displayed in Chapter 6 making those sections/exercises difficult to understand what is supposed to be happening.

In chapter 7 it appears to work if I manually call cityTempe, but otherwise the code itself as written doesn't appear to execute. If I call cityTemp("Scottsdale") and hit save multiple times within the Eclipse IDE or command line, it will show up eventually every fifth or sixth time.

def cityTemp(name: String): Double = {
        val url = "http://api.openweathermap.org/data/2.5/weather?appid=[APIKEY]"
        val cityUrl = s"$url&q=$name"
        val json = io.Source.fromURL(cityUrl).mkString.trim
        val pattern = """.*"temp":([\d.]+).*""".r
        val pattern(temp) = json
        temp.toDouble
  }                                               //> cityTemp: (name: String)Double

    val cityTemps = Future sequence Seq(
            Future(cityTemp("Fresno")), Future(cityTemp("Tempe"))
    )                                         //> cityTemps  : scala.concurrent.Future[Seq[Double]] = List()

    cityTemps onSuccess {
        case Seq(x,y) if x > y => println(s"Fresno is warmer: $x K")
        case Seq(x,y) if x < y => println(s"Tempe is warmer: $y K")
    }

Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant