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.



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

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.



152
153
154
# File 'lib/pry-parsecom/setting.rb', line 152

def api_key
  @api_key
end

#app_idObject

Returns the value of attribute app_id.



152
153
154
# File 'lib/pry-parsecom/setting.rb', line 152

def app_id
  @app_id
end

#app_nameObject

Returns the value of attribute app_name.



152
153
154
# File 'lib/pry-parsecom/setting.rb', line 152

def app_name
  @app_name
end

#master_keyObject

Returns the value of attribute master_key.



152
153
154
# File 'lib/pry-parsecom/setting.rb', line 152

def master_key
  @master_key
end

#schemasObject

Returns the value of attribute schemas.



152
153
154
# File 'lib/pry-parsecom/setting.rb', line 152

def schemas
  @schemas
end

Class Method Details

.app_namesObject



134
135
136
# File 'lib/pry-parsecom/setting.rb', line 134

def app_names
  @@apps.keys
end

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



142
143
144
# File 'lib/pry-parsecom/setting.rb', line 142

def by_name app_name
  @@apps[app_name.to_s]
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
# File 'lib/pry-parsecom/setting.rb', line 30

def current_app= app
  @@current_app = app
end

.current_settingObject



34
35
36
# File 'lib/pry-parsecom/setting.rb', line 34

def current_setting
  @@apps[@@current_app]
end

.each(&block) ⇒ Object



147
148
149
# File 'lib/pry-parsecom/setting.rb', line 147

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

.has_app?(app_name) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/pry-parsecom/setting.rb', line 138

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

.loadObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pry-parsecom/setting.rb', line 57

def load
  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
      return true
    end
  end
  false
end

.login(email, password) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pry-parsecom/setting.rb', line 94

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

  save
end

.logoutObject



127
128
129
130
131
132
# File 'lib/pry-parsecom/setting.rb', line 127

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



46
47
48
# File 'lib/pry-parsecom/setting.rb', line 46

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

.saveObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pry-parsecom/setting.rb', line 78

def save
  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

.setup_if_neededObject



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

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

.write_setting_file(name, hash) ⇒ Object



50
51
52
53
54
55
# File 'lib/pry-parsecom/setting.rb', line 50

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

#classesObject



199
200
201
# File 'lib/pry-parsecom/setting.rb', line 199

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

#resetObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/pry-parsecom/setting.rb', line 177

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



203
204
205
206
207
208
# File 'lib/pry-parsecom/setting.rb', line 203

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

#setObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/pry-parsecom/setting.rb', line 159

def set
  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