From 883a467fd94888531b1f6fc7fbe28bb91744b8b8 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Tue, 12 Aug 2014 15:11:02 +0900 Subject: [PATCH 01/19] FARADAY_HTTP_BACKEND now affects RssAgent and PostAgent too. --- .env.example | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index 7a0c727a..8fed4f11 100644 --- a/.env.example +++ b/.env.example @@ -85,11 +85,11 @@ AWS_SANDBOX=false # Various Settings # ######################## -# Specify the HTTP backend library for Faraday, used in WebsiteAgent. -# You can change this depending on the performance and stability you -# need for your service. Any choice other than "typhoeus", -# "net_http", or "em_http" should require you to bundle a corresponding -# gem via Gemfile. +# Specify the HTTP backend library for Faraday, commonly used by +# WebsiteAgent, RssAgent and PostAgent. You can change this depending +# on the performance and stability you need for your service. Any +# choice other than "typhoeus", "net_http", or "em_http" should +# require you to bundle a corresponding gem via Gemfile. FARADAY_HTTP_BACKEND=typhoeus # Allow JSONPath eval expresions. i.e., $..price[?(@ < 20)] From 7ecbe78dffa42bc662fb4ed1e92d9bd322802abb Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Tue, 12 Aug 2014 15:31:26 +0900 Subject: [PATCH 02/19] Set a global default User-Agent value via an environment variable. Names starting with HTTP_ should be avoided in the context of Web app, hence the rather verbose name `DEFAULT_HTTP_USER_AGENT`. --- .env.example | 4 +++ app/concerns/web_request_concern.rb | 11 +++++--- .../shared_examples/web_request_concern.rb | 27 ++++++++++++++++++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index 8fed4f11..4bc92dbd 100644 --- a/.env.example +++ b/.env.example @@ -92,6 +92,10 @@ AWS_SANDBOX=false # require you to bundle a corresponding gem via Gemfile. FARADAY_HTTP_BACKEND=typhoeus +# Specify the default User-Agent header value for HTTP requests made +# by Agents that allow overriding the User-Agent header value. +DEFAULT_HTTP_USER_AGENT="Huginn - https://github.com/cantino/huginn" + # Allow JSONPath eval expresions. i.e., $..price[?(@ < 20)] # You should not allow this on a shared Huginn box because it is not secure. ALLOW_JSONPATH_EVAL=false diff --git a/app/concerns/web_request_concern.rb b/app/concerns/web_request_concern.rb index b4f4616f..86d7bccb 100644 --- a/app/concerns/web_request_concern.rb +++ b/app/concerns/web_request_concern.rb @@ -21,9 +21,7 @@ module WebRequestConcern @faraday ||= Faraday.new { |builder| builder.headers = headers if headers.length > 0 - if (user_agent = interpolated['user_agent']).present? - builder.headers[:user_agent] = user_agent - end + builder.headers[:user_agent] = user_agent builder.use FaradayMiddleware::FollowRedirects builder.request :url_encoded @@ -58,4 +56,9 @@ module WebRequestConcern def faraday_backend ENV.fetch('FARADAY_HTTP_BACKEND', 'typhoeus').to_sym end -end \ No newline at end of file + + def user_agent + interpolated['user_agent'].presence || + ENV.fetch('DEFAULT_HTTP_USER_AGENT', Faraday.new.headers[:user_agent]) + end +end diff --git a/spec/support/shared_examples/web_request_concern.rb b/spec/support/shared_examples/web_request_concern.rb index 510c533d..db44229f 100644 --- a/spec/support/shared_examples/web_request_concern.rb +++ b/spec/support/shared_examples/web_request_concern.rb @@ -63,4 +63,29 @@ shared_examples_for WebRequestConcern do agent.should_not be_valid end end -end \ No newline at end of file + + describe "User-Agent" do + before do + @default_http_user_agent = ENV['DEFAULT_HTTP_USER_AGENT'] + ENV['DEFAULT_HTTP_USER_AGENT'] = nil + end + + after do + ENV['DEFAULT_HTTP_USER_AGENT'] = @default_http_user_agent + end + + it "should have the default value set by Faraday" do + agent.user_agent.should == Faraday.new.headers[:user_agent] + end + + it "should be overridden by the environment variable if present" do + ENV['DEFAULT_HTTP_USER_AGENT'] = 'Huginn - https://github.com/cantino/huginn' + agent.user_agent.should == 'Huginn - https://github.com/cantino/huginn' + end + + it "should be overriden by the value in options if present" do + agent.options['user_agent'] = 'Override' + agent.user_agent.should == 'Override' + end + end +end From a224a0b9089ac5fa5e0e00c0b8c4a49406628662 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Tue, 12 Aug 2014 18:58:35 +0900 Subject: [PATCH 03/19] Use content_tag. --- app/helpers/application_helper.rb | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index db1583ec..a76dea97 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,27 +1,19 @@ module ApplicationHelper def nav_link(name, path, options = {}) - (<<-HTML).html_safe -
  • - #{link_to name, path} -
  • - HTML + content_tag :li, link_to(name, path), class: current_page?(path) ? 'active' : '' end def yes_no(bool) - if bool - 'Yes'.html_safe - else - 'No'.html_safe - end + content_tag :span, bool ? 'Yes' : 'No', class: "label #{bool ? 'label-info' : 'label-default' }" end def working(agent) if agent.disabled? - link_to 'Disabled', agent_path(agent), :class => 'label label-warning' + link_to 'Disabled', agent_path(agent), class: 'label label-warning' elsif agent.working? - 'Yes'.html_safe + content_tag :span, 'Yes', class: 'label label-success' else - link_to 'No', agent_path(agent, :tab => (agent.recent_error_logs? ? 'logs' : 'details')), :class => 'label label-danger' + link_to 'No', agent_path(agent, tab: (agent.recent_error_logs? ? 'logs' : 'details')), class: 'label label-danger' end end end From 05e373614efea3858e4e01cfc67be8ab15630238 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Tue, 12 Aug 2014 19:33:12 +0900 Subject: [PATCH 04/19] Add a hover menu to the "Agents" nav link. --- .../stylesheets/application.css.scss.erb | 6 +++++ app/helpers/application_helper.rb | 26 +++++++++++++++++-- app/views/layouts/_navigation.html.erb | 8 +++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/app/assets/stylesheets/application.css.scss.erb b/app/assets/stylesheets/application.css.scss.erb index 866b0b17..2cbff3e3 100644 --- a/app/assets/stylesheets/application.css.scss.erb +++ b/app/assets/stylesheets/application.css.scss.erb @@ -98,6 +98,12 @@ span.not-applicable:after { right: 1px; } +.navbar { + .dropdown.dropdown-hover:hover .dropdown-menu { + display: block; + } +} + // Flash .flash { diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index a76dea97..87de4b8e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,6 +1,28 @@ module ApplicationHelper - def nav_link(name, path, options = {}) - content_tag :li, link_to(name, path), class: current_page?(path) ? 'active' : '' + def nav_link(name, path, options = {}, &block) + if glyphicon = options[:glyphicon] + name = " ".html_safe + name + end + content = link_to(name, path) + active = current_page?(path) + if block + # Passing a block signifies that the link is a header of a hover + # menu which contains what's in the block. + begin + @nav_in_menu = true + @nav_link_active = active + content += capture(&block) + class_name = "dropdown dropdown-hover #{@nav_link_active ? 'active' : ''}" + ensure + @nav_in_menu = @nav_link_active = false + end + else + # Mark the menu header active if it contains the current page + @nav_link_active ||= active if @nav_in_menu + # An "active" menu item may be an eyesore, hence `!@nav_in_menu &&`. + class_name = !@nav_in_menu && active ? 'active' : '' + end + content_tag :li, content, class: class_name end def yes_no(bool) diff --git a/app/views/layouts/_navigation.html.erb b/app/views/layouts/_navigation.html.erb index 8a7d8915..f33d8981 100644 --- a/app/views/layouts/_navigation.html.erb +++ b/app/views/layouts/_navigation.html.erb @@ -12,7 +12,13 @@ <% if user_signed_in? %>