-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathquotes_finder.rb
75 lines (74 loc) · 1.46 KB
/
quotes_finder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# ** OBSOLETE **
# Class used for retrieving the quotes data. However,
# it is no longer used in favour of another way
# to obtain this data.
# require 'net/http'
# require 'json'
# require 'set'
# require 'csv'
#
# class Quote
# attr_reader :text
# attr_reader :author
#
# def initialize(text, author)
# @text = text
# @author = author
# end
#
# def to_str
# to_s
# end
#
# def to_s
# @text + '(' + @author + ')'
# end
#
# def ==(other)
# other.class == self.class && other.state == state
# end
#
# def eql?(other)
# other.class == self.class && other.state == state
# end
#
# def hash
# state.hash
# end
#
# protected
#
# def state
# [@text, @author]
# end
# end
#
# quotes = Set.new
#
# while quotes.size != 1800
#
# # Get quote from public rest API. Use key request parameter to get random quotes
# url = 'http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en&key=' + 6.times.map { rand(10) }.join
# uri = URI(url)
# response = Net::HTTP.get(uri)
#
# # When response contains unescaped characters, we are unable to parse it.
# begin
# json = JSON.parse(response)
# rescue
# puts 'Unable to parse response ' + response
# next
# end
#
# quote = Quote.new(json['quoteText'], json['quoteAuthor'])
# quotes.add(quote)
#
# puts quotes.size
# end
#
# CSV.open('quotes.csv', 'wb') do |csv|
# csv << %w[text author]
# quotes.each do |q|
# csv << [q.text, q.author]
# end
# end