Module: ResourceMethods

Included in:
Klient::KlientClassMethods, Klient::Resource
Defined in:
lib/klient/resource_methods.rb

Instance Method Summary collapse

Instance Method Details

#collection(name, template = nil, **hash_args, &block) ⇒ Object



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/klient/resource_methods.rb', line 10

def collection(name, template = nil, **hash_args, &block)
  klass_name = name.to_s.camelcase

  klass = Class.new(Klient::Resource) do
    @arguments = hash_args
    @type = :collection

    # Obtain the collection's resource identifier. Don't allow hash arg AND block
    # param for same thing -- it has to be either one or the other.
    if block_given? && block.arity > 0 && hash_args[:identifier]
      raise ArgumentError, "Collection identifier for :#{name} can be specified as a " \
      "hash argument OR a block parameter (You can't use both simultaneously.)"
    elsif block_given? && block.arity > 0
      @id = block.parameters[0][1]
    elsif hash_args[:identifier]
      @id = hash_args[:identifier]
    else
      # raise ArgumentError, "#{name} collection definition does not specify a resource identifier."
    end

    # Don't allow templates with variables.
    if template && template =~ /[\{\}]/
      raise ArgumentError, "URL template variables not supported."
    end

    # Build a URL template if the template arg was provided.
    if template && id
      @url_template = Addressable::Template.new(template.to_s + "{/#{id}}")
    elsif id
      @identifier = nil
      @url_template = Addressable::Template.new("/#{name}{/#{id}}")
    else
      @identifier = nil
      @url_template = Addressable::Template.new("/#{name}")
    end

    class_eval(&block) if block
  end
  const_set(klass_name, klass)

  define_method(name) do
    klass.new(self)
  end
end

#default_collection_accessor(sym) ⇒ Object



2
3
4
# File 'lib/klient/resource_methods.rb', line 2

def default_collection_accessor(sym)
  @collection_accessor = sym
end

#headers(&block) ⇒ Object



6
7
8
# File 'lib/klient/resource_methods.rb', line 6

def headers(&block)
  @header_proc = block
end

#resource(name, template = nil, **hash_args, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/klient/resource_methods.rb', line 55

def resource(name, template = nil, **hash_args, &block)
  klass_name = name.to_s.camelcase

  klass = Class.new(Klient::Resource) do
    @arguments = hash_args
    @type = :resource

    # TODO: Avoid identifier conflicts between hash and URL template.
    if template
      @url_template = Addressable::Template.new(template)
    else
      @identifier = nil
      @url_template = Addressable::Template.new("/#{name}")
    end

    class_eval(&block) if block
  end
  const_set(klass_name, klass)

  define_method(name) do
    klass.new(self)
  end
end

#resources(*resource_names) ⇒ Object



79
80
81
# File 'lib/klient/resource_methods.rb', line 79

def resources(*resource_names)
  resource_names.each { |rname| resource rname }
end