Class: SirenClient::Action
- Inherits:
-
Object
- Object
- SirenClient::Action
- Includes:
- Modules::WithRawResponse
- Defined in:
- lib/siren_client/action.rb
Instance Attribute Summary collapse
-
#classes ⇒ Object
readonly
Returns the value of attribute classes.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#href ⇒ Object
Returns the value of attribute href.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(data, config = {}) ⇒ Action
constructor
A new instance of Action.
- #where(params = {}) ⇒ Object (also: #submit)
Methods included from Modules::WithRawResponse
#disable_raw_response, #next_response_is_raw?, #with_raw_response
Constructor Details
#initialize(data, config = {}) ⇒ Action
Returns a new instance of Action.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/siren_client/action.rb', line 8 def initialize(data, config={}) super() if data.class != Hash raise ArgumentError, "You must pass in a Hash to SirenClient::Action.new" end @payload = data @config = { format: :json }.merge config @name = @payload['name'] || '' @classes = (@payload['class'] || []).clone @method = (@payload['method'] || 'GET').downcase @href = @payload['href'] || '' @title = @payload['title'] || '' @type = @payload['type'] || 'application/x-www-form-urlencoded' @fields = (@payload['fields'] || []).clone @fields.map! do |field_data| SirenClient::Field.new(field_data) end end |
Instance Attribute Details
#classes ⇒ Object (readonly)
Returns the value of attribute classes.
6 7 8 |
# File 'lib/siren_client/action.rb', line 6 def classes @classes end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/siren_client/action.rb', line 6 def config @config end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
6 7 8 |
# File 'lib/siren_client/action.rb', line 6 def fields @fields end |
#href ⇒ Object
Returns the value of attribute href.
5 6 7 |
# File 'lib/siren_client/action.rb', line 5 def href @href end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
6 7 8 |
# File 'lib/siren_client/action.rb', line 6 def method @method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/siren_client/action.rb', line 6 def name @name end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
6 7 8 |
# File 'lib/siren_client/action.rb', line 6 def payload @payload end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
6 7 8 |
# File 'lib/siren_client/action.rb', line 6 def title @title end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/siren_client/action.rb', line 6 def type @type end |
Instance Method Details
#where(params = {}) ⇒ Object Also known as: submit
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/siren_client/action.rb', line 28 def where(params = {}) = { headers: {} }.merge @config if @method == 'get' [:query] = params else [:body] = params # Only set the Content-Type if we have a body [:headers]['Content-Type'] = @type end begin resp = generate_raw_response(@method, self.href, ) if next_response_is_raw? disable_raw_response resp else if resp.parsed_response.nil? raise InvalidResponseError.new "Response could not be parsed. Code=#{resp.code} Message=\"#{resp.}\" Body=#{resp.body}" end Entity.new(resp.parsed_response, @config) end rescue URI::InvalidURIError => e raise InvalidURIError, e. rescue JSON::ParserError => e raise InvalidResponseError, e. end end |