lib/gist_updater/content.rb in gist_updater-0.1.0 vs lib/gist_updater/content.rb in gist_updater-0.2.0

- old
+ new

@@ -2,39 +2,55 @@ require 'octokit' module GistUpdater class Content - attr_reader :name + def initialize(user:, access_token:, gist_id:, file_path:) + @user = user + @access_token = access_token + @gist_id = gist_id + @file_path = file_path + end - def initialize(user:, access_token:, gist_id:, file_name:) - @client = Octokit::Client.new( - login: user, - access_token: access_token - ) - @gist_id = gist_id - @name = file_name - @basename = File.basename(file_name) + def update_if_need + if need_to_update? + result = update + puts "Updated `#{file_path}` to #{result.html_url}" + elsif GistUpdater.debug + puts "There was no need to update `#{file_path}`." + end + + result end + private + + attr_reader :user, :access_token, :gist_id, :file_path + + def client + @client ||= Octokit::Client.new(login: user, access_token: access_token) + end + + def need_to_update? + @need_to_update ||= gist != local + end + def gist - @gist ||= client.gist(gist_id).files[basename].content + @gist ||= client.gist(gist_id).files[file_name].content end def local - @local ||= File.read(name) + @local ||= File.read(file_path) end def update - result = client.edit_gist( + client.edit_gist( gist_id, - files: { basename => { 'content' => local } } + files: { file_name => { 'content' => local } } ) - puts "Updated `#{name}` to #{result.html_url}" - result end - private - - attr_reader :client, :gist_id, :basename + def file_name + @file_name ||= File.basename(file_path) + end end end