Class: Ruboty::YMCrawl::DataManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ruboty/ymcrawl/main.rb

Instance Method Summary collapse

Constructor Details

#initializeDataManager

Returns a new instance of DataManager.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruboty/ymcrawl/main.rb', line 22

def initialize
  @setting = JSON.parse( File.open(SETTING_FILE_PATH).read)
  puts "setting: #{@setting}"
  puts "YMCrawlfile valid"
  puts JSON::Validator.fully_validate(SCHEMA_FILE_PATH, @setting, :insert_defaults => true).to_s
  @sites = get_sites_json(SITE_JSON_PATH)
  File.write( SITE_JSON_PATH, JSON.unparse(@sites) ) unless FileTest.exist?(SITE_JSON_PATH)
  puts "uploader valid"
  puts "uploder data: #{get_uploader_data}"   
  puts JSON::Validator.fully_validate(UPLOADER_SCHEMA_FILE_PATH, get_uploader_data, :insert_defaults => true).to_s
end

Instance Method Details

#get_current_access_tokenObject



60
# File 'lib/ruboty/ymcrawl/main.rb', line 60

def get_current_access_token;     get_uploader_data["access_token"]                end

#get_current_app_keyObject



61
# File 'lib/ruboty/ymcrawl/main.rb', line 61

def get_current_app_key;          ENV["#{@setting["save_to"].upcase }_APP_KEY"]    end

#get_current_app_secretObject



62
# File 'lib/ruboty/ymcrawl/main.rb', line 62

def get_current_app_secret;       ENV["#{@setting["save_to"].upcase }_APP_SECRET"] end

#get_current_uploder_info(url) ⇒ Object

URLのドメインに合致するsite情報を返す



42
43
44
45
46
47
# File 'lib/ruboty/ymcrawl/main.rb', line 42

def get_current_uploder_info(url)
  host = URI(url).host
  # ハッシュのkeyがs[0],valueがs[1]に入る
  @sites.each{ |s| return s[1] if s[1]["host"] == host }
  return @sites["default"]
end

#get_save_toObject



58
# File 'lib/ruboty/ymcrawl/main.rb', line 58

def get_save_to;                  @setting["save_to"]                              end

#get_settingObject



57
# File 'lib/ruboty/ymcrawl/main.rb', line 57

def get_setting;                  @setting                                         end

#get_sites_json(path) ⇒ Object

各サイトごとの、画像取得のためのcssセレクタを記載したjsonをファイルから取得して返す



35
36
37
38
39
# File 'lib/ruboty/ymcrawl/main.rb', line 35

def get_sites_json(path)
  path = FileTest.exist?(path) ? path : @setting["site_json"]
  puts "reading site json file from #{path}"
  JSON.parse( open(path).read)
end

#get_uploader_dataObject



59
# File 'lib/ruboty/ymcrawl/main.rb', line 59

def get_uploader_data;            @setting["uploader"][get_save_to]                end

#update_access_token(uploader_name, access_token) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/ruboty/ymcrawl/main.rb', line 49

def update_access_token(uploader_name, access_token)
  @setting["uploader"][uploader_name]["access_token"] = access_token if @setting["uploader"][uploader_name] != access_token
  puts "setting: #{@setting}"
  open(SETTING_FILE_PATH, 'w') do |io|
    JSON.dump(@setting, io)
  end
end