Permalink
Browse files

bdw classes and methods for assignment

  • Loading branch information...
Collin Schaafsma
Collin Schaafsma committed Feb 2, 2012
1 parent c18e410 commit def2538e6f3f4cc90db7809b0c8f452300de24e8
Showing with 110 additions and 25 deletions.
  1. +7 −0 base.rb
  2. +0 −24 fetch.rb
  3. +5 −0 mkay.rb
  4. +47 −1 simple.rb
  5. +51 −0 twitter.rb
View
@@ -0,0 +1,7 @@
+class Base
+
+ def current_date
+ Time.new
+ end
+
+end
View
@@ -1,24 +0,0 @@
-require 'net/http'
-require 'json'
-
-class Fetch
-
- @username = "collinschaafsma"
-
- def initialize(username="collinschaafsma")
- @username = username
- end
-
- def self.write_latest_tweet(write_to="/Users/collin/Desktop/latest_tweet.txt")
- File.open(write_to, 'w') {|f| f.write(self.new(@username).latest_tweet) }
- end
-
- def latest_tweet
- JSON.parse(
- Net::HTTP.get_response(
- URI("http://api.twitter.com/1/users/show.json?screen_name=#{@username}")
- ).body
- )['status']['text']
- end
-
-end
View
@@ -0,0 +1,5 @@
+class String
+ def to_mkay
+ self.concat(" mmmkay children.")
+ end
+end
View
@@ -1,3 +1,49 @@
-class Simple
+require "base"
+
+class Simple < Base
+
+ LYRIC = "They got money for wars, but can't feed the poor"
+
+ def in_2pac_lyric?(term)
+ LYRIC.include? term
+ end
+
+ def talk_to_me
+ if block_given?
+ #im gonna do something then yield
+ yield
+ else
+ puts "no block for you"
+ end
+ end
+
+ class << self
+
+ def big_array
+ Array(1..1000)
+ end
+
+ def add_it_all_up(*args)
+ args.inject(:+)
+ end
+
+ def add_two_numbers(number1, number2)
+ number1.to_i + number2.to_i
+ end
+
+ def fibonacci_sequence(sequence_count=10)
+ sequences = []
+ current_sequence = 0
+ next_sequence = 1
+
+ sequence_count.times do
+ sequences << current_sequence
+ current_sequence, next_sequence = next_sequence, current_sequence + next_sequence
+ end
+
+ sequences
+ end
+
+ end
end
View
@@ -0,0 +1,51 @@
+require 'net/http'
+require 'json'
+
+class Twitter
+
+ TWITTER_API_URL = "http://api.twitter.com/1"
+ DEFAULT_SAVE_FILE = "/Users/collin/Desktop/latest_tweet.txt"
+
+ def initialize(username="collinschaafsma")
+ @username = username
+ end
+
+ # Get's the users status if found or throws an exception
+ def status!
+ @text = JSON.parse(api_response)['status']['text']
+ self
+ end
+
+ # Get's the users status if found
+ def status
+ response = JSON.parse(api_response)
+ @text = response['status']['text'] if response.has_key?('status')
+ self
+ end
+
+ # Saves the status text to a file
+ def write(save_file=DEFAULT_SAVE_FILE)
+ File.open(save_file, 'w') {|f| f.write(@text) }
+ puts "Tweet saved to \"#{save_file}\""
+ end
+
+ # Returns the text of a status
+ def text
+ @text
+ end
+
+ # Returns the Twitter username
+ def username
+ @username
+ end
+
+ private
+
+ # Makes GET request to Twitter API
+ def api_response
+ Net::HTTP.get_response(
+ URI("#{TWITTER_API_URL}/users/show.json?screen_name=#{@username}")
+ ).body
+ end
+
+end

0 comments on commit def2538

Please sign in to comment.