Class: ConvenientService::Examples::Rails::V1::Gemfile::Services::ParseContent

Inherits:
Object
  • Object
show all
Includes:
RailsService::Config
Defined in:
lib/convenient_service/examples/rails/v1/gemfile/services/parse_content.rb

Constant Summary collapse

RUBY_REGEX =
/\A\s*ruby/
SOURCE_REGEX =
/\A\s*source/
GIT_SOURCE_REGEX =
/\A\s*git_source/
GROUP_REGEX =
/\A\s*group/
GEM_REGEX =
/\A\s*gem/
BLOCK_END_REGEX =
/\A\s*end/
ENV_SCAN_REGEX =
/:(\w+)/

Instance Method Summary collapse

Instance Method Details

#parse_contentObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/convenient_service/examples/rails/v1/gemfile/services/parse_content.rb', line 55

def parse_content
  hash = ::Hash.new { |h, k| h[k] = [] }

  ::StringIO.open(content) do |io|
    envs = []

    until io.eof?
      line = io.readline.rstrip

      case line
      when RUBY_REGEX
        hash[:ruby] << line
      when SOURCE_REGEX
        hash[:source] << line
      when GIT_SOURCE_REGEX
        hash[:git_source] << line
      when GROUP_REGEX
        envs = line.scan(ENV_SCAN_REGEX).map(&:first).map(&:to_sym)
      when GEM_REGEX
        hash[:gems] << {envs: envs, line: line.lstrip}
      when BLOCK_END_REGEX
        envs = []
      else
        hash[:rest] << line
      end
    end
  end

  hash
end

#resultObject



51
52
53
# File 'lib/convenient_service/examples/rails/v1/gemfile/services/parse_content.rb', line 51

def result
  success(data: {parsed_content: parse_content})
end