Class: FactsByDate::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/facts_by_date.rb

Constant Summary collapse

ROOT =
File.expand_path("../..", __FILE__)

Class Method Summary collapse

Class Method Details

.births_for_specific_date(date, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/facts_by_date.rb', line 73

def self.births_for_specific_date(date, options = {})
  begin
    converted_date = Date.parse(date)

    if File.exist?("#{ROOT}/data/births/#{converted_date.month}/#{converted_date.day}.json")
      file = File.read("#{ROOT}/data/births/#{converted_date.month}/#{converted_date.day}.json")
      data_hash = JSON.parse(file)

      facts = data_hash['births']

      # if size option is provided retrieve information only for that size
      if options[:size].nil?
        facts
      else
        facts.take(options[:size])
      end
    else
      []
    end
  rescue ArgumentError
    []
  end
end

.births_for_today(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/facts_by_date.rb', line 52

def self.births_for_today(options = {})
  converted_date = Date.today
  facts = []

  if File.exist?("#{ROOT}/data/births/#{converted_date.month}/#{converted_date.day}.json")
    file = File.read("#{ROOT}/data/births/#{converted_date.month}/#{converted_date.day}.json")
    data_hash = JSON.parse(file)

    facts = data_hash['births']

    # if size option is provided retrieve information only for that size
    if options[:size].nil?
      facts
    else
      facts.take(options[:size])
    end
  else
    []
  end
end

.deaths_for_specific_date(date, options = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/facts_by_date.rb', line 118

def self.deaths_for_specific_date(date, options = {})
  begin
    converted_date = Date.parse(date)

    if File.exist?("#{ROOT}/data/deaths/#{converted_date.month}/#{converted_date.day}.json")
      file = File.read("#{ROOT}/data/deaths/#{converted_date.month}/#{converted_date.day}.json")
      data_hash = JSON.parse(file)

      facts = data_hash['deaths']

      # if size option is provided retrieve information only for that size
      if options[:size].nil?
        facts
      else
        facts.take(options[:size])
      end
    else
      []
    end
  rescue ArgumentError
    []
  end
end

.deaths_for_today(options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/facts_by_date.rb', line 97

def self.deaths_for_today(options = {})
  converted_date = Date.today
  facts = []

  if File.exist?("#{ROOT}/data/deaths/#{converted_date.month}/#{converted_date.day}.json")
    file = File.read("#{ROOT}/data/deaths/#{converted_date.month}/#{converted_date.day}.json")
    data_hash = JSON.parse(file)

    facts = data_hash['deaths']

    # if size option is provided retrieve information only for that size
    if options[:size].nil?
      facts
    else
      facts.take(options[:size])
    end
  else
    []
  end
end

.facts_for_specific_date(date, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/facts_by_date.rb', line 28

def self.facts_for_specific_date(date, options = {})
  begin
    converted_date = Date.parse(date)

    if File.exist?("#{ROOT}/data/facts/#{converted_date.month}/#{converted_date.day}.json")
      file = File.read("#{ROOT}/data/facts/#{converted_date.month}/#{converted_date.day}.json")
      data_hash = JSON.parse(file)

      facts = data_hash[0]['facts']

      # if size option is provided retrieve information only for that size
      if options[:size].nil?
        facts
      else
        facts.take(options[:size])
      end
    else
      []
    end
  rescue ArgumentError
    []
  end
end

.facts_for_today(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/facts_by_date.rb', line 7

def self.facts_for_today(options = {})
  converted_date = Date.today
  facts = []

  if File.exist?("#{ROOT}/data/facts/#{converted_date.month}/#{converted_date.day}.json")
    file = File.read("#{ROOT}/data/facts/#{converted_date.month}/#{converted_date.day}.json")
    data_hash = JSON.parse(file)

    facts = data_hash[0]['facts']

    # if size option is provided retrieve information only for that size
    if options[:size].nil?
      facts
    else
      facts.take(options[:size])
    end
  else
    []
  end
end

.international_days_for_specific_date(date, options = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/facts_by_date.rb', line 163

def self.international_days_for_specific_date(date, options = {})
  begin
    converted_date = Date.parse(date)

    if File.exist?("#{ROOT}/data/international_days/#{converted_date.month}/#{converted_date.day}.json")
      file = File.read("#{ROOT}/data/international_days/#{converted_date.month}/#{converted_date.day}.json")
      data_hash = JSON.parse(file)

      facts = data_hash['facts']

      # if size option is provided retrieve information only for that size
      if options[:size].nil?
        facts
      else
        facts.take(options[:size])
      end
    else
      []
    end
  rescue ArgumentError
    []
  end
end

.international_days_for_today(options = {}) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/facts_by_date.rb', line 142

def self.international_days_for_today(options = {})
  converted_date = Date.today
  facts = []

  if File.exist?("#{ROOT}/data/international_days/#{converted_date.month}/#{converted_date.day}.json")
    file = File.read("#{ROOT}/data/international_days/#{converted_date.month}/#{converted_date.day}.json")
    data_hash = JSON.parse(file)

    facts = data_hash['facts']

    # if size option is provided retrieve information only for that size
    if options[:size].nil?
      facts
    else
      facts.take(options[:size])
    end
  else
    []
  end
end

.national_days_for_specific_date(date, options = {}) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/facts_by_date.rb', line 208

def self.national_days_for_specific_date(date, options = {})
  begin
    converted_date = Date.parse(date)

    if File.exist?("#{ROOT}/data/national_days/#{converted_date.month}/#{converted_date.day}.json")
      file = File.read("#{ROOT}/data/national_days/#{converted_date.month}/#{converted_date.day}.json")
      data_hash = JSON.parse(file)

      facts = data_hash['facts']

      # if size option is provided retrieve information only for that size
      if options[:size].nil?
        facts
      else
        facts.take(options[:size])
      end
    else
      []
    end
  rescue ArgumentError
    []
  end
end

.national_days_for_today(options = {}) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/facts_by_date.rb', line 187

def self.national_days_for_today(options = {})
  converted_date = Date.today
  facts = []

  if File.exist?("#{ROOT}/data/national_days/#{converted_date.month}/#{converted_date.day}.json")
    file = File.read("#{ROOT}/data/national_days/#{converted_date.month}/#{converted_date.day}.json")
    data_hash = JSON.parse(file)

    facts = data_hash['facts']

    # if size option is provided retrieve information only for that size
    if options[:size].nil?
      facts
    else
      facts.take(options[:size])
    end
  else
    []
  end
end

.sourcesObject



232
233
234
235
236
237
238
239
# File 'lib/facts_by_date.rb', line 232

def self.sources
  file_to_read = "#{ROOT}/data/sources.json"
  data = []
  file = File.read(file_to_read)
  data << JSON.parse(file).flatten

  data.flatten
end