class GH::Wrapper

Public: Simple base class for low level layers. Handy if you want to manipulate resources coming in from Github.

Examples

class IndifferentAccess
  def [](key) super.tap { |r| r.data.with_indifferent_access! } end
end

gh = IndifferentAccess.new
gh['users/rkh'][:name] # => "Konstantin Haase"

# easy to use in the low level stack
gh = Github.build do
  use GH::Cache
  use IndifferentAccess
  use GH::Normalizer
end

Attributes

backend[R]

Public: Get wrapped layer.

options[R]

Public: …

Public Class Methods

[](key) click to toggle source

Public: Retrieves resources from Github.

# File lib/gh/wrapper.rb, line 73
def self.[](key)
  new[key]
end
new(backend = nil, options = {}) click to toggle source

Public: Initialize a new Wrapper.

backend - layer to be wrapped options - config options

# File lib/gh/wrapper.rb, line 100
def initialize(backend = nil, options = {})
  backend, @options = normalize_options(backend, options)
  @options.each_pair { |key, value| public_send("#{key}=", value) if respond_to? "#{key}=" }
  setup(backend, @options)
end
wraps(klass = nil) click to toggle source

Internal: Get/set default layer to wrap when creating a new instance.

# File lib/gh/wrapper.rb, line 91
def self.wraps(klass = nil)
  @wraps = klass if klass
  @wraps ||= Remote
end

Private Class Methods

double_dispatch() click to toggle source
# File lib/gh/wrapper.rb, line 153
def self.double_dispatch
  define_method(:modify) { |data| double_dispatch(data) }
end

Public Instance Methods

[](key) click to toggle source

Public: Retrieves resources from Github.

By default, this method is delegated to the next layer on the stack and modify is called.

# File lib/gh/wrapper.rb, line 81
def [](key)
  generate_response key, fetch_resource(key)
end
backend=(layer) click to toggle source

Public: Set wrapped layer.

# File lib/gh/wrapper.rb, line 107
def backend=(layer)
  reset if backend
  layer.frontend = self
  @backend = layer
end
frontend() click to toggle source

Internal: …

# File lib/gh/wrapper.rb, line 119
def frontend
  @frontend ? @frontend.frontend : self
end
frontend=(value) click to toggle source

Internal: …

# File lib/gh/wrapper.rb, line 114
def frontend=(value)
  @frontend = value
end
generate_response(key, resource) click to toggle source

Internal: …

# File lib/gh/wrapper.rb, line 86
def generate_response(key, resource)
  modify backend.generate_response(key, resource)
end
inspect() click to toggle source

Public: …

# File lib/gh/wrapper.rb, line 124
def inspect
  "#<#{self.class}: #{backend.inspect}>"
end
load(data) click to toggle source

Public: …

# File lib/gh/wrapper.rb, line 139
def load(data)
  modify backend.load(data)
end
prefixed(key) click to toggle source

Internal: …

# File lib/gh/wrapper.rb, line 129
def prefixed(key)
  prefix + "#" + identifier(key)
end
reset() click to toggle source

Public: …

# File lib/gh/wrapper.rb, line 134
def reset
  backend.reset if backend
end

Private Instance Methods

double_dispatch(data) click to toggle source
# File lib/gh/wrapper.rb, line 157
def double_dispatch(data)
  case data
  when respond_to(:to_gh)   then modify_response(data)
  when respond_to(:to_hash) then modify_hash(data)
  when respond_to(:to_ary)  then modify_array(data)
  when respond_to(:to_str)  then modify_string(data)
  when respond_to(:to_int)  then modify_integer(data)
  else modify_unknown data
  end
rescue Exception => error
  raise Error.new(error, data)
end
identifier(key) click to toggle source
# File lib/gh/wrapper.rb, line 145
def identifier(key)
  backend.prefixed(key)
end
modify(data, *) click to toggle source
# File lib/gh/wrapper.rb, line 175
def modify(data, *)
  data
rescue Exception => error
  raise Error.new(error, data)
end
modify_array(array) click to toggle source
# File lib/gh/wrapper.rb, line 181
def modify_array(array)
  array.map { |e| modify(e) }
end
modify_hash(hash, &block) click to toggle source
# File lib/gh/wrapper.rb, line 185
def modify_hash(hash, &block)
  corrected = {}
  hash.each_pair { |k,v| corrected[k] = modify(v) }
  corrected.default_proc = hash.default_proc if hash.default_proc
  corrected
end
modify_integer(data, *)
Alias for: modify
modify_response(response) click to toggle source
# File lib/gh/wrapper.rb, line 170
def modify_response(response)
  result = double_dispatch response.data
  result.respond_to?(:to_gh) ? result.to_gh : Response.new(result, response.headers, response.url)
end
modify_string(data, *)
Alias for: modify
modify_unknown(data, *)
Alias for: modify
normalize_options(backend, options) click to toggle source
# File lib/gh/wrapper.rb, line 200
def normalize_options(backend, options)
  backend, options = nil, backend if Hash === backend
  options ||= {}
  backend ||= options[:backend] || options[:api_url] || 'https://api.github.com'
  [backend, options]
end
prefix() click to toggle source
# File lib/gh/wrapper.rb, line 149
def prefix
  self.class.name
end
setup(backend, options) click to toggle source
# File lib/gh/wrapper.rb, line 196
def setup(backend, options)
  self.backend = Wrapper === backend ? backend : self.class.wraps.new(backend, options)
end
setup_default_proc(hash, &block) click to toggle source
# File lib/gh/wrapper.rb, line 207
def setup_default_proc(hash, &block)
  old_proc = hash.default_proc
  hash.default_proc = proc do |hash, key|
    value = old_proc.call(hash, key) if old_proc
    value = block[hash, key] if value.nil?
    value
  end
end
setup_lazy_loading(hash, *args) click to toggle source
# File lib/gh/wrapper.rb, line 216
def setup_lazy_loading(hash, *args)
  loaded = false
  setup_default_proc hash do |hash, key|
    next if loaded
    fields = lazy_load(hash, key, *args)
    if fields
      modify_hash fields
      hash.merge! fields
      loaded = true
      fields[key]
    end
  end
  hash
end