Module: ActAsApiClient::Clients::GithubClient

Includes:
HttpClient
Defined in:
lib/act_as_api_client/clients/github_client.rb

Instance Method Summary collapse

Instance Method Details

#find(repository_name) ⇒ Hash

Searches Github for one repository by it’s owner and repository names. More details look at the corresponding Github Docs page

Parameters:

  • repository_name (String)

    owner and repository name, ‘Rukomoynikov/tabled’

Returns:

  • (Hash)

    detailed info about the repository.



16
17
18
19
20
21
22
23
24
# File 'lib/act_as_api_client/clients/github_client.rb', line 16

def find(repository_name)
  unless repository_name.match?(%r{[a-zA-Z]/[a-zA-Z]})
    raise StandardError, "repository_name parameter is not valid"
  end

  get("https://api.github.com/repos/#{repository_name}",
      headers: { "Accept" => "application/vnd.github.v3+json",
                 "Authorization" => (options[:token] ? "token #{options[:token]}" : nil) })
end

#find_by(options = {}) ⇒ Array

Use this method if you need to get list of repositories selected by one of this conditions: user, authenticated_user, organization

Examples:

Reverse a string

class GithubClient < ApiClient
  act_as_api_client for: :github
end

GithubClient.new.find_by(organization: 'rails')

Parameters:

  • options (Hash) (defaults to: {})

    has to have one of these keys: user, authenticated_user, organization

Returns:

  • (Array)

    list of repositories

Raises:

  • (StandardError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/act_as_api_client/clients/github_client.rb', line 59

def find_by(options = {})
  unless options.is_a?(Hash)
    raise StandardError, "provide a hash as an argument not #{options.class.to_s.downcase}"
  end
  raise StandardError, "provide at least one parameter" if options.keys.length.zero?
  raise StandardError, "method 'find_by' supports only one parameter" if options.keys.length > 1

  url = case options.keys.first
        when :organization
          "https://api.github.com/orgs/#{options[:organization]}/repos"
        when :user
          "https://api.github.com/users/#{options[:user]}/repos"
        when :authenticated_user
          "https://api.github.com/repositories"
        end

  get(url,
      headers: { "Accept" => "application/vnd.github.v3+json",
                 "Authorization" => (options[:token] ? "token #{options[:token]}" : nil) })
end

#where(query_string, parameters = {}) ⇒ Array

Search through Github repositories using query string and additional parameters explained on the Github Docs page

Examples:

Reverse a string

class GithubClient < ApiClient
  act_as_api_client for: :github
end

GithubClient.new.where('rails')
GithubClient.new.where('rails', per_page: 100)

Parameters:

  • query_string (String)

    query string to search github repositories

  • parameters (Hash) (defaults to: {})

    paramaters like per_page, page, sort and order

Returns:

  • (Array)

    list of repositories



40
41
42
43
44
45
# File 'lib/act_as_api_client/clients/github_client.rb', line 40

def where(query_string, parameters = {})
  get("https://api.github.com/search/repositories",
      headers: { "Accept" => "application/vnd.github.v3+json",
                 "Authorization" => (options[:token] ? "token #{options[:token]}" : nil) },
      params: { q: query_string }.merge(parameters))
end