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

Class Method Summary collapse

Class Method Details

.branches(url) ⇒ Array

Returns the branches of a repo.

Parameters:

  • url (String)

    @see #repo

Returns:

  • (Array)

    The list of the branches.


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.

Parameters:

  • url (String)

    @see #repo

  • path (#to_s) (defaults to: nil)

    The path for which the contents are needed.

  • branch (String) (defaults to: nil)

    The branch for which to fetch the contents of the path.

Returns:

  • (Array)

    The list of the files and of the directories if the given path is a directory.

  • (Hash)

    The contents of the file (usually base64 encoded).


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

.normalized_repo_id(url_or_id) ⇒ String (private)

Returns the repo ID as it is or converting a GitHub URL.

Parameters:

  • url_or_id (String)

    A repo ID or the URL of the repo.

Returns:

  • (String)

    the repo ID.


91
92
93
# File 'lib/cocoapods-core/github.rb', line 91

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.

Parameters:

  • url (String)

    The URL of the resource.

Returns:

  • (Array, Hash)

    The information of the resource as Ruby objects.


114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cocoapods-core/github.rb', line 114

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.

Parameters:

  • url (String)

    The URL of the repo.

Returns:

  • (Hash)

    The hash containing the data as reported by GitHub.


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.

Parameters:

  • url (String)

    The URL of the repo.

Returns:

  • (String)

    the repo ID.

  • (Nil)

    if the given url is not a valid github repo url.


103
104
105
# File 'lib/cocoapods-core/github.rb', line 103

def self.repo_id_from_url(url)
  url[%r{github.com/([^/]*/[^/]*)\.*}, 1]
end

.tags(url) ⇒ Array

Returns the tags of a repo.

Parameters:

  • url (String)

    @see #repo

Returns:

  • (Array)

    The list of the tags.


37
38
39
40
41
# File 'lib/cocoapods-core/github.rb', line 37

def self.tags(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.

Parameters:

  • login (String)

    The name of the user.

Returns:

  • (Hash)

    The data of user.


14
15
16
# File 'lib/cocoapods-core/github.rb', line 14

def self.user()
  peform_request("https://api.github.com/users/#{}")
end