skip to Main Content

I’m using Omniauth-Twitter with Device.

Twitter API gives me

{
  "id": 2244994945,
  "id_str": "2244994945",
  "name": "TwitterDev",
  "screen_name": "TwitterDev",
  "location": "Internet",
  "profile_location": null,
  "description": "Developer and Platform Relations @Twitter. We are developer advocates. We can't answer all your questions, but we listen to all of them!",
  "url": "http://twitter.com",
  "entities": {
    "url": {
      "urls": [
        {
          "url": "https://twitter.com",
          "expanded_url": "https://dev.twitter.com/",
          "display_url": "dev.twitter.com",
          "indices": [
            0,
            23
          ]
        }
      ]
    },
    "description": {
      "urls": []
    }
  },
.....

and I need to get the expanded_url for my website field, but it is in kinda array, how do I get that data?

def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |designer|
      user.provider = auth.provider
      user.uid = auth.uid

      user.username = auth.info.nickname

      user.website = HERE_I_NEED_USERS_WEBISTE_INFO
    end
  end

2

Answers


  1. Chosen as BEST ANSWER

    adding this to my designer.rb worked!

      begin 
        user.website = Net::HTTP.get_response(URI.parse(auth.info.urls.Website))['location']
        rescue
          user.website = auth.info.urls.Website
      end
    

  2. Lets suppose you have that twitter data in a variable called twitter_data. if it has the above structure, you’d be able to get to the extended_url this way:

    twitter_data["entities"]["url"]["urls"].first["expanded_url"]
    

    I’d recommend trying this in the console with some real data and seeing what you get.

    Edit:

    According to the omniauth-twitter gem doco, you can get all this info from the authentication hash.

    and it looks like you’d be able to get the relevant url like this:

    auth = request.env['omniauth.auth']
    twitter_data = auth[:extra][:raw_info]
    twitter_data["entities"]["url"]["urls"].first["expanded_url"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search