Module: ContributorsStats::JsonHelper

Included in:
Reader, Reader::GhOrg, Reader::GhRepo, UserData::Fetch, ExampleData
Defined in:
lib/contributors_stats/json_helper.rb

Overview

Support code for loading json urls/files

Instance Method Summary collapse

Instance Method Details

#load_json(url) ⇒ Object

Load json from url, fallback to prefix



15
16
17
18
19
20
21
22
23
24
25
# 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_prefixObject

get prefix, sets the default if empty, makes sure it’s ending with ‘/’



28
29
30
31
32
# 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_suffixObject

get suffix, sets the default if empty, makes sure it’s starts with ‘.’



35
36
37
38
39
# 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) ⇒ Object

Build full path to resource to use



10
11
12
# File 'lib/contributors_stats/json_helper.rb', line 10

def url_builder(path)
  "#{self.path_prefix}#{path}#{self.path_suffix}"
end