Sha256: 0181db10e4ac24768ace4a195d74202413ab9b2d11f29eb7cfa7c2c357f12126
Contents?: true
Size: 1.3 KB
Versions: 1
Compression:
Stored size: 1.3 KB
Contents
require 'json' require 'open-uri' module ContributorsStats # Support code for loading json urls/files module JsonHelper # Build full path to resource to use def url_builder(path) "#{self.path_prefix}#{path}#{self.path_suffix}" end # Load json from url, fallback to prefix 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 # get prefix, sets the default if empty, makes sure it's ending with '/' def path_prefix @path_prefix ||= "https://api.github.com" @path_prefix+="/" if @path_prefix != "" && @path_prefix[-1] != "/" @path_prefix end # get suffix, sets the default if empty, makes sure it's starts with '.' def path_suffix @path_suffix ||= "" @path_suffix = ".#{@path_suffix}" if @path_suffix != "" && @path_suffix[0] != "." @path_suffix end protected def configure_path(prefix, suffix) @path_prefix = prefix @path_suffix = suffix end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
contributors_stats-1.0.0 | lib/contributors_stats/json_helper.rb |