Completed
Push — master ( 9d769a...f41e1d )
by John
01:43
created

Quotes   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 86
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 5 1
A init_quotes() 0 49 1
A authors_string() 0 3 1
B get_quote() 0 21 5
1
module Inspirebot
2
  class Quotes
3
    attr_accessor :add_attribution
4
    attr_accessor :authors
5
6
    def initialize()
7
      init_quotes
8
      @authors = @quotes.keys
9
      @add_attribution = false
10
    end
11
12
    def init_quotes
13
      @quotes = {
14
        branson: {
15
          name: 'Richard Branson',
16
          quotes: [
17
          "You don't learn to walk by following rules. You learn by doing, and by falling over.",
18
          "Business opportunities are like buses, there’s always another one coming.",
19
          "A business has to be involving, it has to be fun, and it has to exercise your creative instincts.",
20
          "Do not be embarrassed by your failures, learn from them and start again.",
21
          "One thing is certain in business. You and everyone around you will make mistakes.",
22
          "My general attitude to life is to enjoy every minute of every day. I never do anything with a feeling of, ’Oh God, I’ve got to do this today.’",
23
          "I cannot remember a moment in my life when I have not felt the love of my family. We were a family that would have killed for each other - and we still are.",
24
          "I never get the accountants in before I start up a business. It's done on gut feeling, especially if I can see that they are taking the mickey out of the consumer.",
25
          "I love the freedom of movement that my phone gives me. That has definitely transformed my life."
26
          ],
27
        },
28
        jobs: {
29
          name: 'Steve Jobs',
30
          quotes: [
31
          "Be a yardstick of quality. Some people aren't used to an environment where excellence is expected.",
32
          "Being the richest man in the cemetery doesn't matter to me. Going to bed at night saying we've done something wonderful, that's what matters to me.",
33
          "Design is not just what it looks like and feels like. Design is how it works.",
34
          "Do you want to spend the rest of your life selling sugared water or do you want a chance to change the world?",
35
          "Don’t let the noise of others’ opinions drown out your own inner voice.",
36
          "I want to put a ding in the universe.",
37
          "Innovation distinguishes between a leader and a follower.",
38
          "It's really hard to design products by focus groups. A lot of times, people don't know what they want until you show it to them.",
39
          "My favorite things in life don't cost any money. It's really clear that the most precious resource we all have is time.",
40
          "Quality is more important than quantity. One home run is much better than two doubles.",
41
          "Sometimes life is going to hit you in the head with a brick. Don't lose faith.",
42
          "Sometimes when you innovate, you make mistakes. It is best to admit them quickly, and get on with improving your other innovations.",
43
          "Things don't have to change the world to be important.",
44
          "Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do."
45
        ]
46
        },
47
        tesla: {
48
          name: 'Nikola Tesla',
49
          quotes: [
50
          "If you want to find the secrets of the universe, think in terms of energy, frequency and vibration.",
51
          "The day science begins to study non-physical phenomena, it will make more progress in one decade than in all the previous centuries of its existence.",
52
          "The scientists of today think deeply instead of clearly. One must be sane to think clearly, but one can think deeply and be quite insane.",
53
          "Our virtues and our failings are inseparable, like force and matter. When they separate, man is no more.",
54
          "The present is theirs; the future, for which I really worked, is mine.",
55
          "I do not think you can name many great inventions that have been made by married men.",
56
          "The spread of civilisation may be likened to a fire; first, a feeble spark, next a flickering flame, then a mighty blaze, ever increasing in speed and power."
57
          ]
58
        }
59
      }
60
    end
61
62
    def get_quote(name=nil)
63
      if name.nil?
64
        name = @authors.sample
65
      else
66
        name = name.to_sym unless name.is_a?(Symbol)
67
      end
68
      name = name.to_s.gsub(/\W/, '').downcase.to_sym
69
     
70
      quote = ''
71
72
      if @quotes.key? name
73
        collection = @quotes[name]
74
        display = collection[:name]
75
        quote = collection[:quotes].sample
76
        quote += ' - ' + display if @add_attribution
77
      else
78
        authors = authors_string
79
        quote = "I'm sorry, I could not found quotes for #{name}. To get a list of authors, type QUOTE AUTHORS"
80
      end
81
      return quote
82
    end
83
84
    def authors_string
85
      @authors.sort.join(', ')
86
    end
87
  end
88
end
89
90
module Lita
91
  module Handlers
92
    class Inspirebot < Handler
93
      route(/^(?:Test SMS usng a RingCentral Developer account - )?inspire me!?\s*$/i, :quote, command: false, help: { 'inspire me!' => 'Returns a random quote' })
94
      route(/^(?:Test SMS usng a RingCentral Developer account - )?authors\s*$/i, :authors, command: false, help: { 'quote authors' => 'Returns a list of known authors.' })
95
      route(/^(?:Test SMS usng a RingCentral Developer account - )?quote\s+([A-Za-z0-9]+)!?\s*/i, :quote, command: false, help: { 'quote <AUTHOR>' => 'Replies with random AUTHOR quote.' })
96
97
      def authors(response)
98
        @quotes = ::Inspirebot::Quotes.new
99
        authors = @quotes.authors_string.upcase
100
        response.reply "I know about the following authors: #{authors}. To hear quotes, type QUOTE {AUTHOR}"
101
      end
102
103
      def quote(response)
104
        @quotes = ::Inspirebot::Quotes.new
105
        author = response.match_data[1].downcase
106
        if author.downcase == 'authors'
107
          authors = @quotes.authors_string.upcase
108
          response.reply "I know about the following authors: #{authors}. To hear quotes, type QUOTE {AUTHOR}"
109
        else
110
          response.reply @quotes.get_quote(author)
111
        end
112
      end
113
114
      def menu(response)
115
        help = 'use QUOTE AUTHORS to get a list of authors. use QUOTE {AUTHOR} to get a random quote from the author'
116
        response.reply @quotes.get_quote(help)
117
      end
118
    end
119
  end
120
  Lita.register_handler(Lita::Handlers::Inspirebot)
121
end