I am getting incorrect user names from 'https://meta.discourse.org/c/bug.json'


#1

I have the following code in my controller

@url = ‘https://meta.discourse.org/c/bug.json
@forum_data = HTTParty.get(@url).parsed_response
@topic_one = @forum_data[‘topic_list’][‘topics’][1][‘title’]
@topic_five = @forum_data[‘topic_list’][‘topics’][5][‘title’]
@topic_one_author = @forum_data[‘users’][1][‘username’]
@topic_five_author = @forum_data[‘users’][5][‘username’]

and in my view

  <%= @topic_one %> by <%= @topic_one_author %>
  <%= @topic_five %> by <%= @topic_five_author %>

Which gives me the following results:

“Leave Group” button on /groups page doesn’t give any feedback by codinghorror
Attachment / Upload email links now throw 404 if not logged in (SSO) by eviltrout

The 1st and 5th results show the incorrect user.


(Matt Palmer) #2

I don’t think that the indices are consistent between the users array and the topics array. You have to search the users array for the object whose id attribute is the same value as the user_id attribute of the topic or post you’re interested in.


#3

I see this now

“posters”:[{“extras”:“latest single”,“description”:“Original Poster, Most Recent Poster”,“user_id”:26776}]

but I am really confused about pretty much all of it… it does not seem to follow key/value pairing … what code I use to pull user_id out of it?

thanks


(Matt Palmer) #4

That’s because the data is stored in arrays, so you need to search through it, rather than an object.

I already described that process:


#5

I understand clearly what needs to be done now, thanks.

@topic_author = @forum_data[‘topic_list’][‘topics’][1][‘posters’][0][‘user_id’] returns the user_id of the original poster.

Now i need to search the users array for the object whose id attribute is the same value as @topic_author.

I do not know how to write code that will do that. If the code is trivial, would you mind giving me a hint on how to do it? I am searching stackoverflow but not getting anywhere.


(Matt Palmer) #6
users.find { |u| u['id'] == user_id }

(cpradio) #7

Selfish plug

I would encourage you to visit Sitepoint or any other tech community and post your question there in the language you plan/want to use.