Sha256: ca111c2db1e9502d3e54f7f62d20c738cb8d8cbe0d4733b110d090634825f22d

Contents?: true

Size: 1.2 KB

Versions: 27

Compression:

Stored size: 1.2 KB

Contents

require 'gh'

module GH
  # Public: ...
  class NestedResources < Wrapper
    wraps GH::Normalizer
    double_dispatch

    def modify_hash(hash, loaded = false)
      hash = super(hash)
      link = hash['_links'].try(:[], 'self') unless loaded
      set_links hash, Addressable::URI.parse(link['href']) if link
      hash
    end

    def add(hash, link, name, path = name)
      hash["_links"][name] ||= { "href" => nested(link, path) }
    end

    def nested(link, path)
      new_link = link.dup
      if path.start_with? '/'
        new_link.path = path
      else
        new_link.path += path
      end
      new_link
    end

    def set_links(hash, link)
      case link.path
      when '/gists'
        add hash, link, 'public'
        add hash, link, 'starred'
      when %r{^/repos/[^/]+/[^/]+$}
        add hash, link, 'commits', 'git/commits'
        add hash, link, 'refs',    'git/refs'
        add hash, link, 'tags',    'git/tags'
        add hash, link, 'issues'
      when %r{^/repos/[^/]+/[^/]+/issues/\d+$}
        add hash, link, 'comments'
        add hash, link, 'events'
      when '/user'
        add hash, link, 'gists',  '/gists'
        add hash, link, 'issues', '/issues'
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
gh-0.13.3 lib/gh/nested_resources.rb
gh-0.13.2 lib/gh/nested_resources.rb
gh-0.13.1 lib/gh/nested_resources.rb
gh-0.13.0 lib/gh/nested_resources.rb
gh-0.12.4 lib/gh/nested_resources.rb
gh-0.12.3 lib/gh/nested_resources.rb
gh-0.12.2 lib/gh/nested_resources.rb
gh-0.12.1 lib/gh/nested_resources.rb
gh-0.12.0 lib/gh/nested_resources.rb
gh-0.11.3 lib/gh/nested_resources.rb
gh-0.11.2 lib/gh/nested_resources.rb
gh-0.11.1 lib/gh/nested_resources.rb
gh-0.11.0 lib/gh/nested_resources.rb
gh-0.10.3 lib/gh/nested_resources.rb
gh-0.10.2 lib/gh/nested_resources.rb
gh-0.10.1 lib/gh/nested_resources.rb
gh-0.10.0 lib/gh/nested_resources.rb
gh-0.9.2 lib/gh/nested_resources.rb
gh-0.9.1 lib/gh/nested_resources.rb
gh-0.9.0 lib/gh/nested_resources.rb