Jeremy Bray Jeremy Bray -4 years ago 168
JSON Question

Parsing Api with Httparty NoMethodError: undefined method `each' for nil:NilClass

Hi I am parsing JSON API from https://api.commissionfactory.com/V1/ via rake task

desc "run feed"
task :mr => :environment do
include HTTParty
url = 'https://api.commissionfactory.com/V1/Affiliate/Merchants?apiKey=fakekey&status=Joined'
response = HTTParty.get(url).parsed_response

response.each do |item|
Vendor.find_or_create_by(name: item['Name'])
end
end


I can get the json data but not parse it via response.each or response.map

When I try with the code above I get a

rake aborted!
NoMethodError: undefined method `each' for nil:NilClass


when I puts response I get the data below as expected:

{"Id"=>201, "DateCreated"=>"2014-04-10T10:47:35.747", "DateModified"=>"2017-06-11T00:00:32.11", "Name"=>"Tony's Furniture", "AvatarUrl"=>"https://c.url.com/io/39BD094A-24B5-4659-8A7A-FE6E023E9ED8.png"}


and I can manipulate the data via

response.map do |item|
puts item['Name']
puts item['Category']
puts item['TargetUrl']
puts item['Summary']
puts item['TrackingUrl']
puts item['AvatarUrl']
end


but as soon as i try to create or save records i get the same issue.

Vendors Controller

class VendorsController < ApplicationController
before_action :set_vendor, only: [:show, :edit, :update, :destroy]

# GET /vendors
# GET /vendors.json
def index
@vendors = Vendor.all
end

# GET /vendors/1
# GET /vendors/1.json
def show
@products = @vendor.products.find(params[:id])
end

# GET /vendors/new
def new
@vendor = Vendor.new
end

# GET /vendors/1/edit
def edit
end

# POST /vendors
# POST /vendors.json
def create
@vendor = Vendor.new(vendor_params)

respond_to do |format|
if @vendor.save
format.html { redirect_to @vendor, notice: 'Vendor was successfully created.' }
format.json { render :show, status: :created, location: @vendor }
else
format.html { render :new }
format.json { render json: @vendor.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /vendors/1
# PATCH/PUT /vendors/1.json
def update
respond_to do |format|
if @vendor.update(vendor_params)
format.html { redirect_to @vendor, notice: 'Vendor was successfully updated.' }
format.json { render :show, status: :ok, location: @vendor }
else
format.html { render :edit }
format.json { render json: @vendor.errors, status: :unprocessable_entity }
end
end
end

# DELETE /vendors/1
# DELETE /vendors/1.json
def destroy
@vendor.destroy
respond_to do |format|
format.html { redirect_to vendors_url, notice: 'Vendor was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_vendor
@vendor = Vendor.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def vendor_params
params.require(:vendor).permit(:name, :description, :Category, :TargetUrl, :Summary, :TrackingUrl, :AvatarUrl)
end
end


Vendor Model

class Vendor < ApplicationRecord
has_many :products
has_many :brands
end


While I have parsed xml data with nokogiri this is my first json parse with httparty. Can you please point out why I cannot seem to parse the data into the rails vendor db

Answer Source

With a little help from a mentor we were able to discover that the

include HTTParty

in my task

desc "run feed"
task :mr => :environment do
  include HTTParty
  url = 'https://api.commissionfactory.com/V1/Affiliate/Merchants?apiKey=fakekey&status=Joined'
  response = HTTParty.get(url).parsed_response

  response.each do |item|
    Vendor.find_or_create_by(name: item['Name'])
  end
end

was throwing the error. with its removal, the task passed.

As an older library, the include statement imports the entire library, which in this case threw the error and was not related to my needs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download