Skip to content

Instantly share code, notes, and snippets.

@sbauch
Forked from clayallsopp/gist:3277152
Created August 6, 2012 18:30
Show Gist options
  • Save sbauch/3277354 to your computer and use it in GitHub Desktop.
Save sbauch/3277354 to your computer and use it in GitHub Desktop.
having trouble with bubblewrap
#directory_controller.rb
class DirectoryController < UIViewController
def viewDidLoad
super
self.title = "Directory"
@table = UITableView.alloc.initWithFrame(self.view.bounds)
self.view.addSubview @table
@table.dataSource = self
@table.delegate = self
Person.get_people do |people|
@data = people
end
end
def push
new_controller = PersonController.alloc.initWithNibName(nil, bundle: nil)
self.navigationController.pushViewController(new_controller, animated: true)
end
def initWithNibName(name, bundle: bundle)
super
self.tabBarItem = UITabBarItem.alloc.initWithTabBarSystemItem(UITabBarSystemItemFavorites, tag: 1)
self
end
def tableView(tableView, numberOfRowsInSection: section)
@data.count
end
def tableView(tableView, cellForRowAtIndexPath: indexPath)
@reuseIdentifier ||= "CELL_IDENTIFIER"
cell = tableView.dequeueReusableCellWithIdentifier(@reuseIdentifier) || begin
cell = UITableViewCell.alloc.initWithStyle(UITableViewCellSelectionStyleBlue, reuseIdentifier:@reuseIdentifier)
end
cell.textLabel.text = @data[indexPath.row]['first_name'] + " " + @data[indexPath.row]['last_name']
cell
end
def tableView(tableView, didSelectRowAtIndexPath:indexPath)
@detail_controller ||= PersonController.alloc.init
@detail_controller.selected_person(@data[indexPath.row]['id'])
self.navigationController.pushViewController(@detail_controller, animated:true)
tableView.deselectRowAtIndexPath(indexPath, animated:true)
end
end
### person.rb
class Person
PROPERTIES = [:first_name, :last_name, :id, :function, :phone, :email, :pic_url]
PROPERTIES.each { |prop|
attr_accessor prop
}
def initialize(hash = {})
hash.each { |key, value|
if PROPERTIES.member? key.to_sym
self.send((key.to_s + "=").to_s, value)
end
}
end
def self.get_people(&block)
BubbleWrap::HTTP.get("http://vaynernet.com/api.json") do |response|
@ppl = BW::JSON.parse(response.body)
block.call(@ppl)
end
# [{"id"=>10, "admin"=>true ... "last_name"=>"Bauch", "twitter_uid"=>nil, "avatar_file_name"=>"photo.jpeg", "updated_at"=>"2012-08-03T18:41:34Z", "first_name"=>"Sam", "start_date"=>"2012-08-03"}, ....]
end
def self.find(id, &block)
BubbleWrap::HTTP.get("http://vaynernet.com/api/#{id}.json") do |response|
@person = BW::JSON.parse(response.body)
block.call(@person)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment