Class: Gitlab::GithubGistsImport::Representation::Gist

Inherits:
Object
  • Object
show all
Includes:
Gitlab::GithubImport::Representation::ExposeAttribute, Gitlab::GithubImport::Representation::ToHash
Defined in:
lib/gitlab/github_gists_import/representation/gist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::GithubImport::Representation::ExposeAttribute

#[]

Methods included from Gitlab::GithubImport::Representation::ToHash

#convert_value_for_to_hash, #to_hash

Constructor Details

#initialize(attributes) ⇒ Gist

attributes - A hash containing the raw gist details. The keys of this

Hash (and any nested hashes) must be symbols.


39
40
41
# File 'lib/gitlab/github_gists_import/representation/gist.rb', line 39

def initialize(attributes)
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



10
11
12
# File 'lib/gitlab/github_gists_import/representation/gist.rb', line 10

def attributes
  @attributes
end

Class Method Details

.from_api_response(gist, additional_data = {}) ⇒ Object

Builds a gist from a GitHub API response.

gist - An instance of ‘Hash` containing the gist

details.


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gitlab/github_gists_import/representation/gist.rb', line 18

def self.from_api_response(gist, additional_data = {})
  hash = {
    id: gist[:id],
    description: gist[:description],
    is_public: gist[:public],
    files: gist[:files],
    git_pull_url: gist[:git_pull_url],
    created_at: gist[:created_at],
    updated_at: gist[:updated_at]
  }

  new(hash)
end

.from_json_hash(raw_hash) ⇒ Object

Builds a new gist using a Hash that was built from a JSON payload.



33
34
35
# File 'lib/gitlab/github_gists_import/representation/gist.rb', line 33

def self.from_json_hash(raw_hash)
  new(Gitlab::GithubImport::Representation.symbolize_hash(raw_hash))
end

Instance Method Details

#first_fileObject



56
57
58
59
60
61
62
63
# File 'lib/gitlab/github_gists_import/representation/gist.rb', line 56

def first_file
  _key, value = files.first

  {
    file_name: value[:filename],
    file_content: Gitlab::HTTP.try_get(value[:raw_url])&.body
  }
end

#github_identifiersObject



65
66
67
# File 'lib/gitlab/github_gists_import/representation/gist.rb', line 65

def github_identifiers
  { id: id }
end

#total_files_sizeObject



69
70
71
# File 'lib/gitlab/github_gists_import/representation/gist.rb', line 69

def total_files_size
  files.values.sum { |f| f[:size].to_i }
end

#truncated_titleObject

Gist description can be an empty string, so we returning nil to use first file name as a title in such case on snippet creation Gist description has a limit of 256, while the snippet’s title can be up to 255



46
47
48
49
50
# File 'lib/gitlab/github_gists_import/representation/gist.rb', line 46

def truncated_title
  title = description.presence || first_file[:file_name]

  title.truncate(255)
end

#visibility_levelObject



52
53
54
# File 'lib/gitlab/github_gists_import/representation/gist.rb', line 52

def visibility_level
  is_public ? Gitlab::VisibilityLevel::PUBLIC : Gitlab::VisibilityLevel::PRIVATE
end