Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: no implicit conversion of nil into String error

Tags:

ruby

I am trying to set up our CRM system to create posts in wordpress when a specific type of record (a listing) is added.

I found this tutorial and am testing it in the rails console.

My class is:

require 'rubygems'
require 'open-uri'
require 'json'
require 'net/http'

# Please note that the vanilla wp-json-api plugin does not support user authentication for create_post.
# Check out my fork for authentication support: https://github.com/Achillefs/wp-json-api
class Listing
  API_URI = 'http://my_wp_url/api/'
  API_USER = 'my_username'
  API_PASS = 'my_password'

  attr_accessor :title, :address, :sell_price, :type

  def initialize(opts = {})
    # set all values if valid
    opts.each do |key,val|
      begin
        self.send(:"#{key}=",val)
      rescue NoMethodError => e
        raise ArgumentError("#{key} is not a valid listing attribute")
      end
    end
    self.type = 'post' if self.type == nil
  end

  def publish!
    # Need to get a nonce token from WP in order to create a post
    nonce_response = JSON.parse(open(API_URI + "get_nonce/?controller=posts&method=create_post").read)
    if nonce_response['status'] == 'ok'
      nonce = nonce_response['nonce']
      url = URI.parse(API_URI + "posts/create_post")
      args = { 
        'nonce' => nonce, 'author' => API_USER, 'user_password' => API_PASS, 
        'status' => 'draft', 'title' => self.title
      }
      resp, data = Net::HTTP.post_form(url, args)
      response = JSON.parse(data)
      if response['status'] == 'ok'
        puts response.inspect
      else
        raise response['error'].to_s
      end
    else
      raise nonce_response['error'].to_s
    end

  end
end

And the output in the console is as follows

2.0.0-p353 :001 > require 'listing'
 => true 
2.0.0-p353 :002 > l = Listing.new(:title => "test")
 => #<Listing:0x007fab9286a2c8 @title="test", @type="post"> 
2.0.0-p353 :003 > l.publish!
TypeError: no implicit conversion of nil into String
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/json-1.8.1/lib/json/common.rb:155:in `initialize'
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/json-1.8.1/lib/json/common.rb:155:in `new'
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/json-1.8.1/lib/json/common.rb:155:in `parse'
    from /Users/Nick/rails_projects/wp_api/lib/listing.rb:38:in `publish!'
    from (irb):3
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
2.0.0-p353 :004 > 

Line 38 is this one

response = JSON.parse(data)

So I guess I'm getting no response back. I'm relatively new to this and am not sure how to break down the cause.

Thanks in advance for any help.

Nick

like image 692
nktokyo Avatar asked Jan 14 '14 21:01

nktokyo


1 Answers

The error is at this line

resp, data = Net::HTTP.post_form(url, args)
response = JSON.parse(data)

You are assuming that post_form returns both a response and a data object, but it looks like the value of data is nil.

Inspect the content of resp and data. I'm quite sure the response body is contained in the resp object.

You should be able to fetch it using resp.body

response = JSON.parse(resp.body)
like image 109
Simone Carletti Avatar answered Oct 28 '22 08:10

Simone Carletti