Class: PryParsecom::Setting

Inherits:
Object
  • Object
show all
Defined in:
lib/pry-parsecom/setting.rb

Constant Summary collapse

DOT_DIRNAME =
"#{Dir.home}/.pry-parsecom"
SETTINGS_FILENAME =
"settings"
IGNORES_FILENAME =
"ignores"
KEYS_FILENAME =
"keys"
SCHEMAS_FILENAME =
"schemas"
USER_AGENT =
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)'
LOGIN_URL =
'https://parse.com/apps'
LOGIN_SUCCESS_FILENAME =
'apps.html'
LOGIN_ERROR_FILENAME =
'user_session.html'
APPS_XPATH =
'/html/body/div[4]/div[2]/div/div/div/div[3]/ul/li/ul/li/a'
APP_NAME_XPATH =
'//*[@id="parse_app_name"]'
APP_KEYS_CSS =
'div.app_keys.window'
APP_ID_XPATH =
'div[2]/div[1]/div[2]/div/input'
API_KEY_XPATH =
'div[2]/div[5]/div[2]/div/input'
MASTER_KEY_XPATH =
'div[2]/div[6]/div[2]/div/input'
@@agent =
Mechanize.new
@@current_app =
nil
@@apps =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, app_id, api_key, master_key, schemas) ⇒ Setting

Returns a new instance of Setting.



174
175
176
177
# 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

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



172
173
174
# File 'lib/pry-parsecom/setting.rb', line 172

def api_key
  @api_key
end

#app_idObject

Returns the value of attribute app_id.



172
173
174
# File 'lib/pry-parsecom/setting.rb', line 172

def app_id
  @app_id
end

#app_nameObject

Returns the value of attribute app_name.



172
173
174
# File 'lib/pry-parsecom/setting.rb', line 172

def app_name
  @app_name
end

#master_keyObject

Returns the value of attribute master_key.



172
173
174
# File 'lib/pry-parsecom/setting.rb', line 172

def master_key
  @master_key
end

#schemasObject

Returns the value of attribute schemas.



172
173
174
# File 'lib/pry-parsecom/setting.rb', line 172

def schemas
  @schemas
end

Class Method Details

.app_namesObject



154
155
156
# File 'lib/pry-parsecom/setting.rb', line 154

def app_names
  @@apps.keys
end

.by_name(app_name) ⇒ Object Also known as: []



162
163
164
# File 'lib/pry-parsecom/setting.rb', line 162

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

.cache_timeObject



146
147
148
149
150
151
152
# 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

.current_appObject



26
27
28
# File 'lib/pry-parsecom/setting.rb', line 26

def current_app
  @@current_app
end

.current_app=(app) ⇒ Object



30
31
32
33
34
# File 'lib/pry-parsecom/setting.rb', line 30

def current_app= app
  @@current_app = app
  store_setting
  app
end

.current_settingObject



36
37
38
# File 'lib/pry-parsecom/setting.rb', line 36

def current_setting
  @@apps[@@current_app]
end

.each(&block) ⇒ Object



167
168
169
# File 'lib/pry-parsecom/setting.rb', line 167

def each &block
  @@apps.each &block
end

.has_app?(app_name) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/pry-parsecom/setting.rb', line 158

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

.login(email, password) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/pry-parsecom/setting.rb', line 106

def  email, password
  @@apps = {}

   = @@agent.get LOGIN_URL
  apps_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

.logoutObject



139
140
141
142
143
144
# 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) ⇒ Object



48
49
50
# File 'lib/pry-parsecom/setting.rb', line 48

def read_setting_file name
  begin File.read "#{DOT_DIRNAME}/#{name}" rescue '' end
end

.restoreObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

.setup_if_neededObject



40
41
42
43
44
45
46
# File 'lib/pry-parsecom/setting.rb', line 40

def setup_if_needed
  if @@apps.empty?
    unless restore
       *PryParsecom.ask_email_and_password 
    end
  end
end

.store_cacheObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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_settingObject



100
101
102
103
104
# 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) ⇒ Object



52
53
54
55
56
57
# 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

Instance Method Details

#applyObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# 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

#classesObject



219
220
221
# File 'lib/pry-parsecom/setting.rb', line 219

def classes
  schemas['collection'].map{|e| e['id']}.sort
end

#resetObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# 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

#schema(class_name) ⇒ Object



223
224
225
226
227
228
# 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