Module: Pod::GitHub
- Defined in:
- lib/cocoapods-core/github.rb
Overview
Allows to access information about the GitHub repos.
This class is stored in Core because it might be used by web services.
Private helpers collapse
-
.normalized_repo_id(url_or_id) ⇒ String
private
Returns the repo ID as it is or converting a GitHub URL.
-
.peform_request(url) ⇒ Array, Hash
private
Performs a get request with the given URL.
-
.repo_id_from_url(url) ⇒ String, Nil
private
Returns the repo ID given it's URL.
Class Method Summary collapse
-
.branches(url) ⇒ Array
Returns the branches of a repo.
-
.contents(url, path = nil, branch = nil) ⇒ Array, Hash
Returns the contents of a file or directory in a repository.
-
.modified_since_commit(url, commit) ⇒ Boolean
Returns whether the repository has been updated since a given commit.
-
.repo(url) ⇒ Hash
Returns the information of a repo.
-
.tags(url) ⇒ Array
Returns the tags of a repo.
-
.user(login) ⇒ Hash
Returns the information of a user.
Class Method Details
.branches(url) ⇒ Array
Returns the branches of a repo.
49 50 51 52 53 |
# File 'lib/cocoapods-core/github.rb', line 49 def self.branches(url) if repo_id = normalized_repo_id(url) peform_request("https://api.github.com/repos/#{repo_id}/branches") end end |
.contents(url, path = nil, branch = nil) ⇒ Array, Hash
Returns the contents of a file or directory in a repository.
70 71 72 73 74 75 76 77 |
# File 'lib/cocoapods-core/github.rb', line 70 def self.contents(url, path = nil, branch = nil) if repo_id = normalized_repo_id(url) request_url = "https://api.github.com/repos/#{repo_id}/contents" request_url << "/#{path}" if path request_url << "?ref=#{branch}" if branch peform_request(request_url) end end |
.modified_since_commit(url, commit) ⇒ Boolean
Returns whether the repository has been updated since a given commit. If the request fails, the response will be true as the API is still in beta and likely to change.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cocoapods-core/github.rb', line 90 def self.modified_since_commit(url, commit) return true unless repo_id = normalized_repo_id(url) require 'rest' request_url = "https://api.github.com/repos/#{repo_id}/commits/master" headers = { 'User-Agent' => 'CocoaPods', 'Accept' => 'application/vnd.github.v3.sha', 'If-None-Match' => %("#{commit}"), } begin response = REST.get(request_url, headers) code = response.status_code code != 304 rescue raise Informative, "Failed to connect to GitHub to update the #{repo_id} specs repo - Please check if you are offline, or that GitHub is down" end end |
.normalized_repo_id(url_or_id) ⇒ String (private)
Returns the repo ID as it is or converting a GitHub URL.
121 122 123 |
# File 'lib/cocoapods-core/github.rb', line 121 def self.normalized_repo_id(url_or_id) repo_id_from_url(url_or_id) || url_or_id end |
.peform_request(url) ⇒ Array, Hash (private)
Performs a get request with the given URL.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cocoapods-core/github.rb', line 144 def self.peform_request(url) require 'rest' require 'json' headers = { 'User-Agent' => 'CocoaPods' } response = REST.get(url, headers) body = JSON.parse(response.body) if response.ok? body else CoreUI.warn "Request to #{url} failed - #{response.status_code}" CoreUI.warn body['message'] nil end end |
.repo(url) ⇒ Hash
Returns the information of a repo.
25 26 27 28 29 |
# File 'lib/cocoapods-core/github.rb', line 25 def self.repo(url) if repo_id = normalized_repo_id(url) peform_request("https://api.github.com/repos/#{repo_id}") end end |
.repo_id_from_url(url) ⇒ String, Nil (private)
Returns the repo ID given it's URL.
133 134 135 |
# File 'lib/cocoapods-core/github.rb', line 133 def self.repo_id_from_url(url) url[%r{github.com[/:]([^/]*/(?:(?!\.git)[^/])*)\.*}, 1] end |
.tags(url) ⇒ Array
Returns the tags of a repo.
37 38 39 40 41 |
# File 'lib/cocoapods-core/github.rb', line 37 def self.(url) if repo_id = normalized_repo_id(url) peform_request("https://api.github.com/repos/#{repo_id}/tags") end end |
.user(login) ⇒ Hash
Returns the information of a user.
14 15 16 |
# File 'lib/cocoapods-core/github.rb', line 14 def self.user(login) peform_request("https://api.github.com/users/#{login}") end |