lib/reponaut/github.rb in reponaut-1.2.0 vs lib/reponaut/github.rb in reponaut-2.0.0

- old
+ new

@@ -3,12 +3,10 @@ require 'yaml' if ENV['REPONAUT_ENV'] == 'cucumber' require 'reponaut/ext/bool' module Reponaut module GitHub - class NoSuchUserError < StandardError; end - class Client include HTTParty base_uri 'https://api.github.com' @@ -24,25 +22,37 @@ def to_s username end + def method_missing(symbol, *args) + if self.class.respond_to?(symbol) + self.class.send(symbol, *args) + else + super + end + end + private - def repo_data - return mock_repo_data if ENV['REPONAUT_ENV'] == 'cucumber' - resp = self.class.get("/users/#{username}/repos") + + def real_repo_data + resp = get("/users/#{username}/repos") raise NoSuchUserError, username if resp.code == 404 + raise RateLimitExceededError if resp.code == 403 resp.body end def mock_repo_data - path = File.join(File.dirname(__FILE__), '..', '..', 'spec', 'fixtures', 'cassettes', "#{username}.yml") + raise RateLimitExceededError if ENV['REPONAUT_RATE_LIMIT'] == 'on' + path = File.expand_path("../../../spec/fixtures/cassettes/#{username}.yml", __FILE__) raw_data = IO.read(path) data = YAML.load(raw_data) raise NoSuchUserError, username if data['http_interactions'][0]['response']['status']['code'] == 404 data['http_interactions'][0]['response']['body']['string'] end + + define_method(:repo_data, instance_method(ENV.cucumber? ? :mock_repo_data : :real_repo_data)) end class Repository def initialize(service, data) @service = service @@ -53,11 +63,11 @@ !fork? end def upstream return nil unless fork? - @service.class.get("/repos/#{full_name}")['parent']['full_name'] + repo_data['parent']['full_name'] end def to_s full_name end @@ -80,8 +90,28 @@ else super end end end + + private + + def real_repo_data + @service.get("/repos/#{full_name}") + end + + def mock_repo_data + path = File.expand_path("../../../spec/fixtures/repos/#{full_name}.json", __FILE__) + raw_data = IO.read(path) + JSON.parse(raw_data) + end + + define_method(:repo_data, instance_method(ENV.cucumber? ? :mock_repo_data : :real_repo_data)) end + + class GitHubError < StandardError; end + + class NoSuchUserError < GitHubError; end + + class RateLimitExceededError < GitHubError; end end end