Module: HyperResource::Modules::ConfigAttributes

Included in:
HyperResource
Defined in:
lib/hyper_resource/modules/config_attributes.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ATTRS =
[:auth, :headers, :namespace, :adapter, :faraday_options]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



7
8
9
# File 'lib/hyper_resource/modules/config_attributes.rb', line 7

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#adapterObject

Returns the adapter class for this resource.



88
89
90
91
# File 'lib/hyper_resource/modules/config_attributes.rb', line 88

def adapter
  cfg_get(:adapter) ||
    HyperResource::Adapter::HAL_JSON
end

#adapter=(v) ⇒ Object

Sets the adapter class for this resource.



100
101
102
# File 'lib/hyper_resource/modules/config_attributes.rb', line 100

def adapter=(v)
  cfg_set(:adapter, v)
end

#adapter_for_url(url) ⇒ Object

Returns the adapter class for the given url.



94
95
96
97
# File 'lib/hyper_resource/modules/config_attributes.rb', line 94

def adapter_for_url(url)
  self.hr_config.get_for_url(url, :adapter) ||
    HyperResource::Adapter::HAL_JSON
end

#authObject

Returns the auth config hash for this resource.



34
35
36
# File 'lib/hyper_resource/modules/config_attributes.rb', line 34

def auth
  cfg_get(:auth)
end

#auth=(v) ⇒ Object

Sets the auth config hash for this resource. Currently only the format ‘=> [’username’, ‘password’]‘ is supported.



46
47
48
# File 'lib/hyper_resource/modules/config_attributes.rb', line 46

def auth=(v)
  cfg_set(:auth, v)
end

#auth_for_url(url) ⇒ Object

Returns the auth config hash for the given url.



39
40
41
# File 'lib/hyper_resource/modules/config_attributes.rb', line 39

def auth_for_url(url)
  self.hr_config.get_for_url(url, :auth)
end

#config(hash = nil) ⇒ Object

When called with no arguments, returns this resource’s Configuration. When called with a hash, applies the given configuration parameters to this resource’s Configuration. ‘hash` must be in the form:

{'hostmask' => {'attr1' => {...}, 'attr2' => {...}, ...}}

Valid attributes are ‘auth`, `headers`, `namespace`, `adapter`, `default_attributes`, and `faraday_options`.



27
28
29
30
# File 'lib/hyper_resource/modules/config_attributes.rb', line 27

def config(hash=nil)
  return hr_config unless hash
  hr_config.config(hash)
end

#default_attributesObject

Returns the hash of default attributes for this resource.



106
107
108
# File 'lib/hyper_resource/modules/config_attributes.rb', line 106

def default_attributes
  cfg_get(:default_attributes)
end

#default_attributes=(v) ⇒ Object

Sets the hash of default attributes for this resource. These attributes will be passed along with every HTTP request.



117
118
119
# File 'lib/hyper_resource/modules/config_attributes.rb', line 117

def default_attributes=(v)
  cfg_set(:default_attributes, v)
end

#default_attributes_for_url(url) ⇒ Object

Returns the hash of default attributes for the given url.



111
112
113
# File 'lib/hyper_resource/modules/config_attributes.rb', line 111

def default_attributes_for_url(url)
  self.hr_config.get_for_url(url, :default_attributes)
end

#faraday_optionsObject

Returns the Faraday connection options hash for this resource.



123
124
125
# File 'lib/hyper_resource/modules/config_attributes.rb', line 123

def faraday_options
  cfg_get(:faraday_options)
end

#faraday_options=(v) ⇒ Object

Sets the Faraday connection options hash for this resource. These options will be used for all HTTP requests.



134
135
136
# File 'lib/hyper_resource/modules/config_attributes.rb', line 134

def faraday_options=(v)
  cfg_set(:faraday_options, v)
end

#faraday_options_for_url(url) ⇒ Object

Returns the Faraday connection options hash for this resource.



128
129
130
# File 'lib/hyper_resource/modules/config_attributes.rb', line 128

def faraday_options_for_url(url)
  self.hr_config.get_for_url(url, :faraday_options)
end

#headersObject

Returns the headers hash for this resource. This is done by merging all applicable header configs.



53
54
55
56
57
58
# File 'lib/hyper_resource/modules/config_attributes.rb', line 53

def headers
  matching_masks = self.hr_config.matching_masks_for_url(self.url)
  matching_masks.inject({}) do |hash, mask|
    hash.merge(self.hr_config.get(mask, 'headers') || {})
  end
end

#headers=(v) ⇒ Object

Sets the headers hash for this resource.



66
67
68
# File 'lib/hyper_resource/modules/config_attributes.rb', line 66

def headers=(v)
  cfg_set(:headers, v)
end

#headers_for_url(url) ⇒ Object

Returns the headers hash for the given url.



61
62
63
# File 'lib/hyper_resource/modules/config_attributes.rb', line 61

def headers_for_url(url)
  self.hr_config.get_for_url(url, :headers)
end

#hr_configObject



12
13
14
# File 'lib/hyper_resource/modules/config_attributes.rb', line 12

def hr_config
  @hr_config ||= self.class::Configuration.new
end

#hr_config=(cfg) ⇒ Object



17
18
19
# File 'lib/hyper_resource/modules/config_attributes.rb', line 17

def hr_config=(cfg)
  @hr_config = cfg
end

#namespaceObject

Returns the namespace string/class for this resource.



72
73
74
# File 'lib/hyper_resource/modules/config_attributes.rb', line 72

def namespace
  cfg_get(:namespace)
end

#namespace=(v) ⇒ Object

Sets the namespace string/class for this resource.



82
83
84
# File 'lib/hyper_resource/modules/config_attributes.rb', line 82

def namespace=(v)
  cfg_set(:namespace, v)
end

#namespace_for_url(url) ⇒ Object

Returns the namespace string/class for the given url.



77
78
79
# File 'lib/hyper_resource/modules/config_attributes.rb', line 77

def namespace_for_url(url)
  self.hr_config.get_for_url(url, :namespace)
end