Permalink
Browse files

Merge pull request #11 from andyw8/tweets-controller-refactoring

Extract controller logic into models
  • Loading branch information...
Laurent Sansonetti
Laurent Sansonetti committed May 18, 2012
2 parents af59956 + 9485b73 commit 549f03deee4d13689343ef217bc3ab922d8f248f
Showing with 27 additions and 10 deletions.
  1. +10 −0 Tweets/app/data_parser.rb
  2. +12 −0 Tweets/app/json_parser.rb
  3. +5 −10 Tweets/app/tweets_controller.rb
View
@@ -0,0 +1,10 @@
+class DataParser
+ def self.parse(url)
+ error_ptr = Pointer.new(:object)
+ data = NSData.alloc.initWithContentsOfURL(NSURL.URLWithString(url), options:NSDataReadingUncached, error:error_ptr)
+ unless data
+ raise error_ptr[0]
+ end
+ data
+ end
+end
View
@@ -0,0 +1,12 @@
+class JSONParser
+ def self.parse_from_url(url)
+ data = DataParser.parse(url)
+
+ error_ptr = Pointer.new(:object)
+ json = NSJSONSerialization.JSONObjectWithData(data, options:0, error:error_ptr)
+ unless json
+ raise error_ptr[0]
+ end
+ json
+ end
+end
@@ -18,16 +18,11 @@ def searchBarSearchButtonClicked(searchBar)
@tweets.clear
Dispatch::Queue.concurrent.async do
- error_ptr = Pointer.new(:object)
- data = NSData.alloc.initWithContentsOfURL(NSURL.URLWithString(url), options:NSDataReadingUncached, error:error_ptr)
- unless data
- presentError error_ptr[0]
- return
- end
- json = NSJSONSerialization.JSONObjectWithData(data, options:0, error:error_ptr)
- unless json
- presentError error_ptr[0]
- return
+ json = nil
+ begin
+ json = JSONParser.parse_from_url(url)
+ rescue RuntimeError => e
+ presentError e.message
end
new_tweets = []

0 comments on commit 549f03d

Please sign in to comment.