Class: Gitlab::GithubGistsImport::Representation::Gist
- Inherits:
-
Object
- Object
- Gitlab::GithubGistsImport::Representation::Gist
- Includes:
- Gitlab::GithubImport::Representation::ExposeAttribute, Gitlab::GithubImport::Representation::ToHash
- Defined in:
- lib/gitlab/github_gists_import/representation/gist.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
-
.from_api_response(gist, additional_data = {}) ⇒ Object
Builds a gist from a GitHub API response.
-
.from_json_hash(raw_hash) ⇒ Object
Builds a new gist using a Hash that was built from a JSON payload.
Instance Method Summary collapse
- #first_file ⇒ Object
- #github_identifiers ⇒ Object
-
#initialize(attributes) ⇒ Gist
constructor
attributes - A hash containing the raw gist details.
- #total_files_size ⇒ Object
-
#truncated_title ⇒ Object
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.
- #visibility_level ⇒ Object
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
#attributes ⇒ Object (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_file ⇒ Object
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_identifiers ⇒ Object
65 66 67 |
# File 'lib/gitlab/github_gists_import/representation/gist.rb', line 65 def github_identifiers { id: id } end |
#total_files_size ⇒ Object
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_title ⇒ Object
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_level ⇒ Object
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 |