Module: KegbotApi::RestNounAttributes::ClassMethods

Defined in:
lib/kegbot_api/nouns/rest_noun_attributes.rb

Instance Method Summary collapse

Instance Method Details

#boolean(name, options = {}) ⇒ Object

Defines a boolean field for this REST noun, available at name?



55
56
57
58
59
60
61
62
63
# File 'lib/kegbot_api/nouns/rest_noun_attributes.rb', line 55

def boolean(name, *args)
  options = args.slice!(0) || {}
  attribute_name = options[:attribute_name] || name
  key = attribute_name.to_s

  define_method("#{name}?") do
    self.attributes[key]
  end
end

#define_accessor(name, *args) ⇒ Object

Defines an attribute accessor with the specified name, returning the value of #attribute value with key name



108
109
110
111
112
113
114
115
116
# File 'lib/kegbot_api/nouns/rest_noun_attributes.rb', line 108

def define_accessor(name, *args)
  options = args.slice!(0) || {}
  aliases = options[:alias]

  define_method(name) do
    self.attributes[name.to_s]
  end
  alias_method aliases.to_sym, name.to_sym if aliases
end

#float(name, *args) ⇒ Object

Defines a Float field for this REST noun



25
26
27
# File 'lib/kegbot_api/nouns/rest_noun_attributes.rb', line 25

def float(name, *args)
  define_accessor name, *args
end

#has_one(name, options = {}) ⇒ Object

Defines a 1-1 relationship between this Noun and another.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kegbot_api/nouns/rest_noun_attributes.rb', line 36

def has_one(name, *args)
  options = args.slice!(0) || {}
  class_name = options[:class_name] || name.capitalize
  attribute_name = options[:attribute_name] || name
  key = attribute_name.to_s
  aliases = options[:alias]

  define_method(name) do
    if self.attributes.include? key
      self.client.noun_class(class_name).new self.attributes[key]
    else
      nil
    end
  end
  alias_method aliases.to_sym, name.to_sym if aliases
end

#id(name, *args) ⇒ Object

Defines an id field for this REST noun



15
16
17
# File 'lib/kegbot_api/nouns/rest_noun_attributes.rb', line 15

def id(name, *args)
  define_accessor  name
end

#integer(name, *args) ⇒ Object

Defines a Fixnum field for this REST noun



30
31
32
# File 'lib/kegbot_api/nouns/rest_noun_attributes.rb', line 30

def integer(name, *args)
  define_accessor name
end

#string(name, *args) ⇒ Object

Defines a String field for this REST noun



20
21
22
# File 'lib/kegbot_api/nouns/rest_noun_attributes.rb', line 20

def string(name, *args)
  define_accessor  name
end

#time(name, *args) ⇒ Object

Defines a Time field for this REST noun. The time must be defined as a string in the REST JSON in ISO8601 format



67
68
69
70
71
72
73
74
75
76
# File 'lib/kegbot_api/nouns/rest_noun_attributes.rb', line 67

def time(name, *args)
  define_method(name) do
    if self.attributes.include? name.to_s
      DateTime.iso8601(self.attributes[name.to_s]).to_time
    else
      nil
    end

  end
end

#url(name, options = {}) ⇒ Object

Defines a NOUN attribute of a URL In the REST JSON, the URL my be relative or absolute. If relative, it will get appended to the client’s base_url host.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kegbot_api/nouns/rest_noun_attributes.rb', line 81

def url(name, *args)
  options = args.slice!(0) || {}
  attribute_name = options[:attribute_name] || name
  key = attribute_name.to_s

  define_method("#{name}") do
    path = self.attributes[key]

    if path
      uri = URI(path)

      if uri.absolute?
        uri.to_s
      else
        # merge this URI with the client path
        base_uri = URI(self.client.base_url)

        klass = base_uri.scheme == 'http' ? URI::HTTP : URI::HTTPS
        klass.build(:host => base_uri.host, :path => path).to_s
      end
    else
      nil
    end
  end
end