Class: DocRepo::GithubFile

Inherits:
Object
  • Object
show all
Defined in:
lib/doc_repo/github_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, org: DocRepo.configuration.org, repo: DocRepo.configuration.repo, branch: DocRepo.configuration.branch) ⇒ GithubFile



7
8
9
10
11
12
13
14
15
# File 'lib/doc_repo/github_file.rb', line 7

def initialize(file,
               org: DocRepo.configuration.org,
               repo: DocRepo.configuration.repo,
               branch: DocRepo.configuration.branch)
  @file = file
  @org = org
  @repo = repo
  @branch = branch
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



6
7
8
# File 'lib/doc_repo/github_file.rb', line 6

def branch
  @branch
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/doc_repo/github_file.rb', line 6

def file
  @file
end

#orgObject (readonly)

Returns the value of attribute org.



6
7
8
# File 'lib/doc_repo/github_file.rb', line 6

def org
  @org
end

#repoObject (readonly)

Returns the value of attribute repo.



6
7
8
# File 'lib/doc_repo/github_file.rb', line 6

def repo
  @repo
end

Instance Method Details

#headersObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/doc_repo/github_file.rb', line 27

def headers
  hash = {
    "Accept"  => "application/vnd.github.v3.raw",
    "User-Agent" => "RadiusNetworks-ProximityKit",
  }

  if ENV["GITHUB_TOKEN"]
    hash["Authorization"] = "token #{ENV["GITHUB_TOKEN"]}"
  end

  hash
end

#read_remote_fileObject



21
22
23
24
25
# File 'lib/doc_repo/github_file.rb', line 21

def read_remote_file
  open(url(file), headers).read
rescue OpenURI::HTTPError => http_error
  raise DocRepo::NotFound.new(base: http_error)
end

#redirect_urlObject



17
18
19
# File 'lib/doc_repo/github_file.rb', line 17

def redirect_url
  "https://github.com/#{org}/#{repo}/raw/#{branch}/docs/#{file}"
end

#url(file) ⇒ Object



40
41
42
# File 'lib/doc_repo/github_file.rb', line 40

def url(file)
  "https://api.github.com/repos/#{org}/#{repo}/contents/docs/#{file}?ref=#{branch}"
end