Class: SocialSnippet::RegistryCore::Fetcher::GitHubFetcher

Inherits:
FetcherBase
  • Object
show all
Defined in:
lib/social_snippet/registry_core/fetcher/github_fetcher.rb

Constant Summary collapse

SNIPPET_JSON_NAME =
"snippet.json"

Instance Method Summary collapse

Methods inherited from FetcherBase

#latest_version, #versions

Instance Method Details

#clientObject



35
36
37
38
39
40
# File 'lib/social_snippet/registry_core/fetcher/github_fetcher.rb', line 35

def client
  @client ||= ::Octokit::Client.new(
    :client_id => ENV["SSPM_GITHUB_CLIENT_ID"],
    :client_secret => ENV["SSPM_GITHUB_CLIENT_SECRET"],
  )
end

#is_github?(uri) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/social_snippet/registry_core/fetcher/github_fetcher.rb', line 16

def is_github?(uri)
  return uri.scheme === "git" &&
    uri.host === "github.com" &&
    /\/[a-z0-9\-]+\/[a-z0-9\-\.\_]+\.git/ === uri.path
end

#parse_url(url) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/social_snippet/registry_core/fetcher/github_fetcher.rb', line 22

def parse_url(url)
  uri = URI.parse(url)
  if is_github?(uri)
    matches = /\/([a-z0-9\-]+)\/([a-z0-9\-\.\_]+)\.git/.match(uri.path)
    return {
      :owner_id => matches[1],
      :repo_id => matches[2],
    }
  else
    raise "should be passed GitHub URL"
  end
end

#refs_by(owner_id, repo_id) ⇒ Object



61
62
63
64
65
# File 'lib/social_snippet/registry_core/fetcher/github_fetcher.rb', line 61

def refs_by(owner_id, repo_id)
  client.refs("#{owner_id}/#{repo_id}")
    .map {|ref_info| ref_info[:ref] }
    .map {|ref| ref.gsub /^refs\/[a-z]+\//, "" }
end

#snippet_json(url) ⇒ Object



11
12
13
14
# File 'lib/social_snippet/registry_core/fetcher/github_fetcher.rb', line 11

def snippet_json(url)
  info = parse_url(url)
  snippet_json_by info[:owner_id], info[:repo_id]
end

#snippet_json_by(owner_id, repo_id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/social_snippet/registry_core/fetcher/github_fetcher.rb', line 42

def snippet_json_by(owner_id, repo_id)
  begin
    opts = {}
    opts[:path] = SNIPPET_JSON_NAME

    # resolve version
    vers = versions(owner_id, repo_id)
    unless vers.empty?
      opts[:ref] = latest_version(vers)
    end

    contents_info = client.contents("#{owner_id}/#{repo_id}", opts)
    decoded_content = ::Base64.decode64(contents_info[:content])
    return ::JSON.parse(decoded_content)
  rescue ::Octokit::NotFound => error
    raise "not found"
  end
end