Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Extract controller logic into models #11

Merged
merged 1 commit into from May 18, 2012
Jump to file or symbol
Failed to load files and symbols.
+27 −10
Split
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 = []