Skip to content

Instantly share code, notes, and snippets.

@richardking
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save richardking/9081047 to your computer and use it in GitHub Desktop.

Select an option

Save richardking/9081047 to your computer and use it in GitHub Desktop.
Insta1 - Original InstagramAccount model
class InstagramAccount < ActiveRecord::Base
belongs_to :tag
has_many :instagram_media, :dependent => :destroy
after_create :get_instagram_id
after_save :sync_following
class << self
def import_or_sync(obj)
account = InstagramAccount.find_by_instagram_id(obj["id"]) || InstagramAccount.new
account.instagram_id = obj["id"]
account.name = obj["username"]
account.full_name = obj["full_name"]
if account.followers_count.nil?
more_details = Instagram.user(account.instagram_id)
account.followers_count = more_details["counts"]["followed_by"]
end
account.import_enabled = true
account.save
end
end
private
def get_instagram_id
user = Instagram.user_search(name).first
self.instagram_id = user["id"]
self.full_name = user["full_name"]
self.save
end
def sync_following
if self.import_enabled_changed?
self.import_enabled ? follow_user : unfollow_user
end
end
def follow_user
Instagram.follow_user(self.instagram_id) if Rails.env == 'production'
end
def unfollow_user
Instagram.unfollow_user(self.instagram_id) if Rails.env == 'production'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment