Module: Klient
- Defined in:
- lib/klient/klient.rb,
lib/klient/version.rb,
lib/klient/resource.rb,
lib/klient/response.rb,
lib/klient/response_data.rb,
lib/klient/resource_collection.rb
Defined Under Namespace
Modules: KlientClassMethods Classes: Resource, ResourceCollection, Response, ResponseData
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Attribute Summary collapse
-
#base_url ⇒ Object
(also: #url)
readonly
Returns the value of attribute base_url.
-
#collection_accessor ⇒ Object
readonly
Returns the value of attribute collection_accessor.
-
#collection_key ⇒ Object
readonly
Returns the value of attribute collection_key.
-
#header_proc ⇒ Object
readonly
Returns the value of attribute header_proc.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#url_template ⇒ Object
readonly
Returns the value of attribute url_template.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#base_url ⇒ Object (readonly) Also known as: url
Returns the value of attribute base_url.
6 7 8 |
# File 'lib/klient/klient.rb', line 6 def base_url @base_url end |
#collection_accessor ⇒ Object (readonly)
Returns the value of attribute collection_accessor.
6 7 8 |
# File 'lib/klient/klient.rb', line 6 def collection_accessor @collection_accessor end |
#collection_key ⇒ Object (readonly)
Returns the value of attribute collection_key.
6 7 8 |
# File 'lib/klient/klient.rb', line 6 def collection_key @collection_key end |
#header_proc ⇒ Object (readonly)
Returns the value of attribute header_proc.
6 7 8 |
# File 'lib/klient/klient.rb', line 6 def header_proc @header_proc end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
6 7 8 |
# File 'lib/klient/klient.rb', line 6 def headers @headers end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/klient/klient.rb', line 6 def root @root end |
#url_template ⇒ Object (readonly)
Returns the value of attribute url_template.
6 7 8 |
# File 'lib/klient/klient.rb', line 6 def url_template @url_template end |
Class Method Details
.included(klass) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/klient/klient.rb', line 14 def self.included(klass) klass.extend(KlientClassMethods) klass.send(:attr_reader, :header_proc) klass.send(:attr_reader, :resource_map) klass.send(:attr_reader, :identifier_map) klass.instance_variable_set(:@resource_map, {}) klass.instance_variable_set(:@identifier_map, {}) end |
Instance Method Details
#initialize(base_url) ⇒ Object
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/klient/klient.rb', line 23 def initialize(base_url) @root = self @header_proc = self.class.header_proc @collection_accessor = self.class.instance_variable_get(:@collection_accessor) @base_url = base_url @headers = headers @url_template = Addressable::Template.new(base_url) rmap = {} self.class::Resource.descendants .select { |x| x.name.split('::').length == 2 } .sort { |x, y| x.name.demodulize <=> y.name.demodulize } .each do |klass| next unless klass.id cname = klass.name.demodulize.underscore.singularize.to_sym cname_plural = klass.name.demodulize.underscore.pluralize.to_sym if rmap.include?(cname) next else rmap[cname_plural] = klass rmap[cname] = klass end end imap = {} rmap.values.each do |klass| next unless klass.id imap[klass.id] = klass end @resource_map = rmap @identifier_map = imap self.class::Resource.descendants.each do |rklass| cname = rklass.name.demodulize.underscore.to_sym if rklass && rklass.arguments && rklass.arguments[:type] rklass.resource_type = @resource_map[rklass.arguments[:type]] elsif @resource_map[rklass.arguments[:type]] && @identifier_map.key(rklass) rklass.resource_type = @resource_map[@identifier_map.key(rklass)] elsif @resource_map.values.include?(rklass) rklass.resource_type = rklass elsif @resource_map[cname] rklass.resource_type = @resource_map[cname] elsif @identifier_map[rklass.id] rklass.resource_type = rklass.id else rklass.resource_type = rklass end end end |