Class: Poke::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/poke/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, name:, alias_name:, group_name:) ⇒ Request

Returns a new instance of Request.



37
38
39
40
41
42
# File 'lib/poke/request.rb', line 37

def initialize(path:, name:, alias_name:, group_name:)
  @path = path
  @name = name
  @alias_name = alias_name
  @group_name = group_name
end

Instance Attribute Details

#alias_nameObject (readonly)

Returns the value of attribute alias_name.



35
36
37
# File 'lib/poke/request.rb', line 35

def alias_name
  @alias_name
end

#group_nameObject (readonly)

Returns the value of attribute group_name.



35
36
37
# File 'lib/poke/request.rb', line 35

def group_name
  @group_name
end

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'lib/poke/request.rb', line 35

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



35
36
37
# File 'lib/poke/request.rb', line 35

def path
  @path
end

Class Method Details

.allObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/poke/request.rb', line 11

def self.all
  @all ||= begin
    request_paths = TTY::Command.new(printer: :null).run("find #{Dir.home}/.poke -name '*.curl'").out.split("\n")

    request_paths.map do |path|
      path = Pathname.new(path)

      group_name = path.to_s.gsub(%r{#{Config.root_path}/([^\/]+)/.*}, '\1')
      name = path.to_s.gsub(%r{#{Config.root_path}/(.*)\.curl}, '\1')
      alias_name = Config.find_alias_by_request_name(name)

      new(group_name:, name:, alias_name:, path:)
    end
  end
end

.find_by_name(name) ⇒ Object



27
28
29
# File 'lib/poke/request.rb', line 27

def self.find_by_name(name)
  all.find { |request| request.name == name }
end

.find_by_name_with_alias(name) ⇒ Object



31
32
33
# File 'lib/poke/request.rb', line 31

def self.find_by_name_with_alias(name)
  all.find { |request| request.name_with_alias == name }
end

Instance Method Details

#groupObject



56
57
58
# File 'lib/poke/request.rb', line 56

def group
  Group.all.find { |g| g.name == group_name }
end

#name_with_aliasObject



44
45
46
# File 'lib/poke/request.rb', line 44

def name_with_alias
  @name_with_alias ||= "#{name}#{alias_name ? " (#{alias_name})" : ''}"
end

#positionObject



48
49
50
# File 'lib/poke/request.rb', line 48

def position
  LastRecentlyUsed.position(namespace: 'requests', key: name.to_s)
end

#use!Object



52
53
54
# File 'lib/poke/request.rb', line 52

def use!
  LastRecentlyUsed.use!(namespace: 'requests', key: name.to_s)
end