Class: SirenClient::Action

Inherits:
Object
  • Object
show all
Includes:
Modules::WithRawResponse
Defined in:
lib/siren_client/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#classesObject (readonly)

Returns the value of attribute classes.



6
7
8
# File 'lib/siren_client/action.rb', line 6

def classes
  @classes
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/siren_client/action.rb', line 6

def config
  @config
end

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/siren_client/action.rb', line 6

def fields
  @fields
end

#hrefObject

Returns the value of attribute href.



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

def href
  @href
end

#methodObject (readonly)

Returns the value of attribute method.



6
7
8
# File 'lib/siren_client/action.rb', line 6

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/siren_client/action.rb', line 6

def name
  @name
end

#payloadObject (readonly)

Returns the value of attribute payload.



6
7
8
# File 'lib/siren_client/action.rb', line 6

def payload
  @payload
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/siren_client/action.rb', line 6

def title
  @title
end

#typeObject (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 = {})
  options = { headers: {} }.merge @config
  if @method == 'get'
    options[:query] = params
  else
    options[:body] = params

    # Only set the Content-Type if we have a body
    options[:headers]['Content-Type'] = @type
  end

  begin
    resp = generate_raw_response(@method, self.href, options)
    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.message}\" Body=#{resp.body}"
      end
      Entity.new(resp.parsed_response, @config)
    end
  rescue URI::InvalidURIError => e
    raise InvalidURIError, e.message
  rescue JSON::ParserError => e
    raise InvalidResponseError, e.message
  end
end