concerto_weather 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e15925cea3acb3e5c7263440c4747a5c0dd0897d
4
- data.tar.gz: 7de5a306a41e68b7fa7fd81a987eb8ada775fbd1
3
+ metadata.gz: fbddb0b388802f7c7bb916c32cdf2daf65717cea
4
+ data.tar.gz: 4d49707e9781261b06ae1f0bea600d3f4655aa23
5
5
  SHA512:
6
- metadata.gz: 7187967decce7e690a845f62ec0a3de6f935565ddf144f2eb52c8ec18875ecc9115858ffd945f40c5889947b774c3bc52cee46068f3269049424f34100d2e069
7
- data.tar.gz: d9157f22e38cce71ac993be5dcb3bd8ab6890fd9e797a1dc0159e3f95dd01bc73dbded059c30468c18e17658aaf797ee924ef1eccfab7e50f0405434bd994bba
6
+ metadata.gz: c419cefafa223ad60cc782c4bf2cd6419e4568820cea428f362c8af456207cfeebfa28d94e969edc0554034424925cda47d09a90011ba0f3b6255c051a427a91
7
+ data.tar.gz: e73a3895525f5db27e359126a789517a723949c4dd2e1a7059529cb8c35db827a91199e16fdaae78ddfdd7446746bd64e65f4c97595e0f7851cfc77e56250e6d
@@ -0,0 +1,93 @@
1
+ function weatherRowClickHandler(event) {
2
+ // Handle click events on city results
3
+ // Lat/lng saved with weather content for future API calls.
4
+ $("#weather_config_lat").val($(this).attr("data-lat"));
5
+ $("#weather_config_lng").val($(this).attr("data-lng"));
6
+ // Reset all selected rows
7
+ $(".city-info").find("tr").each(function() {
8
+ $(this).attr("data-selected", "0");
9
+ $(this).removeClass("alert-info");
10
+ });
11
+ // Set a new selected row
12
+ $(this).attr("data-selected", "1");
13
+ $(this).addClass("alert-info");
14
+ }
15
+
16
+ function reverseGeocode(place) {
17
+ // Reverse gecode returned coordinates from OpenWeatherMap API
18
+ // OpenWeatherMap API returns limited location information so this is important
19
+ // if users want to distinguish similarly named places
20
+ var params = {
21
+ format: "json",
22
+ lat: place.coord.lat,
23
+ lon: place.coord.lon
24
+ };
25
+
26
+ $.ajax({
27
+ url: "http://nominatim.openstreetmap.org/reverse",
28
+ data: params,
29
+ dataType: "json",
30
+ success: function(data) {
31
+ // Add a row to our results table
32
+ var lat = place.coord.lat;
33
+ var lng = place.coord.lon;
34
+ var row = "<tr class='link-hl' data-selected='0' data-lat='"+lat+"' data-lng='"+lng+"'> \
35
+ <td>" + place.name + "</td> \
36
+ <td>" + (data.address.county || '') + "</td> \
37
+ <td>" + (data.address.state || '') + "</td> \
38
+ <td>" + (data.address.country_code.toUpperCase() || '') + "</td>";
39
+ $('#cityResults tr:last').after(row);
40
+ // Handle click events for search results
41
+ $('#cityResults tr:last').on('click', weatherRowClickHandler)
42
+ }
43
+ });
44
+ }
45
+
46
+ function buildResultsTable(data) {
47
+ var places = data.list;
48
+ var info_el = $(".city-info");
49
+ // Build a table to display city query results
50
+ // User can select a city that best matches their intended location
51
+ table = "<table id='cityResults' class='table table-condensed'> \
52
+ <thead><th>Name</th><th>District/County/Region</th><th>Province/State</th><th>Country</th>";
53
+ tableBody = "<tbody></tbody></table>";
54
+ tableBody += "<hr/><i>Can't find your city? Try entering your zip code <b>or</b> your city along with its state (ex: Madison, WI).</i>"
55
+ // Insert our empty results table
56
+ $(info_el).empty().html(table + tableBody);
57
+ // Find the address info for each weather search result
58
+ // Then insert the place data into our results table
59
+ for (var i = 0; i < places.length; i++) {
60
+ reverseGeocode(places[i]);
61
+ }
62
+ }
63
+
64
+ function searchForCityInfo() {
65
+ var info_el = $(".city-info");
66
+ var cityQuery = $("input#weather_config_city_query").val();
67
+
68
+ if (info_el.length != 0 && cityQuery.length > 0) {
69
+ // Add a 'searching' placeholder while city query results are loading
70
+ $(info_el).empty().html("<i class='fa fa-spinner fa-spin'></i> searching...");
71
+ // Query the OpenWeatherAPI to find a list of matching cities based on input
72
+ $.ajax({
73
+ url: "/concerto_weather/city_search.js",
74
+ data: {"q": cityQuery},
75
+ dataType: "json",
76
+ timeout: 4000,
77
+ success: function(data) {
78
+ // Build a table of results returned from city query
79
+ buildResultsTable(data);
80
+ },
81
+ error: function() {
82
+ $(info_el).empty().html("<p>No results found.</p>");
83
+ }
84
+ });
85
+ }
86
+ }
87
+
88
+ function initCityIdSearch() {
89
+ var query_el = $("input#weather_config_city_query")
90
+ query_el.on("focusout", searchForCityInfo);
91
+ }
92
+
93
+ $(document).ready(initCityIdSearch);
@@ -0,0 +1,21 @@
1
+ module ConcertoWeather
2
+ class SearchController < ConcertoWeather::ApplicationController
3
+ def find_city
4
+ @results = getOpenWeatherCities(params[:q])
5
+
6
+ respond_to do |format|
7
+ format.js { render json: @results }
8
+ end
9
+ end
10
+
11
+ private
12
+ def getOpenWeatherCities(query)
13
+ require 'net/http'
14
+ require 'json'
15
+
16
+ appid = ConcertoConfig["open_weather_map_api_key"]
17
+ url = "http://api.openweathermap.org/data/2.5/find?q=#{query}&type=like&mode=json&appid=#{appid}"
18
+ return Net::HTTP.get(URI(url))
19
+ end
20
+ end
21
+ end
@@ -12,7 +12,8 @@ class Weather < DynamicContent
12
12
 
13
13
  # Build request url
14
14
  params = {
15
- id: self.config['city_id'],
15
+ lat: self.config['lat'],
16
+ lon: self.config['lng'],
16
17
  units: self.config['units'],
17
18
  cnt: 1,
18
19
  mode: 'json',
@@ -51,6 +52,6 @@ class Weather < DynamicContent
51
52
  # Weather needs a location. Also allow specification of units
52
53
  def self.form_attributes
53
54
  attributes = super()
54
- attributes.concat([:config => [:city_id, :units, :location_name]])
55
+ attributes.concat([:config => [:lat, :lng, :units, :location_name]])
55
56
  end
56
57
  end
@@ -7,24 +7,26 @@
7
7
  <div class='span4'>
8
8
  <%= form.fields_for :config do |config| %>
9
9
  <div class="clearfix">
10
- <%= config.label :city_id, "City ID" %>
10
+ <%= config.label :city_query, "City Search" %>
11
11
  <div class="input">
12
- <%= config.text_field :city_id, placeholder: "1273294", value: @content.config['city_id'] %>
12
+ <%= config.text_field :city_query, placeholder:"Name or Zipcode", value: @content.config['city_id'] %>
13
+ <%= button_tag type: "button", :class => "btn", onclick: "searchForCityInfo()" do %>
14
+ <i class="fa fa-refresh"></i>
15
+ <% end %>
13
16
  </div>
14
17
  </div>
15
- <div class="clearfix">
16
- <a href="http://openweathermap.org/help/city_list.txt" target="_blank"> Find your City ID </a>
17
- </div>
18
18
  <div class="clearfix">
19
19
  <%= config.label :units %>
20
20
  <div class="input">
21
21
  <%= config.select :units, Weather::UNITS.map {|k,v| [v, k]}, :selected => @content.config['units'] %>
22
22
  </div>
23
23
  </div>
24
- </div>
24
+ <%= config.hidden_field :lat %>
25
+ <%= config.hidden_field :lng %>
25
26
  <% end %>
26
- <div>
27
- <div class='woeid-info span8'></div>
27
+ </div>
28
+ <div class="clearfix">
29
+ <div class='city-info span8'></div>
28
30
  </div>
29
31
  </div>
30
32
  </fieldset>
data/config/routes.rb CHANGED
@@ -1,3 +1,7 @@
1
1
  Rails.application.routes.draw do
2
- resources :weathers, :controller => :contents, :except => [:index, :show], :path => "content"
2
+ resources :weathers, :controller => :contents, :except => [:index, :show], :path => 'content'
3
+ end
4
+
5
+ ConcertoWeather::Engine.routes.draw do
6
+ get '/city_search', to: 'search#find_city'
3
7
  end
@@ -5,24 +5,26 @@ module ConcertoWeather
5
5
  initializer "register content type" do |app|
6
6
  app.config.content_types << Weather
7
7
  end
8
-
8
+
9
9
  def plugin_info(plugin_info_class)
10
- @plugin_info ||= plugin_info_class.new do
10
+ @plugin_info ||= plugin_info_class.new do
11
+ add_route("concerto_weather", ConcertoWeather::Engine)
12
+
11
13
  add_config("open_weather_map_api_key", "",
12
14
  value_type: "string",
13
15
  category: "API Keys",
14
16
  description: "OpenWeatherMap API Access Token. This token is used for obtaining weather information when adding weather content. http://openweathermap.org/appid")
15
-
17
+
16
18
  # Add owfont (open weather) icon set to application layout for content preview / browsing
17
19
  add_header_tags do
18
20
  stylesheet_link_tag "concerto_weather/application"
19
21
  end
20
22
 
21
23
  # Add owfont (open weather) icon set to concerto-frontend layout for weather content shown on screens
22
- add_view_hook "frontend/ScreensController", :concerto_frontend_plugins do
24
+ add_view_hook "frontend/ScreensController", :concerto_frontend_plugins do
23
25
  "#{ stylesheet_link_tag 'concerto_weather/application' }"
24
26
  end
25
-
27
+
26
28
  end
27
29
  end
28
30
  end
@@ -1,3 +1,3 @@
1
1
  module ConcertoWeather
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: concerto_weather
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Michalski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-05 00:00:00.000000000 Z
11
+ date: 2016-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -41,9 +41,11 @@ files:
41
41
  - app/assets/fonts/concerto_weather/owfont-regular.ttf
42
42
  - app/assets/fonts/concerto_weather/owfont-regular.woff
43
43
  - app/assets/javascripts/concerto_weather/application.js
44
+ - app/assets/javascripts/concerto_weather/weather.js
44
45
  - app/assets/stylesheets/concerto_weather/application.css
45
46
  - app/assets/stylesheets/concerto_weather/owfont-regular.css.scss
46
47
  - app/controllers/concerto_weather/application_controller.rb
48
+ - app/controllers/concerto_weather/search_controller.rb
47
49
  - app/helpers/concerto_weather/application_helper.rb
48
50
  - app/models/weather.rb
49
51
  - app/views/contents/weather/_form_top.html.erb