Class: ActiveRestClient::Base

Inherits:
Object
  • Object
show all
Includes:
Caching, Configuration, Mapping, Recording, RequestFiltering, Validation
Defined in:
lib/active_rest_client/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Recording

included

Methods included from Caching

included

Methods included from Validation

#errors, included, #valid?

Methods included from RequestFiltering

included

Methods included from Configuration

included

Methods included from Mapping

included

Constructor Details

#initialize(attrs = {}) ⇒ Base

Returns a new instance of Base.

Raises:

  • (Exception)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_rest_client/base.rb', line 17

def initialize(attrs={})
  @attributes = {}
  @dirty_attributes = Set.new

  raise Exception.new("Cannot instantiate Base class") if self.class.name == "ActiveRestClient::Base"

  attrs.each do |attribute_name, attribute_value|
    attribute_name = attribute_name.to_sym
    if attribute_value.to_s[/\d{4}\-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})/]
      @attributes[attribute_name] = DateTime.parse(attribute_value)
    else
      @attributes[attribute_name] = attribute_value
    end
    @dirty_attributes << attribute_name
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/active_rest_client/base.rb', line 98

def method_missing(name, *args)
  if name.to_s[-1,1] == "="
    name = name.to_s.chop.to_sym
    @attributes[name] = args.first
    @dirty_attributes << name
  else
    name_sym = name.to_sym
    name = name.to_s

    if @attributes.has_key? name_sym
      @attributes[name_sym]
    else
      if name[/^lazy_/] && mapped = self.class._mapped_method(name_sym)
        raise ValidationFailedException.new unless valid?
        request = Request.new(mapped, self, args.first)
        ActiveRestClient::LazyLoader.new(request)
      elsif mapped = self.class._mapped_method(name_sym)
        raise ValidationFailedException.new unless valid?
        request = Request.new(mapped, self, args.first)
        request.call
      elsif self.class.whiny_missing
        raise NoAttributeException.new("Missing attribute #{name_sym}")
      else
        nil
      end
    end
  end
end

Instance Attribute Details

#_statusObject

Returns the value of attribute _status.



10
11
12
# File 'lib/active_rest_client/base.rb', line 10

def _status
  @_status
end

Class Method Details

._lazy_request(request, method = :get, params = nil) ⇒ Object



59
60
61
# File 'lib/active_rest_client/base.rb', line 59

def self._lazy_request(request, method = :get, params = nil)
  ActiveRestClient::LazyLoader.new(prepare_direct_request(request, method), params)
end

._plain_request(request, method = :get, params = nil) ⇒ Object



55
56
57
# File 'lib/active_rest_client/base.rb', line 55

def self._plain_request(request, method = :get, params = nil)
  prepare_direct_request(request, method, plain:true).call(params)
end

._request(request, method = :get, params = nil) ⇒ Object



51
52
53
# File 'lib/active_rest_client/base.rb', line 51

def self._request(request, method = :get, params = nil)
  prepare_direct_request(request, method).call(params)
end

._request_for(method_name, *args) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/active_rest_client/base.rb', line 73

def self._request_for(method_name, *args)
  if mapped = self._mapped_method(method_name)
    params = (args.first.is_a?(Hash) ? args.first : nil)
    request = Request.new(mapped, self, params)
    request
  else
    nil
  end
end

.prepare_direct_request(request, method, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/active_rest_client/base.rb', line 63

def self.prepare_direct_request(request, method, options={})
  unless request.is_a? ActiveRestClient::Request
    options[:plain] ||= false
    mapped = {url:"DIRECT-CALLED-#{request}", method:method, options:{url:request, plain:options[:plain]}}

    request = Request.new(mapped, self)
  end
  request
end

Instance Method Details

#[](key) ⇒ Object



83
84
85
# File 'lib/active_rest_client/base.rb', line 83

def [](key)
  @attributes[key.to_sym]
end

#[]=(key, value) ⇒ Object



87
88
89
90
# File 'lib/active_rest_client/base.rb', line 87

def []=(key, value)
  @attributes[key.to_sym] = value
  @dirty_attributes << key
end

#_attributesObject



38
39
40
# File 'lib/active_rest_client/base.rb', line 38

def _attributes
  @attributes
end

#_clean!Object



34
35
36
# File 'lib/active_rest_client/base.rb', line 34

def _clean!
  @dirty_attributes = Set.new
end

#_copy_from(result) ⇒ Object



42
43
44
45
# File 'lib/active_rest_client/base.rb', line 42

def _copy_from(result)
  @attributes =  result._attributes
  @_status = result._status
end

#dirty?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/active_rest_client/base.rb', line 47

def dirty?
  @dirty_attributes.size > 0
end

#eachObject



92
93
94
95
96
# File 'lib/active_rest_client/base.rb', line 92

def each
  @attributes.each do |key, value|
    yield key, value
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/active_rest_client/base.rb', line 127

def respond_to_missing?(method_name, include_private = false)
  @attributes.has_key? method_name.to_sym
end

#to_hashObject



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/active_rest_client/base.rb', line 131

def to_hash
  output = {}
  @attributes.each do |key, value|
    if value.is_a? ActiveRestClient::Base
      output[key.to_s] = value.to_hash
    elsif value.is_a? Array
      output[key.to_s] = value.map(&:to_hash)
    else
      output[key.to_s] = value
    end
  end
  output
end

#to_jsonObject



145
146
147
148
# File 'lib/active_rest_client/base.rb', line 145

def to_json
  output = to_hash
  output.to_json
end