Temparature =
AbsoluteZeroDegree: -273.15
City =
Tokyo:
Id: 1850147
TimeZone: '+09:00'
SlackUrl =
incoming:
bot: 'https://hooks.slack.com/services/THIS/IS/EXAMPLE'
class WeatherAPIGateway
instance = null
class _WeatherAPIGatewayCore
url_base = 'http://api.openweathermap.org/data/2.5/weather'
tmp_json = null
getWeatherByCityId: (id) ->
url = url_base + '?id=' + id
return @.getWeatherJSONByUrl(url)
getWeatherJSONByUrl: (url) ->
webGateway = WebGateway.getInstance()
jsonString = webGateway.getBody(url)
return new Weather(Utilities.jsonParse(jsonString))
getDetailUrlByCityId: (id) ->
return 'http://openweathermap.org/city/id?=' + id
@getInstance: () ->
instance ?= new _WeatherAPIGatewayCore()
class Weather
_json = null
constructor: (json) ->
_json = json
getImageUrl: () ->
return 'http://openweathermap.org/img/w/' +
_json.weather[0].icon + '.png'
getTempertureInCelcius: () ->
return _json.main.temp + Temparature.AbsoluteZeroDegree
getMinimumTemparetureInCelcius: () ->
minimumTemparature = _json.main.temp_min + Temparature.AbsoluteZeroDegree
minimumTemparature = Math.round(minimumTemparature * 100) / 100
return minimumTemparature
getMaximumTemparatureInCelcius: () ->
maximumTemparature = _json.main.temp_max + Temparature.AbsoluteZeroDegree
maximumTemparature = Math.round(maximumTemparature * 100) / 100
return maximumTemparature
getCityName: ->
return _json.name
getTitle: ->
return _json.weather[0].main
getDescription: ->
return _json.weather[0].description
getWindSpeed: ->
return _json.wind.speed
getUnixTime: ->
return _json.dt
class SlackPushMessage
_message = {}
setText: (text) ->
_message['text'] = text
return @
getText: ->
return _message['text']
setUserName: (user_name) ->
_message['username'] = user_name
return @
getUserName: ->
return _message['user_name']
setIconUrl: (icon_url) ->
_message['icon_url'] = icon_url
return @
getIconUrl: ->
return _message['icon_url']
setMarkDown: (trueOrFalse) ->
_message['mrkdwn'] = trueOrFalse
return @
getMarkDown: ->
return _message['mrkdwn']
toJson: ->
return JSON.stringify(_message)
class SlackGateway
instance = null
class _SlackGateway
push: (url, payload) ->
webGateway = WebGateway.getInstance()
webGateway.post(url, payload)
@getInstance: ->
return instance ?= new _SlackGateway()
class WebGateway
instance = null
class _WebGateway
getBody: (url) ->
UrlFetchApp.fetch(url).getContentText()
post: (url, payload) ->
options =
method: 'post'
payload: payload
UrlFetchApp.fetch(url, options)
@getInstance: ->
return instance ?= new _WebGateway()
class PushWeatherToSlackService
instance = null
class _PushWeatherToSlackService
pushToSlack: (city) ->
# generate weather object
weatherAPIGateway = WeatherAPIGateway.getInstance()
weather = weatherAPIGateway.getWeatherByCityId(city.Id)
# create slack push message
message = new SlackPushMessage()
message.setUserName(
'Tokyo Weather Forecast at ' +
Utilities.formatDate(
new Date(weather.getUnixTime() * 1000), 'GMT' + city.TimeZone,
'yyyy-MM-dd HH:mm'))
message.setIconUrl(weather.getImageUrl())
message.setText(
'*' + weather.getTitle() + '* (' + weather.getDescription() + ')n' +
'Temp Range: ' + weather.getMinimumTemparetureInCelcius() + '-' +
weather.getMaximumTemparatureInCelcius() + ' °;Cn' +
'Wind: ' + weather.getWindSpeed() + ' m/sn' +
'<' + weatherAPIGateway.getDetailUrlByCityId(city.Id) + '|More Detail>' )
# push to slack
slackGateway = SlackGateway.getInstance()
slackGateway.push(
SlackUrl.incoming.bot, message.toJson())
@getInstance: ->
return instance ?= new _PushWeatherToSlackService()
###
execute
###
pushWeatherToSlackService = PushWeatherToSlackService.getInstance()
pushWeatherToSlackService.pushToSlack(City.Tokyo)