Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract controller logic into models #11

Merged
merged 1 commit into from
May 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Tweets/app/data_parser.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions Tweets/app/json_parser.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
15 changes: 5 additions & 10 deletions Tweets/app/tweets_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ def searchBarSearchButtonClicked(searchBar)


@tweets.clear @tweets.clear
Dispatch::Queue.concurrent.async do Dispatch::Queue.concurrent.async do
error_ptr = Pointer.new(:object) json = nil
data = NSData.alloc.initWithContentsOfURL(NSURL.URLWithString(url), options:NSDataReadingUncached, error:error_ptr) begin
unless data json = JSONParser.parse_from_url(url)
presentError error_ptr[0] rescue RuntimeError => e
return presentError e.message
end
json = NSJSONSerialization.JSONObjectWithData(data, options:0, error:error_ptr)
unless json
presentError error_ptr[0]
return
end end


new_tweets = [] new_tweets = []
Expand Down