class SocialSnippet::Repository::Drivers::GitHubDriver

Attributes

api_client[R]
github_owner[R]
github_repo[R]

Public Class Methods

is_github_url?(uri) click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 48
def is_github_url?(uri)
  /^git$|^https?$/ === uri.scheme &&
  /^github.com$/ === uri.host &&
  /^.*\/.*$/ === uri.path
end
target_path?(path) click to toggle source

Disable directory path

# File lib/social_snippet/repository/drivers/github_driver.rb, line 55
def target_path?(path)
  false
end
target_url?(url) click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 43
def target_url?(url)
  uri = ::URI.parse(url)
  is_github_url?(uri)
end

Public Instance Methods

each_directory(ref, &block) click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 37
def each_directory(ref, &block)
  directories(ref).each &block
end
each_file(ref, &block) click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 33
def each_file(ref, &block)
  files(ref).each &block
end
fetch() click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 12
def fetch
  @github_owner ||= parse_github_owner
  @github_repo ||= parse_github_repo
  @api_client ||= ::Octokit::Client.new(
    :client_id => GITHUB_CLIENT_ID,
    :client_secret => GITHUB_CLIENT_SECRET,
  )
end
refs() click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 25
def refs
  refs_api.map {|info| info[:ref].gsub /^refs\/[a-z]+\//, "" }
end
rev_hash(ref) click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 29
def rev_hash(ref)
  rev_hash_map[ref]
end
snippet_json(ref = "master") click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 21
def snippet_json(ref = "master")
  ::JSON.parse file(ref, "snippet.json")
end

Private Instance Methods

blob(hash) click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 92
def blob(hash)
  blob = api_client.blob(repo_name, hash)
  if blob.encoding === "base64"
    ::Base64.decode64(blob.content)
  elsif blob.encoding === "utf-8"
    blob.content
  end
end
directories(ref) click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 101
def directories(ref)
  tree(ref).select do |item|
    item.type === "tree"
  end.map do |item|
    Entry.new item.path, nil
  end
end
file(ref, path) click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 87
def file(ref, path)
  res = api_client.contents(repo_name, :ref => ref, :path => path)
  ::Base64.decode64 res.content
end
files(ref) click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 79
def files(ref)
  tree(ref).select do |item|
    item.type === "blob"
  end.map do |item|
    Entry.new item.path, blob(item.sha)
  end
end
parse_github_owner() click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 119
def parse_github_owner
  /^\/(.+)\/.+/.match(::URI.parse(url).path)[1]
end
parse_github_repo() click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 123
def parse_github_repo
  uri_path = ::URI.parse(url).path
  if /\.git$/ === uri_path
    /^.+\/([^\/]+)/.match(uri_path)[1].gsub /\.git$/, ""
  else
    /^.+\/([^\/]+)/.match(uri_path)[1]
  end
end
refs_api() click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 71
def refs_api
  @refs_api ||= api_client.refs("#{github_owner}/#{github_repo}")
end
repo() click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 75
def repo
  @repo ||= api_client.repo(repo_name)
end
repo_name() click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 115
def repo_name
  @repo_name ||= "#{github_owner}/#{github_repo}"
end
rev_hash_map() click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 63
def rev_hash_map
  @rev_hash_map ||= refs_api.inject(::Hash.new) do |hash, info|
    ref = info[:ref].gsub(/^refs\/[a-z]+\//, "")
    hash[ref] = info[:object][:sha]
    hash
  end
end
tree(ref) click to toggle source
# File lib/social_snippet/repository/drivers/github_driver.rb, line 109
def tree(ref)
  tree_hash = rev_hash(ref)
  res = api_client.tree(repo_name, tree_hash, :recursive => true)
  res[:tree]
end