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

def self.included(including_class)
  including_class.class_eval do
    def self.media_type(type=nil)
      @media_type = type if type
      @media_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



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

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

#dataObject



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

def data; @data; end

#initialize(uri) ⇒ Object



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

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

#media_typeObject



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

def media_type; self.class.media_type; end

#relation_exists?(rel) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#transition(rel) ⇒ Object



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

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