Skip to content

Instantly share code, notes, and snippets.

Created October 1, 2015 20:05
Show Gist options
  • Save anonymous/c7d69fac247542d1be0d to your computer and use it in GitHub Desktop.
Save anonymous/c7d69fac247542d1be0d to your computer and use it in GitHub Desktop.
Another Scrap from scrapfy.io
class AccountDictionary
QUERY = "Select Id, Name from Account".freeze
def initialize data_store
@redis = Ost.redis
@key = "accounts:#{data_store.id}"
@sf_api = data_store.owner.credential_for(data_store.id).api # Angelica!!!
end
def load
salesforce_accounts.each do |account|
save(account)
end
end
def get account_id
result = @redis.call("HGET", @hash_key, account_id)
result || get_from_salesforce(account_id)
end
private
def save account
@redis.call("HSET", @hash_key, account['Id'], account['Name'])
end
def salesforce_accounts str = QUERY
@sf_api.query(str)
end
def get_from_salesforce account_id
account = salesforce_accounts("#{QUERY} Where Account.Id = #{account_id}")
save(account)
end
end
# Usage
#
pepe = AccountDictionary.new("12345")
pepe.load
pepe.get("999") # => "Demandbyte"
# ...
# def find_data_store data_store_id = params[:id]
# @data_store = current_account.data_stores.find data_store_id
# end
class DataStore
def dict
@dict ||= AccountDictionary.new(self)
end
end
@data_store.get['999'] # => "Pepito" o nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment