Module: Octokit::Client::Objects

Included in:
Octokit::Client
Defined in:
lib/octokit/client/objects.rb

Instance Method Summary collapse

Instance Method Details

#blob(repo, blob_sha, options = {}) ⇒ Hashie::Mash

Get a single blob, fetching its content and encoding

Examples:

Fetch a blob and inspect its contents

blob = Octokit.blob("octocat/Hello-World", "827efc6d56897b048c772eb4087f854f46256132")
blob.encoding # => "utf-8"
blob.content # => "Foo bar baz"

Fetch a base64-encoded blob and inspect its contents

require "base64"
blob = Octokit.blob("octocat/Hello-World", "827efc6d56897b048c772eb4087f854f46256132")
blob.encoding # => "base64"
blob.content # => "Rm9vIGJhciBiYXo="
Base64.decode64(blob.content) # => "Foo bar baz"

Parameters:

  • repo (String, Hash, Repository)

    A GitHub repository

  • blob_sha (String)

    The SHA of the blob to fetch

Returns:

  • (Hashie::Mash)

    A hash representing the fetched blob

See Also:



55
56
57
# File 'lib/octokit/client/objects.rb', line 55

def blob(repo, blob_sha, options={})
  get("repos/#{Repository.new(repo)}/git/blobs/#{blob_sha}", options)
end

#create_blob(repo, content, encoding = "utf-8", options = {}) ⇒ String

Create a blob

Examples:

Create a blob containing foo bar baz

Octokit.create_blob("octocat/Hello-World", "foo bar baz")

Create a blob containing foo bar baz, encoded using base64

require "base64"
Octokit.create_blob("octocat/Hello-World", Base64.encode64("foo bar baz"), "base64")

Parameters:

  • repo (String, Hash, Repository)

    A GitHub repository

  • content (String)

    Content of the blob

  • encoding (String) (defaults to: "utf-8")

    The content’s encoding. utf-8 and base64 are accepted. If your data cannot be losslessly sent as a UTF-8 string, you can base64 encode it

Returns:

  • (String)

    The new blob’s SHA, e.g. 827efc6d56897b048c772eb4087f854f46256132

See Also:



71
72
73
74
75
76
77
# File 'lib/octokit/client/objects.rb', line 71

def create_blob(repo, content, encoding="utf-8", options={})
  parameters = {
    :content => content,
    :encoding => encoding
  }
  post("repos/#{Repository.new(repo)}/git/blobs", options.merge(parameters), 3).sha
end

#create_tree(repo, tree, options = {}) ⇒ Hashie::Mash

Create a tree

Pass :base_tree => "827efc6..." in options to update an existing tree with new data.

Examples:

Create a tree containing one file

tree = Octokit.create_tree("octocat/Hello-World", [ { :path => "file.rb", :mode => "100644", :type => "blob", :sha => "44b4fc6d56897b048c772eb4087f854f46256132" } ])
tree.sha # => "cd8274d15fa3ae2ab983129fb037999f264ba9a7"
tree.tree.first.path # => "file.rb"

Parameters:

  • repo (String, Hash, Repository)

    A GitHub repository

  • tree (Array)

    An array of hashes representing a tree structure

Returns:

  • (Hashie::Mash)

    A hash representing the new tree

See Also:



34
35
36
37
# File 'lib/octokit/client/objects.rb', line 34

def create_tree(repo, tree, options={})
  parameters = { :tree => tree }
  post("repos/#{Repository.new(repo)}/git/trees", options.merge(parameters), 3)
end

#tree(repo, tree_sha, options = {}) ⇒ Hashie::Mash

Get a single tree, fetching information about its root-level objects

Pass :recursive => true in options to fetch information about all of the tree’s objects, including those in subdirectories.

Examples:

Fetch a tree and inspect the path of one of its files

tree = Octokit.tree("octocat/Hello-World", "9fb037999f264ba9a7fc6274d15fa3ae2ab98312")
tree.tree.first.path # => "file.rb"

Fetch a tree recursively

tree = Octokit.tree("octocat/Hello-World", "fc6274d15fa3ae2ab983129fb037999f264ba9a7")
tree.tree.first.path # => "subdir/file.txt"

Parameters:

  • repo (String, Hash, Repository)

    A GitHub repository

  • tree_sha (String)

    The SHA of the tree to fetch

Returns:

  • (Hashie::Mash)

    A hash representing the fetched tree

See Also:



18
19
20
# File 'lib/octokit/client/objects.rb', line 18

def tree(repo, tree_sha, options={})
  get("repos/#{Repository.new(repo)}/git/trees/#{tree_sha}", options)
end