Sha256: b38d7da6c0de749bc384d3f15e8653c1802d0955aea69fe57d961decac813846

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

require 'httparty'
require 'json'
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'

      attr_reader :username

      def initialize(username)
        @username = username
      end

      def repos
        JSON.parse(repo_data).map { |e| Repository.new(e) }
      end

      def to_s
        username
      end

      private
        def repo_data
          return mock_repo_data if ENV['REPONAUT_ENV'] == 'cucumber'
          resp = self.class.get("/users/#{username}/repos")
          raise NoSuchUserError, username if resp.code == 404
          resp.body
        end

        def mock_repo_data
          path = File.join(File.dirname(__FILE__), '..', '..', 'spec', 'fixtures', 'cassettes', "#{username}.yml")
          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
    end

    class Repository
      def initialize(data)
        @data = data
      end

      def source?
        !fork?
      end

      def to_s
        full_name
      end

      def method_missing(symbol, *args)
        if @data.include?(symbol.to_s)
          @data[symbol.to_s]
        else
          if symbol.to_s.end_with?('?')
            bool_sym = symbol.to_s.slice(0, symbol.to_s.length-1)
            if @data.include?(bool_sym) && @data[bool_sym].bool?
              @data[bool_sym]
            else
              super
            end
          else
            super
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
reponaut-1.1.1 lib/reponaut/github.rb
reponaut-1.1.0 lib/reponaut/github.rb
reponaut-1.0.1 lib/reponaut/github.rb