From b924ed491cfba9ae9a46d457a2624c7373572529 Mon Sep 17 00:00:00 2001 From: javathunderman Date: Fri, 15 Jan 2016 22:26:19 +0000 Subject: [PATCH] Continuing #1387 --- lib/DDG/Goodie/Bmi.pm | 68 ++++++++ share/goodie/bmi/bmi.css | 54 +++++++ share/goodie/bmi/bmi.js | 149 ++++++++++++++++++ share/goodie/bmi/bmi.txt | 1 + share/goodie/bmi/content.handlebars | 34 ++++ share/goodie/cheat_sheets/cheat_sheets.js | 2 +- .../goodie/cheat_sheets/json/hdfs-shell.json | 38 +++++ t/Bmi.t | 38 +++++ 8 files changed, 383 insertions(+), 1 deletion(-) create mode 100644 lib/DDG/Goodie/Bmi.pm create mode 100644 share/goodie/bmi/bmi.css create mode 100644 share/goodie/bmi/bmi.js create mode 100644 share/goodie/bmi/bmi.txt create mode 100644 share/goodie/bmi/content.handlebars create mode 100644 share/goodie/cheat_sheets/json/hdfs-shell.json create mode 100644 t/Bmi.t diff --git a/lib/DDG/Goodie/Bmi.pm b/lib/DDG/Goodie/Bmi.pm new file mode 100644 index 000000000..4d9e82291 --- /dev/null +++ b/lib/DDG/Goodie/Bmi.pm @@ -0,0 +1,68 @@ +package DDG::Goodie::Bmi; +# ABSTRACT: A bmi calculator +# +use DDG::Goodie; + +zci answer_type => "bmi"; +zci is_cached => 1; + +name "Bmi"; +description "Pops a bmi calculator"; +primary_example_queries "bmi"; +category "calculations"; +topics "special_interest", "everyday"; +code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Bmi.pm"; +attribution github => ["jrenouard", "Julien Renouard"], + twitter => "jurenouard"; + +triggers start => "bmi"; +triggers any => "bmi calculator", "body mass index calculator"; + +# Handle statement +handle remainder => sub { + + return 'BMI', structured_answer => { + id => 'bmi', + name => 'Body Mass Index Calculatior', + data => { + title => "Body Mass Index Calculation", + text => { + imperial => "Imperial", + metric => "Metric", + height_placeholder => "Height in cm", + weight_placeholder => "Weight", + calculate => "Calculate", + feet => "Feet", + inches => "Inches", + pounds => "Pounds", + range => ["very severely underweight", + "severely underweight", + "underweight", + "normal", + "overweight", + "moderately obese", + "severely obese", + "very severely obese"], + error => "Error, your bmi is not a number, have you filled in the fields?", + your_bmi => "Your bmi is ", + within => "
This is within the ", + str_range => " range", + source_line => "
source Wikipedia https://en.wikipedia.org/wiki/Body_mass_index" + }, + }, + meta => { + sourceName => "Wikipedia", + sourceUrl => "https://en.wikipedia.org/wiki/Body_mass_index" + }, + templates => { + group => 'text', + item => 0, + options => { + content => 'DDH.bmi.content', + moreAt => 0 + } + } + }; +}; + +1; \ No newline at end of file diff --git a/share/goodie/bmi/bmi.css b/share/goodie/bmi/bmi.css new file mode 100644 index 000000000..4aa84dfbb --- /dev/null +++ b/share/goodie/bmi/bmi.css @@ -0,0 +1,54 @@ +form input { + + padding: 10px; + + font-size: 1.5em; + + border: 1px solid #eee; + + background-color: #fff; + +} + +#form_bmi_i input { + + width: 120px; + +} + + +.unit { + + padding: 0 0 10px 0; + +} + + +.unit .selected { + + font-weight: bold; + +} + + +#div_result { + + font-size: 1.5em; + + padding: 10px 0 0 0; + +} + + +#div_result small { + + font-size: 0.6em; + +} + + +#form_bmi_i { + + display: none; + +} diff --git a/share/goodie/bmi/bmi.js b/share/goodie/bmi/bmi.js new file mode 100644 index 000000000..48efbd7e3 --- /dev/null +++ b/share/goodie/bmi/bmi.js @@ -0,0 +1,149 @@ +DDH.bmi = DDH.bmi || {}; + + +DDH.bmi.build = function(ops) { + + var text = ops.data.text; + + + + function getBmi(h, w, unit) { + + if(unit == 'metric') { + + h = h/100; + + bmi = w / (h*h); + + } + + if(unit == 'imperial') { + + bmi = (w*703) / (h*h); + + } + + bmi = Math.round(bmi*100)/100; + + return bmi; + + } + + + function getRange(bmi) { + + if(bmi < 15) { + + range = text.range[0]; + + }else if(bmi >= 15 && bmi < 16) { + + range = text.range[1]; + + }else if(bmi >= 16 && bmi < 18.5) { + + range = text.range[2]; + + }else if(bmi >= 18.5 && bmi < 25) { + + range = text.range[3]; + + }else if(bmi >= 25 && bmi < 30) { + + range = text.range[4]; + + }else if(bmi >= 30 && bmi < 35) { + + range = text.range[5]; + + }else if(bmi >= 35 && bmi < 40) { + + range = text.range[6]; + + }else if(bmi >= 40) { + + range = text.range[7]; + + } + + return range; + + } + + + function formatResult(bmi) { + + if(isNaN(bmi)){ + + html = text.error; + + }else{ + + html = text.your_bmi + bmi; + + html += text.within + getRange(bmi) + text.str_range; + + html += text.source_line; + + } + + $('#div_result').html(html); + + } + + + return { + + onShow: function() { + + $('#form_bmi_m').on('submit', function(e) { + + e.preventDefault(); + + h = $('#height').val(); + + w = $('#weight').val(); + + res = getBmi(h, w, 'metric'); + + formatResult(res); + + }); + + + $('#form_bmi_i').on('submit', function(e) { + + e.preventDefault(); + + h = $('#feet').val()*12; + + h += $('#inches').val()*1; + + w = $('#pounds').val(); + + res = getBmi(h, w, 'imperial'); + + formatResult(res); + + }); + + + $('.unit a').on('click', function(e) { + + e.preventDefault(); + + $('#form_bmi_i, #form_bmi_m').toggle(); + + $('.unit a').toggleClass('selected'); + + $('#form_bmi_i input[type=number], #form_bmi_m input[type=number]').val(''); + + $('#div_result').html(''); + + }); + + } // ends onShow + + }; + +}; diff --git a/share/goodie/bmi/bmi.txt b/share/goodie/bmi/bmi.txt new file mode 100644 index 000000000..19765bd50 --- /dev/null +++ b/share/goodie/bmi/bmi.txt @@ -0,0 +1 @@ +null diff --git a/share/goodie/bmi/content.handlebars b/share/goodie/bmi/content.handlebars new file mode 100644 index 000000000..73c225b3e --- /dev/null +++ b/share/goodie/bmi/content.handlebars @@ -0,0 +1,34 @@ +
+ + {{text.imperial}} | {{text.metric}} + +
+ +
+ + + + + + + +
+ +
+ + + + + + + + + +
+ + +
+ + + +
diff --git a/share/goodie/cheat_sheets/cheat_sheets.js b/share/goodie/cheat_sheets/cheat_sheets.js index 6a8659086..d22f46a06 100644 --- a/share/goodie/cheat_sheets/cheat_sheets.js +++ b/share/goodie/cheat_sheets/cheat_sheets.js @@ -26,7 +26,7 @@ DDH.cheat_sheets.build = function(ops) { return result; }); - var re_brackets = /(?:\[|\{|\}|\])/, // search for [, {, }, or } + var re_brackets = /(?:\[|\{|\}|\])/, // search for [, {, }, or ] re_whitespace = /\s+/, // search for spaces re_codeblock = /(.+?)<\/code>/g; // search for diff --git a/share/goodie/cheat_sheets/json/hdfs-shell.json b/share/goodie/cheat_sheets/json/hdfs-shell.json new file mode 100644 index 000000000..29a8ffca9 --- /dev/null +++ b/share/goodie/cheat_sheets/json/hdfs-shell.json @@ -0,0 +1,38 @@ +{ + "id": "hdfs_shell_cheat_sheet", + "name": "HDFS Shell Commands", + "description": "A brief overview of the commands for interacting with HDFS files and directories", + "metadata": { + "sourceName": "Apache Hadoop v2.7.1 documentation", + "sourceUrl": "http://hadoop.apache.org/docs/r2.7.1/hadoop-project-dist/hadoop-common/FileSystemShell.html" + }, + "template_type": "terminal", + "section_order": ["Commands"], + "sections": { + "Commands": [{ + "key": "hdfs dfs -ls /user/abc", + "val": "List contents of a directory." + }, { + "key": "hdfs dfs -put file.txt /user/abc", + "val": "Upload file from local file system." + }, { + "key": "hdfs dfs -cat /user/abc/file.txt", + "val": "Read contents of a file on HDFS." + }, { + "key": "hdfs dfs -chmod 700 /user/abc/file.txt", + "val": "Change file permission." + }, { + "key": "hdfs dfs -setrep -w /user/adam/songs.txt", + "val": "Set replication factor to N." + }, { + "key": "hdfs dfs -du -h /user/abc/file.txt", + "val": "Check size of a file" + }, { + "key": "hdfs dfs -mv file.txt dir/", + "val": "Move file to subdirectory dir" + }, { + "key": "hdfs dfs -rm file.txt", + "val": "Delete file from HDFS." + }] + } +} \ No newline at end of file diff --git a/t/Bmi.t b/t/Bmi.t new file mode 100644 index 000000000..e41a66f82 --- /dev/null +++ b/t/Bmi.t @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use DDG::Test::Goodie; + +zci answer_type => "bmi"; +zci is_cached => 1; + +my $html = '

BMI Calculator

+ +
+ + + +
+
+ + + + +
+ +
+ +
'; + +ddg_goodie_test( + [qw( + DDG::Goodie::Bmi + )], + 'bmi' => test_zci('null', html => $html), +); + +done_testing; \ No newline at end of file