Class: GhSearch::GithubClient

Inherits:
Object
  • Object
show all
Defined in:
lib/gh_search/github_client.rb

Constant Summary collapse

GITHUB_URL =
"https://api.github.com"
ENDPOINT =
"/search/code"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_text, options = {}) ⇒ GithubClient

Returns a new instance of GithubClient.



19
20
21
22
23
24
# File 'lib/gh_search/github_client.rb', line 19

def initialize(search_text, options={})
  @search_text = search_text
  @org = options[:org]
  @filename = options[:file]
  @pagenum = 1
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



15
16
17
# File 'lib/gh_search/github_client.rb', line 15

def filename
  @filename
end

#orgObject (readonly)

Returns the value of attribute org.



15
16
17
# File 'lib/gh_search/github_client.rb', line 15

def org
  @org
end

#pagenumObject

Returns the value of attribute pagenum.



17
18
19
# File 'lib/gh_search/github_client.rb', line 17

def pagenum
  @pagenum
end

#search_textObject (readonly)

Returns the value of attribute search_text.



15
16
17
# File 'lib/gh_search/github_client.rb', line 15

def search_text
  @search_text
end

Instance Method Details

#eachObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gh_search/github_client.rb', line 26

def each
  has_results = true

  while has_results do
    json_response = make_request(uri)['items']

    if has_results = json_response.any?
      json_response.each do |item|
        each_match(URI(item['url'])) do |match|

          result = match.merge({
            repo_name: item['repository']['full_name'],
            html_url: item['html_url'],
          })

          yield result
        end
      end
    end

    @pagenum += 1
  end
end