Module: Hateoas::MediaType

Defined in:
lib/hateoas.rb

Defined Under Namespace

Classes: Request

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(including_class) ⇒ Object



8
9
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
# File 'lib/hateoas.rb', line 8

def self.included(including_class)
  including_class.class_eval do
    def self.media_type(type)
      @media_type = type
    end

    def self.serialization(type)
    end

    def self.element(*elements)
    end

    def self.attribute(*attributes)
    end

    def self.relation(*relations)
    end

    def self.action(*actions)
      @actions = actions
    end

    def self.actions
      @actions
    end
  end
end

Instance Method Details

#actionsObject



56
57
58
# File 'lib/hateoas.rb', line 56

def actions
  self.class.actions.select{|rel| relation_exists?(rel) }
end

#dataObject



54
# File 'lib/hateoas.rb', line 54

def data; @data; end

#initialize(uri) ⇒ Object



60
61
62
# File 'lib/hateoas.rb', line 60

def initialize(uri)
  @data = Nokogiri::XML(Request.new(uri, @media_type).get)
end

#media_typeObject



53
# File 'lib/hateoas.rb', line 53

def media_type; @media_type; end

#relation_exists?(rel) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/hateoas.rb', line 64

def relation_exists?(rel)
  @data.xpath("//*[@rel='#{rel}']").first
end

#transition(rel) ⇒ Object



68
69
70
71
72
# File 'lib/hateoas.rb', line 68

def transition(rel)
  node = @data.xpath("//*[@rel='#{rel}']").first
  return unless node
  @data = Nokogiri::XML(Request.new(node[:href], @media_type).get)
end