0

我想要做的就是将LinkedIn 标题和群组帖子的ID 存储到我的文章模型中。这应该发生在:

irb> Article.update_from_linkedin_group

我花了很多时间并尝试了很多变化。有人可以指出我缺少什么吗?

article.rb(模型):

class Article < ActiveRecord::Base

  def self.update_from_linkedin_group
    require 'linkedin'
    client = Linkedin.new({
      :consumer_key => "zzzzzz",     # LinkedIn API key.
      :consumer_secret => "zzzzzzz",  # LinkedIn API secret.
      :access_token => "zzzzzzz",     # Authenticated user access token.
      :access_secret => "zzzzzz"     # Authenticated user access secret.
    })
    json = client.group
    puts json.inspect
    add_entries(json)
  end
  private

  def self.add_entries(values)
    values.each do |value|
      unless exists? guid: value.id
        create!(
          title: value.title,
          guid: value.id
        )
      end
    end
  end
end

关注/linkedin.rb:

class Linkedin
  require 'oauth'
  require 'active_support'
  require 'json'
  def initialize(config)
    consumer = OAuth::Consumer.new(config[:consumer_key], config[:consumer_secret], {
      :site => 'https://api.linkedin.com',
      :authorize_path => '/uas/oauth/authorize',
      :request_token_path => '/uas/oauth/requestToken',
      :access_token_path => '/uas/oauth/accessToken'
        })
    @client = OAuth::AccessToken.new(consumer, config[:access_token], config[:access_secret])
  end

  def group
    request(:get, '/groups/4896676/posts?order=recency&category=discussion&format=json')
  end

  private
    def request(method, path)
      JSON.parse(@client.request(method, 'https://api.linkedin.com/v1' + path).body)
    end
end

回复:

1.9.3p374 :001 > Article.update_from_linkedin_group
{"_count"=>10, "_start"=>0, "_total"=>95, "values"=>[{"creator"=>{"firstName"=>"Brian", "headline"=>"Reach AngularJS Developers Via @AngularJobs", "id"=>"Cg70QGPa6j", "lastName"=>"Petro", "pictureUrl"=>"http://m3.licdn.com/mpr/mprx/0_CV5yQJidyeFzm-JZC0zfQsGEpa3v2qZZhZliQU1Miez51K74apvKHRbB-iTE71MN_JbCWpSkwG2e"}, "id"=>"g-4896676-S-240161182", "title"=>"A Reusable Bootstrap Modal AngularJS Directive for Forms", "type"=>{"code"=>"standard"}}, {"creator"=>{"firstName"=>"Brian", "headline"=>"Reach AngularJS Developers Via @AngularJobs", "id"=>"Cg70QGPa6j", "lastName"=>"Petro", "pictureUrl"=>"http://m3.licdn.com/mpr/mprx/0_CV5yQJidyeFzm-JZC0zfQsGEpa3v2qZZhZliQU1Miez51K74apvKHRbB-iTE71MN_JbCWpSkwG2e"}, "id"=>"g-4896676-S-240017185", "title"=>"Testing AngularJS", "type"=>{"code"=>"standard"}}, {"creator"=>{"firstName"=>"Brian", "headline"=>"Reach AngularJS Developers Via @AngularJobs", "id"=>"Cg70QGPa6j", "lastName"=>"Petro", "pictureUrl"=>"http://m3.licdn.com/mpr/mprx/0_CV5yQJidyeFzm-JZC0zfQsGEpa3v2qZZhZliQU1Miez51K74apvKHRbB-iTE71MN_JbCWpSkwG2e"}, "id"=>"g-4896676-S-240016428", "title"=>"AngularJS: Managing Feeds", "type"=>{"code"=>"standard"}}, {"creator"=>{"firstName"=>"Brian", "headline"=>"Reach AngularJS Developers Via @AngularJobs", "id"=>"Cg70QGPa6j", "lastName"=>"Petro", "pictureUrl"=>"http://m3.licdn.com/mpr/mprx/0_CV5yQJidyeFzm-JZC0zfQsGEpa3v2qZZhZliQU1Miez51K74apvKHRbB-iTE71MN_JbCWpSkwG2e"}, "id"=>"g-4896676-S-240016505", "title"=>"HashKeyCopier - An AngularJS Utility Class For Merging Cached And...", "type"=>{"code"=>"standard"}}, {"creator"=>{"firstName"=>"Brian", "headline"=>"Reach AngularJS Developers Via @AngularJobs", "id"=>"Cg70QGPa6j", "lastName"=>"Petro", "pictureUrl"=>"http://m3.licdn.com/mpr/mprx/0_CV5yQJidyeFzm-JZC0zfQsGEpa3v2qZZhZliQU1Miez51K74apvKHRbB-iTE71MN_JbCWpSkwG2e"}, "id"=>"g-4896676-S-240016218", "title"=>"« Building Huuuuuge Apps with AngularJS", "type"=>{"code"=>"standard"}}, {"creator"=>{"firstName"=>"Brian", "headline"=>"Reach AngularJS Developers Via @AngularJobs", "id"=>"Cg70QGPa6j", "lastName"=>"Petro", "pictureUrl"=>"http://m3.licdn.com/mpr/mprx/0_CV5yQJidyeFzm-JZC0zfQsGEpa3v2qZZhZliQU1Miez51K74apvKHRbB-iTE71MN_JbCWpSkwG2e"}, "id"=>"g-4896676-S-240015464", "title"=>"Building a Spreadsheet in 20 Minutes with Angular.js", "type"=>{"code"=>"standard"}}, {"creator"=>{"firstName"=>"Brian", "headline"=>"Reach AngularJS Developers Via @AngularJobs", "id"=>"Cg70QGPa6j", "lastName"=>"Petro", "pictureUrl"=>"http://m3.licdn.com/mpr/mprx/0_CV5yQJidyeFzm-JZC0zfQsGEpa3v2qZZhZliQU1Miez51K74apvKHRbB-iTE71MN_JbCWpSkwG2e"}, "id"=>"g-4896676-S-239786186", "title"=>"5 reasons to use AngularJS in the corporate app world", "type"=>{"code"=>"standard"}}, {"creator"=>{"firstName"=>"Brian", "headline"=>"Reach AngularJS Developers Via @AngularJobs", "id"=>"Cg70QGPa6j", "lastName"=>"Petro", "pictureUrl"=>"http://m3.licdn.com/mpr/mprx/0_CV5yQJidyeFzm-JZC0zfQsGEpa3v2qZZhZliQU1Miez51K74apvKHRbB-iTE71MN_JbCWpSkwG2e"}, "id"=>"g-4896676-S-239786152", "title"=>"dangle.js", "type"=>{"code"=>"standard"}}, {"creator"=>{"firstName"=>"Brian", "headline"=>"Reach AngularJS Developers Via @AngularJobs", "id"=>"Cg70QGPa6j", "lastName"=>"Petro", "pictureUrl"=>"http://m3.licdn.com/mpr/mprx/0_CV5yQJidyeFzm-JZC0zfQsGEpa3v2qZZhZliQU1Miez51K74apvKHRbB-iTE71MN_JbCWpSkwG2e"}, "id"=>"g-4896676-S-239785986", "title"=>"nodejs-mongo-angular-crud", "type"=>{"code"=>"standard"}}, {"creator"=>{"firstName"=>"Milan", "headline"=>"Yii/API Ninja at Divide Nine / Divide Nine Studios", "id"=>"5AGK3AkNFA", "lastName"=>"Z.", "pictureUrl"=>"http://m3.licdn.com/mpr/mprx/0_EBP1XaLgxDUIlA4nH9z2Xub-gdO6PtHne1buXuv_C7o9u9096l_0E23SPXYJrvdswNlDIIIGuxTj"}, "id"=>"g-4896676-S-239768061", "title"=>"2. Incorporate It", "type"=>{"code"=>"standard"}}]}
NoMethodError: undefined method `id' for ["_count", 10]:Array
from /home/brian/apps/angularjobs/app/models/article.rb:19:in `block in add_entries'
from /home/brian/apps/angularjobs/app/models/article.rb:18:in `each'
from /home/brian/apps/angularjobs/app/models/article.rb:18:in `add_entries'
from /home/brian/apps/angularjobs/app/models/article.rb:13:in `update_from_linkedin_group'
from (irb):1
from /home/brian/.rvm/gems/ruby-1.9.3-p374/bundler/gems/rails-1e5ee397ff37/railties/lib/rails/commands/console.rb:90:in `start'
from /home/brian/.rvm/gems/ruby-1.9.3-p374/bundler/gems/rails-1e5ee397ff37/railties/lib/rails/commands/console.rb:9:in `start'
from /home/brian/.rvm/gems/ruby-1.9.3-p374/bundler/gems/rails-1e5ee397ff37/railties/lib/rails/commands.rb:66:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
4

1 回答 1

1

在 Article#add_entries 方法中,您正在对整个返回的 JSON 调用 #each。您要做的是在 JSON 哈希的“值”键上调用 #each,然后从其中的每个哈希中提取 id 和标题。所以它可能看起来像这样:

values["values"].each do |value|
  unless exists? guid: value['id']
    create!(
      title: value['title'],
      guid: value['id']
    )
  end
end
于 2013-05-12T22:07:07.870 回答