Class: Travis::Client::Action::Template

Inherits:
Struct
  • Object
show all
Defined in:
lib/travis/client/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#body_paramsObject

Returns the value of attribute body_params

Returns:

  • (Object)

    the current value of body_params



4
5
6
# File 'lib/travis/client/action.rb', line 4

def body_params
  @body_params
end

#mandatoryObject

Returns the value of attribute mandatory

Returns:

  • (Object)

    the current value of mandatory



4
5
6
# File 'lib/travis/client/action.rb', line 4

def mandatory
  @mandatory
end

#methodObject

Returns the value of attribute method

Returns:

  • (Object)

    the current value of method



4
5
6
# File 'lib/travis/client/action.rb', line 4

def method
  @method
end

#optionalObject

Returns the value of attribute optional

Returns:

  • (Object)

    the current value of optional



4
5
6
# File 'lib/travis/client/action.rb', line 4

def optional
  @optional
end

#uri_templateObject

Returns the value of attribute uri_template

Returns:

  • (Object)

    the current value of uri_template



4
5
6
# File 'lib/travis/client/action.rb', line 4

def uri_template
  @uri_template
end

Instance Method Details

#accept_everything?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/travis/client/action.rb', line 61

def accept_everything?
  method != 'GET' and body_params.empty?
end

#entity_params(key, value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/travis/client/action.rb', line 38

def entity_params(key, value)
  result = {}
  params.each do |param|
    next unless param =~ /^#{key}\.(.+)$/
    next unless value.respond_to? $1
    result[param] = value.public_send($1)
  end
  result[key] = value if result.empty?
  result
end

#map_pair(key, value, prefix) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/travis/client/action.rb', line 9

def map_pair(key, value, prefix)
  return                                             if value.nil?
  return entity_params(key, value)                   if value.is_a? Entity
  return map_pair("#{prefix}.#{key}", value, prefix) if !params.include?(key) and params.include?("#{prefix}.#{key}")

  if value.is_a? Hash and !body_params.include? key
    return value.map { |k,v| map_pair("#{prefix}.#{k}", v, prefix).to_h }.inject(&:merge)
  end

  if mandatory.include? key or optional.include? key
    value = prepare_segment(value)
  else
    value = prepare_json(value)
  end

  { key => value }
end

#map_params(params, prefix) ⇒ Object



5
6
7
# File 'lib/travis/client/action.rb', line 5

def map_params(params, prefix)
  params.map { |key, value| map_pair(key.to_s, value, prefix).to_h }.inject({}, &:merge)
end

#paramsObject



57
58
59
# File 'lib/travis/client/action.rb', line 57

def params
  @params ||= mandatory + optional + body_params
end

#payload(params) ⇒ Object



53
54
55
# File 'lib/travis/client/action.rb', line 53

def payload(params)
  params.reject { |k,_| mandatory.include? k or optional.include? k }
end

#possible_params(prefix) ⇒ Object



65
66
67
68
69
# File 'lib/travis/client/action.rb', line 65

def possible_params(prefix)
  [mandatory, optional + body_params].
    map { |list| list.map { |p| p.sub(/^#{prefix}\./, '').inspect }.join(',') if list.any? }.
    compact.join(', optionally ')
end

#prepare_json(value) ⇒ Object



34
35
36
# File 'lib/travis/client/action.rb', line 34

def prepare_json(value)
  value
end

#prepare_segment(value) ⇒ Object



27
28
29
30
31
32
# File 'lib/travis/client/action.rb', line 27

def prepare_segment(value)
  case value
  when Array then value.map { |v| prepare_segment(v) }.join(',')
  else value.to_s
  end
end

#uri(params) ⇒ Object



49
50
51
# File 'lib/travis/client/action.rb', line 49

def uri(params)
  uri_template.expand(params)
end