Last active
August 29, 2015 13:56
-
-
Save richardking/9081047 to your computer and use it in GitHub Desktop.
Insta1 - Original InstagramAccount model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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