Class: GithubStats::User

Inherits:
Object
  • Object
show all
Includes:
MethodCacher
Defined in:
lib/githubstats.rb

Overview

User object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ User

Creates a new user object


49
50
51
52
53
54
55
# File 'lib/githubstats.rb', line 49

def initialize(params = {})
  params = { name: params } unless params.is_a? Hash
  @name = params[:name] || guess_user
  @url = (params[:url] || DEFAULT_URL) % @name
  @last_updated = nil
  enable_caching %i[streak longest_streak streaks]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)


154
155
156
157
158
159
# File 'lib/githubstats.rb', line 154

def method_missing(sym, *args, &block)
  load_data if @last_updated.nil?
  return super unless @data.respond_to? sym
  define_singleton_method(sym) { |*a, &b| @data.send(sym, *a, &b) }
  send(sym, *args, &block)
end

Instance Attribute Details

#last_updatedObject (readonly)

Returns the value of attribute last_updated


44
45
46
# File 'lib/githubstats.rb', line 44

def last_updated
  @last_updated
end

#nameObject (readonly)

Returns the value of attribute name


44
45
46
# File 'lib/githubstats.rb', line 44

def name
  @name
end

#urlObject (readonly)

Returns the value of attribute url


44
45
46
# File 'lib/githubstats.rb', line 44

def url
  @url
end

Instance Method Details

#data(reload = false) ⇒ Object

Lazy loader for data


91
92
93
94
# File 'lib/githubstats.rb', line 91

def data(reload = false)
  load_data if reload == true || @last_updated.nil?
  @data
end

#longest_streakObject

Set a custom longest_streak to account for the overriden streak


83
84
85
86
# File 'lib/githubstats.rb', line 83

def longest_streak
  return data.longest_streak if data.longest_streak.size < 364
  streak
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Adjust respond_to? to properly respond with patched method_missing

Returns:

  • (Boolean)

99
100
101
102
# File 'lib/githubstats.rb', line 99

def respond_to_missing?(method, include_private = false)
  load_data if @last_updated.nil?
  super || @data.respond_to?(method, include_private)
end

#streakObject

Set a custom streaks value that takes into account GitHub, which makes available streak data for longer than a year


69
70
71
72
# File 'lib/githubstats.rb', line 69

def streak
  return [] if streaks.empty?
  streaks.last.last.date >= Date.today - 1 ? streaks.last : []
end

#streaksObject


74
75
76
77
78
# File 'lib/githubstats.rb', line 74

def streaks
  naive = data.streaks
  return naive if naive.last.nil? || naive.last.size < 364
  [real_streak]
end

#to_sObject Also known as: inspect

Print human-readable string about object


60
61
62
# File 'lib/githubstats.rb', line 60

def to_s
  "Contributions from #{@name}"
end