class GistUpdater::Content

A content related to a gist file

Attributes

access_token[R]
file_path[R]
gist_id[R]
user[R]

Public Class Methods

new(user:, access_token:, gist_id:, file_path:) click to toggle source

@param user [String] GitHub username @param access_token [String] GitHub personal access token @param gist_id [String] A gist id @param file_path [String] A relative file path

# File lib/gist_updater/content.rb, line 12
def initialize(user:, access_token:, gist_id:, file_path:)
  @user         = user
  @access_token = access_token
  @gist_id      = gist_id
  @file_path    = file_path
end

Public Instance Methods

update_if_need() click to toggle source

Update the content if needed

@return [Sawyer::Resource] an updated resource @return [NilClass] isnot updated

# File lib/gist_updater/content.rb, line 23
def update_if_need
  if need_to_update?
    resource = update
    puts "Updated `#{file_path}` to #{resource.html_url}"
  elsif GistUpdater.debug
    puts "There is no update for `#{file_path}`."
  end

  pick_a_file(resource)
end

Private Instance Methods

client() click to toggle source
# File lib/gist_updater/content.rb, line 38
def client
  @client ||= Octokit::Client.new(login: user, access_token: access_token)
end
file_name() click to toggle source
# File lib/gist_updater/content.rb, line 69
def file_name
  @file_name ||= File.basename(file_path)
end
gist() click to toggle source
# File lib/gist_updater/content.rb, line 46
def gist
  @gist ||= client.gist(gist_id).files[file_name].content
end
local() click to toggle source
# File lib/gist_updater/content.rb, line 50
def local
  @local ||= File.read(file_path)
end
need_to_update?() click to toggle source
# File lib/gist_updater/content.rb, line 42
def need_to_update?
  @need_to_update ||= gist != local
end
pick_a_file(resource) click to toggle source
# File lib/gist_updater/content.rb, line 54
def pick_a_file(resource)
  files = resource&.files
  files[file_name] if files
end
update() click to toggle source

Update a Gist file

@return [Sawyer::Resource]

# File lib/gist_updater/content.rb, line 62
def update
  client.edit_gist(
    gist_id,
    files: { file_name => { 'content' => local } }
  )
end