when I place this in my helper file in works and I can read this API info into the hash. But this doesn't work in my controller. I need it to work in my controller in order to paginate.
def object
JSON.parse open(end_point).read
end
def feed # <- feedjust a naming function for object
file = object #
return file #
end
def photo_object(object, record_number) # <- should take a feed or object
output = Hash.new
output = {
"pict" => file["data"][record_number]["images"]["standard_resolution"]["url"],
"height" => file["data"][record_number]["images"]["standard_resolution"]["height"],
"width" => file["data"][record_number]["images"]["standard_resolution"]["width"],
"name" => file["data"][record_number]["user"]["full_name"],
"profile_pict_Instagram" => file["data"][record_number]["user"]["profile_picture"],
"likes" => file["data"][record_number]["likes"]["count"],
"film_look" => file["data"][record_number]["filter"],
"post_date" => Time.at((file["data"][record_number]["created_time"]).to_i),
"rights" => file["data"][record_number]["attribution"],
"tags" => file["data"][record_number]["tags"]
}
return output
end
^^ I'm getting the error:
output = Hash.new
output = {
"pict" => file["data"][record_number]["images"]["standard_resolution"]["url"],
"height" => file["data"][record_number]["images"]["standard_resolution"]["height"],
"width" => file["data"][record_number]["images"]["standard_resolution"]["width"],
"name" => file["data"][record_number]["user"]["full_name"],
I get the Error NoMethodError in InstagramSessionsController#index undefined method `[]' for nil:NilClass on the "pict" => file["data"][record_number]["images"]["standard_resolution"]["url"],
def show
end
def new
@instagram_session = InstagramSession.new
end
def edit
end
# POST /instagram_sessions
# POST /instagram_sessions.json
def create
@instagram_session = InstagramSession.new(instagram_session_params)
respond_to do |format|
if @instagram_session.save
format.html { redirect_to @instagram_session, notice: 'Instagram session was successfully created.' }
format.json { render :show, status: :created, location: @instagram_session }
else
format.html { render :new }
format.json { render json: @instagram_session.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /instagram_sessions/1
# PATCH/PUT /instagram_sessions/1.json
def update
respond_to do |format|
if @instagram_session.update(instagram_session_params)
format.html { redirect_to @instagram_session, notice: 'Instagram session was successfully updated.' }
format.json { render :show, status: :ok, location: @instagram_session }
else
format.html { render :edit }
format.json { render json: @instagram_session.errors, status: :unprocessable_entity }
end
end
end
# DELETE /instagram_sessions/1
# DELETE /instagram_sessions/1.json
def destroy
@instagram_session.destroy
respond_to do |format|
format.html { redirect_to instagram_sessions_url, notice: 'Instagram session was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_instagram_session
@instagram_session = InstagramSession.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def instagram_session_params
params.require(:instagram_session).permit(:layout)
end
end
"pict" => file["data"][record_number]["images"]["standard_resolution"]["url"],
Some value in this chain is nil
which is causing
NoMethodError in InstagramSessionsController#index undefined method `[]' for nil:NilClass on the "pict" => file["data"][record_number]["images"]["standard_resolution"]["url"]
To avoid this much chaining you can change the method slightly which will avoid such errors
def photo_object(object, record_number) # <- should take a feed or object
data = file["data"] && file["data"][record_number]
return {} unless data.present?
images = data["images"] && data["images"]["standard_resolution"]
user = data["user"]
output = {
"pict" => images && images["url"],
"height" => images && images["height"],
"width" => images && images["width"],
"name" => user && user["full_name"],
"profile_pict_Instagram" => user && user["profile_picture"],
"likes" => data["likes"] && data["likes"]["count"],
"film_look" => data["filter"],
"post_date" => Time.at((data["created_time"]).to_i),
"rights" => data["attribution"],
"tags" => data["tags"]
}
end