Class: Acter::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/acter/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, schema_data) ⇒ Action

Returns a new instance of Action.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/acter/action.rb', line 10

def initialize(args, schema_data)
  args = args.dup
  @subject, @name = args.shift(2)

  @params = {}
  @headers = {}
  args.each_with_index do |arg, idx|
    case
    when /^(?<key>[^:=]+):(?<value>.*)/ =~ arg
      warn "Value of header #{key.inspect} is empty" if value.empty?
      @headers[key] = value
    when /^(?<key>[^:=]+)=(?<value>.*)/ =~ arg
      @params[key] = try_json_value(value)
    when idx.zero?
      @params[@subject] = try_json_value(arg)
    else
      raise ArgumentError, arg.inspect
    end
  end

  @schema, errors = JsonSchema.parse(schema_data)
  @schema or raise InvalidSchema.new("JSON schema parsing failed", errors)
  result, errors = @schema.expand_references
  result or raise InvalidSchema.new("JSON schema reference expansion failed", errors)

  @base_url = @schema.links.find do |li|
    li.href && li.rel == "self"
  end.try(:href)
  @base_url or raise InvalidSchema, "Schema has no valid link to self"

  validate_link!
  if Acter.help_wanted?
    raise HelpWanted.new(@name, @subject, @schema)
  end
  validate_params!
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



47
48
49
# File 'lib/acter/action.rb', line 47

def base_url
  @base_url
end

#headersObject (readonly)

Returns the value of attribute headers.



47
48
49
# File 'lib/acter/action.rb', line 47

def headers
  @headers
end

Returns the value of attribute link.



47
48
49
# File 'lib/acter/action.rb', line 47

def link
  @link
end

#nameObject (readonly)

Returns the value of attribute name.



47
48
49
# File 'lib/acter/action.rb', line 47

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



47
48
49
# File 'lib/acter/action.rb', line 47

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



47
48
49
# File 'lib/acter/action.rb', line 47

def path
  @path
end

#schemaObject (readonly)

Returns the value of attribute schema.



47
48
49
# File 'lib/acter/action.rb', line 47

def schema
  @schema
end

#subjectObject (readonly)

Returns the value of attribute subject.



47
48
49
# File 'lib/acter/action.rb', line 47

def subject
  @subject
end

Instance Method Details

#send_request(&block) ⇒ Object



50
51
52
53
54
# File 'lib/acter/action.rb', line 50

def send_request(&block)
  req = Request.new(link.method, base_url, path, params, headers)
  req.client(&block)
  Result.new(req.send)
end