Skip to content

Instantly share code, notes, and snippets.

@joelbrewer
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save joelbrewer/7c63573fb222e90400c6 to your computer and use it in GitHub Desktop.

Select an option

Save joelbrewer/7c63573fb222e90400c6 to your computer and use it in GitHub Desktop.
def load_instagram(object)
object_method = self.method("instagram_#{object}")
if object_method.call != nil
object_method.call
elsif instagram_username.nil? || instagram_username.empty?
""
else
user = Instagram.user_search(instagram_username, {:count => 1})
if user.empty?
""
else
# save to db
attribute = user[0].send(object)
update_attribute("instagram_#{object}", attribute)
# return
object_method.call
end
end
end
@havenwood

Copy link
Copy Markdown
def load_instagram object
  method_name = "instagram_#{object}"
  return_value = public_send method_name

  if return_value
    return_value
  elsif no_instagram_username?
    ''
  else
    update_instagram_user return_value
  end
end

def no_instagram_username?
  instagram_username.nil? || instagram_username.empty?
end

def update_instagram_user return_value
  user = find_instagram_user

  if user.empty?
    ''
  else
    attribute = user.first.public_send object
    update_attribute method_name, attribute
    return_value
  end
end

def find_instagram_user
  Instagram.user_search instagram_username, count: 1
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment