获取HTTP响应正文作为字符串(BubbleWrap for RubyMotion)

时间:2012-11-07 17:23:01

标签: http twitter rubymotion

使用RubyMotion(第一次!),我想使用Twitter的搜索API为一些用户检索一些最近的推文,所以将下面的类放在一起。

tweets的值始终为空数组。我怀疑BW::HTTP.get(url)会产生自己的线程,导致问题。

真的,我只是希望twitter_search_results返回response.body.to_str,但我不知道该怎么做。

如何使用RubyMotion(或BubbleWrap)将一组Tweet对象放入我的UIViewController中?

class TweetsController
  def initialize
    @twitter_accounts = %w(dhh google)
    @tweets = []
  end

  def tweets
    twitter_search_results
    puts @tweets.count
    @tweets
  end

  def create_tweets(response)
    BW::JSON.parse(response)["results"].each do |result|
      @tweets << Tweet.new(result)
    end
  end

  def twitter_search_results
    query = @twitter_accounts.map{ |account| "from:#{account}" }.join(" OR ")
    url = "http://search.twitter.com/search.json?q=#{query}"
    BW::HTTP.get(url) do |response|
      create_tweets(response.body.to_str)
    end
  end
end

class TwitterViewController < UIViewController
  def viewDidLoad
    super
    self.view.backgroundColor = UIColor.blueColor
    @table = UITableView.alloc.initWithFrame(self.view.bounds)
    self.view.addSubview @table
    @table.dataSource = self
    @tweets_controller = TweetsController.new
  end

  def initWithNibName(name, bundle: bundle)
    super
    self.tabBarItem = UITabBarItem.alloc.initWithTitle(
      "Twitter",
      image: UIImage.imageNamed('twitter.png'),
      tag: 1)
    self
  end

  def tableView(tableView, numberOfRowsInSection: section)
    @tweets_controller.tweets.length
  end

  def tableView(tableView, cellForRowAtIndexPath: indexPath)
    @reuse_id = "Tweet"
    cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:@reuse_id)
    cell.textLabel.text = @tweets_controller.tweets[indexPath.row].text
    return cell
  end
end

class Tweet
  attr_reader :created_at, :from_user, :text
  def initialize(tweet_result)
    @created_at = tweet_result["created_at"]
    @from_user = tweet_result["from_user"]
    @text = tweet_result["text"]
  end
end

2 个答案:

答案 0 :(得分:0)

这里的问题是异步性。我认为你几乎就在那里,但在create_tweets之前没有调用puts @tweets方法。在这种情况下,我建议使用通知,因为我认为它们很好; - )

TweetsReady = 'TweetsReady'  # constants are nice
NSNotificationCenter.defaultCenter.postNotificationName(TweetsReady, object:@tweets)

In your controller, register for this notification in `viewWillAppear` and unregister in `viewWillDisappear`

NSNotificationCenter.defaultCenter.addObserver(self, selector: 'tweets_ready:', name: TweetsReady, object:nil)  # object:nil means 'register for all events, not just ones associated with 'object'
# ...
NSNotificationCenter.defaultCenter.removeObserver(self, name:TweetsReady, object:nil)

你的tweets_ready方法应该实现你的UI更改。

def tweets_ready(notification)
  @table.reloadData
end

答案 1 :(得分:0)

以下完整的控制器代码。我还放了project on GitHub

class TweetsController
  def initialize
    @twitter_accounts = %w(dhh google)
    @tweets = []
    create_tweets
  end

  def tweets
    @tweets
  end

  def create_tweets
    json_data = twitter_search_results.dataUsingEncoding(NSUTF8StringEncoding)
    e = Pointer.new(:object)
    dict = NSJSONSerialization.JSONObjectWithData(json_data, options:0, error: e)
    dict["results"].each do |result|
      p result.class
      p result
      @tweets << Tweet.new(result)
    end
  end

  def twitter_search_results
    query = @twitter_accounts.map{ |account| "from:#{account}" }.join(" OR ")
    url_string = "http://search.twitter.com/search.json?q=#{query}"
    url_string_escaped = url_string.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
    url = NSURL.URLWithString(url_string_escaped)
    request = NSURLRequest.requestWithURL(url)
    response = nil
    error = nil
    data = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: error)
    raise "BOOM!" unless (data.length > 0 && error.nil?)
    json = NSString.alloc.initWithData(data, encoding: NSUTF8StringEncoding)
  end
end