Class: Travis::Client::Action
- Inherits:
-
Object
- Object
- Travis::Client::Action
- Defined in:
- lib/travis/client/action.rb
Defined Under Namespace
Classes: Template
Constant Summary collapse
- METHOD_ORDER =
['GET', 'PATCH', 'DELETE', 'PUT', 'POST']
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#resource_type ⇒ Object
readonly
Returns the value of attribute resource_type.
-
#templates ⇒ Object
readonly
Returns the value of attribute templates.
Instance Method Summary collapse
- #accepted_types ⇒ Object
- #add_template(method, pattern, accepted_params = nil) ⇒ Object
- #call(session, params) ⇒ Object
-
#initialize(base_href, resource_type, name) ⇒ Action
constructor
A new instance of Action.
- #instance_action?(prefix) ⇒ Boolean
- #possible_params ⇒ Object
- #request_for(params) ⇒ Object
Constructor Details
#initialize(base_href, resource_type, name) ⇒ Action
Returns a new instance of Action.
76 77 78 79 80 |
# File 'lib/travis/client/action.rb', line 76 def initialize(base_href, resource_type, name) @resource_type, @name = resource_type, name @base_href = Addressable::URI.parse(base_href) @templates = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
74 75 76 |
# File 'lib/travis/client/action.rb', line 74 def name @name end |
#resource_type ⇒ Object (readonly)
Returns the value of attribute resource_type.
74 75 76 |
# File 'lib/travis/client/action.rb', line 74 def resource_type @resource_type end |
#templates ⇒ Object (readonly)
Returns the value of attribute templates.
74 75 76 |
# File 'lib/travis/client/action.rb', line 74 def templates @templates end |
Instance Method Details
#accepted_types ⇒ Object
92 93 94 |
# File 'lib/travis/client/action.rb', line 92 def accepted_types templates.flat_map { |t| t.mandatory.map { |k| k.split('.', 2).first if k.include? '.' }.compact }.uniq end |
#add_template(method, pattern, accepted_params = nil) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/travis/client/action.rb', line 116 def add_template(method, pattern, accepted_params = nil) uri_template = Addressable::Template.new(@base_href.join(pattern).to_s) template = Template.new(method, uri_template, [], [], Array(accepted_params)) pattern.scan(/\{(\W?)(?:([^\}]+))\}/) do |prefix, params| list = prefix == '?' ? template.optional : template.mandatory list.concat(params.split(',')) end templates << template templates.sort_by { |t| [METHOD_ORDER.index(t.method) || METHOD_ORDER.size, -t.mandatory.size, t.optional.size] } self end |
#call(session, params) ⇒ Object
82 83 84 85 86 |
# File 'lib/travis/client/action.rb', line 82 def call(session, params) method, url, payload = request_for(params) raise ArgumentError, "parameters don't match action, possible parameters: #{possible_params}, given: #{params.keys.map { |k| k.to_s.inspect }.join(', ')}" unless method session.request(method, url, payload) end |
#instance_action?(prefix) ⇒ Boolean
96 97 98 99 100 101 |
# File 'lib/travis/client/action.rb', line 96 def instance_action?(prefix) prefix = "#{prefix}." templates.any? do |template| template.mandatory.any? { |key| key.start_with? prefix } end end |
#possible_params ⇒ Object
88 89 90 |
# File 'lib/travis/client/action.rb', line 88 def possible_params templates.map { |t| t.possible_params(resource_type) }.join('; or ') end |
#request_for(params) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/travis/client/action.rb', line 103 def request_for(params) params = { params['@type'] => params } if params.is_a? Entity and params['@type'] params = params.to_h templates.each do |template| mapped = template.map_params(params, resource_type) next unless template.mandatory.all? { |k| mapped.include? k } next unless template.accept_everything? or mapped.keys.all? { |k| template.params.include? k } return [template.method, template.uri(mapped), template.payload(mapped)] end false end |