module ContributorsStats::JsonHelper
Support code for loading json urls/files
Public Instance Methods
load_json(url)
click to toggle source
Load json from url, fallback to prefix
# File lib/contributors_stats/json_helper.rb, line 15 def load_json(url) open(url){ |json| JSON.load(json) } rescue Exception => e if File.exist?("#{self.path_prefix}#{url}") open("#{self.path_prefix}#{url}"){ |json| JSON.load(json) } elsif File.exist?("#{self.path_prefix}#{url}#{self.path_suffix}") open("#{self.path_prefix}#{url}#{self.path_suffix}"){ |json| JSON.load(json) } else raise e end end
path_prefix()
click to toggle source
get prefix, sets the default if empty, makes sure it’s ending with ‘/’
# File lib/contributors_stats/json_helper.rb, line 28 def path_prefix @path_prefix ||= "https://api.github.com" @path_prefix+="/" if @path_prefix != "" && @path_prefix[-1] != "/" @path_prefix end
path_suffix()
click to toggle source
get suffix, sets the default if empty, makes sure it’s starts with ‘.’
# File lib/contributors_stats/json_helper.rb, line 35 def path_suffix @path_suffix ||= "" @path_suffix = ".#{@path_suffix}" if @path_suffix != "" && @path_suffix[0] != "." @path_suffix end
url_builder(path)
click to toggle source
Build full path to resource to use
# File lib/contributors_stats/json_helper.rb, line 10 def url_builder(path) "#{self.path_prefix}#{path}#{self.path_suffix}" end
Protected Instance Methods
configure_path(prefix, suffix)
click to toggle source
# File lib/contributors_stats/json_helper.rb, line 43 def configure_path(prefix, suffix) @path_prefix = prefix @path_suffix = suffix end