class PryParsecom::Setting

Constants

API_KEY_XPATH
APPS_XPATH
APP_ID_XPATH
APP_KEYS_CSS
APP_NAME_XPATH
DOT_DIRNAME
IGNORES_FILENAME
KEYS_FILENAME
LOGIN_ERROR_FILENAME
LOGIN_SUCCESS_FILENAME
LOGIN_URL
MASTER_KEY_XPATH
SCHEMAS_FILENAME
SETTINGS_FILENAME
USER_AGENT

Attributes

api_key[RW]
app_id[RW]
app_name[RW]
master_key[RW]
schemas[RW]

Public Class Methods

app_names() click to toggle source
# File lib/pry-parsecom/setting.rb, line 154
def app_names
  @@apps.keys
end
apply() click to toggle source
# File lib/pry-parsecom/setting.rb, line 179
def apply
  Parse.credentials :application_id => @app_id, :api_key => @api_key, :master_key => @master_key
  Parse::Client.default.application_id = @app_id
  Parse::Client.default.api_key = @api_key
  Parse::Client.default.master_key = @master_key

  schemas['collection'].each do |e|
    class_name = e['id']
    next if class_name[0] == '_'

    if Object.const_defined? class_name
      puts "#{class_name.to_s} has already exist."
      next
    end
    Parse::Object class_name
  end
end
by_name(app_name) click to toggle source
# File lib/pry-parsecom/setting.rb, line 162
def by_name app_name
  @@apps[app_name.to_s]
end
cache_time() click to toggle source
# File lib/pry-parsecom/setting.rb, line 146
def cache_time
  if File.exist? DOT_DIRNAME
    File.mtime "#{DOT_DIRNAME}/#{SCHEMAS_FILENAME}"
  else
    nil
  end
end
classes() click to toggle source
# File lib/pry-parsecom/setting.rb, line 219
def classes
  schemas['collection'].map{|e| e['id']}.sort
end
current_app() click to toggle source
# File lib/pry-parsecom/setting.rb, line 26
def current_app
  @@current_app
end
current_app=(app) click to toggle source
# File lib/pry-parsecom/setting.rb, line 30
def current_app= app
  @@current_app = app
  store_setting
  app
end
current_setting() click to toggle source
# File lib/pry-parsecom/setting.rb, line 36
def current_setting
  @@apps[@@current_app]
end
each(&block) click to toggle source
# File lib/pry-parsecom/setting.rb, line 167
def each &block
  @@apps.each &block
end
has_app?(app_name) click to toggle source
# File lib/pry-parsecom/setting.rb, line 158
def has_app? app_name
  @@apps.has_key? app_name.to_s
end
initialize(app_name, app_id, api_key, master_key, schemas) click to toggle source
# File lib/pry-parsecom/setting.rb, line 174
def initialize app_name, app_id, api_key, master_key, schemas
  @app_name, @app_id, @api_key, @master_key, @schemas = 
    app_name, app_id, api_key, master_key, schemas
end
login(email, password) click to toggle source
# File lib/pry-parsecom/setting.rb, line 106
def login email, password
  @@apps = {}

  login_page = @@agent.get LOGIN_URL
  apps_page = login_page.form_with :id => 'new_user_session' do |form|
    form['user_session[email]'] = email
    form['user_session[password]'] = password
  end.submit

  if apps_page.filename == LOGIN_ERROR_FILENAME
    puts 'login error'
    return
  end

  apps_page.search(APPS_XPATH).each do |a|
    href = a.attributes['href'].to_s
    count_page = @@agent.get "#{href}/collections/count"
    schema = JSON.parse count_page.content
    edit_page = @@agent.get "#{href}/edit"
    app_name = edit_page.search(APP_NAME_XPATH).first.attributes['value'].to_s
    app_keys = edit_page.search('div.app_keys.window')
    app_id = app_keys.search(APP_ID_XPATH).first.attributes['value'].to_s
    api_key = app_keys.search(API_KEY_XPATH).first.attributes['value'].to_s
    master_key = app_keys.search(MASTER_KEY_XPATH).first.attributes['value'].to_s

    next if read_setting_file(IGNORES_FILENAME).split("\n").include? app_name

    @@apps[app_name] = Setting.new app_name, app_id, api_key, master_key, schema
  end

  store_cache
end
logout() click to toggle source
# File lib/pry-parsecom/setting.rb, line 139
def logout
  FileUtils.rm "#{DOT_DIRNAME}/#{KEYS_FILENAME}"
  FileUtils.rm "#{DOT_DIRNAME}/#{SCHEMAS_FILENAME}"
  @@apps.clear
  @@current_app = nil
end
read_setting_file(name) click to toggle source
# File lib/pry-parsecom/setting.rb, line 48
  def read_setting_file name
    begin File.read "#{DOT_DIRNAME}/#{name}" rescue '' end
  end

  def write_setting_file name, hash
    Dir.mkdir DOT_DIRNAME unless File.exist? DOT_DIRNAME
    File.open "#{DOT_DIRNAME}/#{name}", 'w' do |file|
      file.write YAML.dump(hash)
    end
  end

  def restore
    if File.exist? DOT_DIRNAME
      settings = read_setting_file SETTINGS_FILENAME
      ignores = read_setting_file IGNORES_FILENAME
      keys = read_setting_file KEYS_FILENAME
      schemas = read_setting_file SCHEMAS_FILENAME

      if !keys.empty? && !schemas.empty?
        ignores = ignores.split "\n"
        keys = YAML.load keys
        schemas = YAML.load schemas
        keys.each do |app_name, key|
          @@apps[app_name] = Setting.new app_name, key['app_id'], key['api_key'], 
            key['master_key'], schemas[app_name]
        end
        unless settings.empty?
          settings = YAML.load settings
          self.current_app = settings['current_app']
        end
        return true
      end
    end
    false
  end

  def store_cache
    keys = {}
    schemas = {}
    @@apps.each do |app_name, setting|
      keys[app_name] = {
        'app_id' => setting.app_id,
        'api_key' => setting.api_key,
        'master_key' => setting.master_key
      }

      schemas[app_name] = setting.schemas
    end
    write_setting_file KEYS_FILENAME, keys
    write_setting_file SCHEMAS_FILENAME, schemas
  end

  def store_setting
    if @@current_app
      write_setting_file SETTINGS_FILENAME, 'current_app' => @@current_app
    end
  end

  def login email, password
    @@apps = {}

    login_page = @@agent.get LOGIN_URL
    apps_page = login_page.form_with :id => 'new_user_session' do |form|
      form['user_session[email]'] = email
      form['user_session[password]'] = password
    end.submit

    if apps_page.filename == LOGIN_ERROR_FILENAME
      puts 'login error'
      return
    end

    apps_page.search(APPS_XPATH).each do |a|
      href = a.attributes['href'].to_s
      count_page = @@agent.get "#{href}/collections/count"
      schema = JSON.parse count_page.content
      edit_page = @@agent.get "#{href}/edit"
      app_name = edit_page.search(APP_NAME_XPATH).first.attributes['value'].to_s
      app_keys = edit_page.search('div.app_keys.window')
      app_id = app_keys.search(APP_ID_XPATH).first.attributes['value'].to_s
      api_key = app_keys.search(API_KEY_XPATH).first.attributes['value'].to_s
      master_key = app_keys.search(MASTER_KEY_XPATH).first.attributes['value'].to_s

      next if read_setting_file(IGNORES_FILENAME).split("\n").include? app_name

      @@apps[app_name] = Setting.new app_name, app_id, api_key, master_key, schema
    end

    store_cache
  end

  def logout
    FileUtils.rm "#{DOT_DIRNAME}/#{KEYS_FILENAME}"
    FileUtils.rm "#{DOT_DIRNAME}/#{SCHEMAS_FILENAME}"
    @@apps.clear
    @@current_app = nil
  end

  def cache_time
    if File.exist? DOT_DIRNAME
      File.mtime "#{DOT_DIRNAME}/#{SCHEMAS_FILENAME}"
    else
      nil
    end
  end

  def app_names
    @@apps.keys
  end

  def has_app? app_name
    @@apps.has_key? app_name.to_s
  end

  def by_name app_name
    @@apps[app_name.to_s]
  end
  alias [] by_name

  def each &block
    @@apps.each &block
  end
end
reset() click to toggle source
# File lib/pry-parsecom/setting.rb, line 197
def reset
  schemas['collection'].each do |e|
    class_name = e['id']
    next if class_name[0] == '_'

    if Object.const_defined? class_name
      klass = Object.const_get class_name
      if klass < Parse::Object
        Object.class_eval do
          remove_const class_name
        end
      end
    end
  end

  Parse.credentials :application_id => '', :api_key => '', :mater_key => ''
  Parse::Client.default.application_id = nil
  Parse::Client.default.api_key = nil
  Parse::Client.default.master_key = nil
  Parse::Client.default
end
restore() click to toggle source
# File lib/pry-parsecom/setting.rb, line 59
def restore
  if File.exist? DOT_DIRNAME
    settings = read_setting_file SETTINGS_FILENAME
    ignores = read_setting_file IGNORES_FILENAME
    keys = read_setting_file KEYS_FILENAME
    schemas = read_setting_file SCHEMAS_FILENAME

    if !keys.empty? && !schemas.empty?
      ignores = ignores.split "\n"
      keys = YAML.load keys
      schemas = YAML.load schemas
      keys.each do |app_name, key|
        @@apps[app_name] = Setting.new app_name, key['app_id'], key['api_key'], 
          key['master_key'], schemas[app_name]
      end
      unless settings.empty?
        settings = YAML.load settings
        self.current_app = settings['current_app']
      end
      return true
    end
  end
  false
end
schema(class_name) click to toggle source
# File lib/pry-parsecom/setting.rb, line 223
def schema class_name
  schms = schemas['collection'].select do |e| 
    e['id'] == class_name || e['display_name'] == class_name
  end
  schms.empty? ? [] : schms.first['schema']
end
setup_if_needed() click to toggle source
# File lib/pry-parsecom/setting.rb, line 40
def setup_if_needed
  if @@apps.empty?
    unless restore
      login *PryParsecom.ask_email_and_password 
    end
  end
end
store_cache() click to toggle source
# File lib/pry-parsecom/setting.rb, line 84
def store_cache
  keys = {}
  schemas = {}
  @@apps.each do |app_name, setting|
    keys[app_name] = {
      'app_id' => setting.app_id,
      'api_key' => setting.api_key,
      'master_key' => setting.master_key
    }

    schemas[app_name] = setting.schemas
  end
  write_setting_file KEYS_FILENAME, keys
  write_setting_file SCHEMAS_FILENAME, schemas
end
store_setting() click to toggle source
# File lib/pry-parsecom/setting.rb, line 100
def store_setting
  if @@current_app
    write_setting_file SETTINGS_FILENAME, 'current_app' => @@current_app
  end
end
write_setting_file(name, hash) click to toggle source
# File lib/pry-parsecom/setting.rb, line 52
def write_setting_file name, hash
  Dir.mkdir DOT_DIRNAME unless File.exist? DOT_DIRNAME
  File.open "#{DOT_DIRNAME}/#{name}", 'w' do |file|
    file.write YAML.dump(hash)
  end
end