Class: ZohoService::Base
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- ZohoService::Base
- Defined in:
- lib/zoho_service/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.default_parent_name ⇒ Object
Returns the value of attribute default_parent_name.
-
.model_params ⇒ Object
Returns the value of attribute model_params.
Instance Attribute Summary collapse
-
#childs ⇒ Object
readonly
Returns the value of attribute childs.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#full_data ⇒ Object
readonly
Returns the value of attribute full_data.
-
#item_id ⇒ Object
readonly
Returns the value of attribute item_id.
-
#parents ⇒ Object
readonly
Returns the value of attribute parents.
-
#saved ⇒ Object
readonly
Returns the value of attribute saved.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Class Method Summary collapse
Instance Method Summary collapse
- #connector ⇒ Object
- #delete ⇒ Object
- #delete! ⇒ Object
- #full ⇒ Object
- #get_childs(child_model, childs_class) ⇒ Object
- #get_parent(model, params = {}) ⇒ Object
- #init_data(data) ⇒ Object
-
#initialize(parent = nil, data = nil, params = {}) ⇒ Base
constructor
A new instance of Base.
- #load_full(data = nil) ⇒ Object
- #parent ⇒ Object
- #resource_path ⇒ Object
- #save ⇒ Object
- #save! ⇒ Object
- #to_hash ⇒ Object
- #update(params) ⇒ Object
Constructor Details
#initialize(parent = nil, data = nil, params = {}) ⇒ Base
Returns a new instance of Base.
7 8 9 10 11 12 13 14 |
# File 'lib/zoho_service/base.rb', line 7 def initialize(parent = nil, data = nil, params = {}) @childs = {} @errors = [] @parents = parent ? { parent.class.name.demodulize.underscore.to_sym => parent } : {} @item_id = params[:item_id] || ((data && data['id']) ? data['id'] : nil) super(data) @full_data = params[:full_data] end |
Class Attribute Details
.default_parent_name ⇒ Object
Returns the value of attribute default_parent_name.
109 110 111 |
# File 'lib/zoho_service/base.rb', line 109 def default_parent_name @default_parent_name end |
.model_params ⇒ Object
Returns the value of attribute model_params.
109 110 111 |
# File 'lib/zoho_service/base.rb', line 109 def model_params @model_params end |
Instance Attribute Details
#childs ⇒ Object (readonly)
Returns the value of attribute childs.
5 6 7 |
# File 'lib/zoho_service/base.rb', line 5 def childs @childs end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
5 6 7 |
# File 'lib/zoho_service/base.rb', line 5 def errors @errors end |
#full_data ⇒ Object (readonly)
Returns the value of attribute full_data.
5 6 7 |
# File 'lib/zoho_service/base.rb', line 5 def full_data @full_data end |
#item_id ⇒ Object (readonly)
Returns the value of attribute item_id.
5 6 7 |
# File 'lib/zoho_service/base.rb', line 5 def item_id @item_id end |
#parents ⇒ Object (readonly)
Returns the value of attribute parents.
5 6 7 |
# File 'lib/zoho_service/base.rb', line 5 def parents @parents end |
#saved ⇒ Object (readonly)
Returns the value of attribute saved.
5 6 7 |
# File 'lib/zoho_service/base.rb', line 5 def saved @saved end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
5 6 7 |
# File 'lib/zoho_service/base.rb', line 5 def table @table end |
Class Method Details
.class_path(id = nil) ⇒ Object
110 111 112 |
# File 'lib/zoho_service/base.rb', line 110 def class_path(id = nil) "/#{models_name}" + (id ? '/'+id : '') end |
.models_name ⇒ Object
114 115 116 |
# File 'lib/zoho_service/base.rb', line 114 def models_name self.name.demodulize.pluralize.underscore end |
.new_by_id(parent, id) ⇒ Object
118 119 120 121 122 |
# File 'lib/zoho_service/base.rb', line 118 def new_by_id(parent, id) raise('Need id in Base::new_by_id of ZohoService gem.') unless id data = parent.connector.load_by_api(parent.resource_path + class_path(id)) new(parent, data, { full_data: true }) end |
Instance Method Details
#connector ⇒ Object
16 17 18 |
# File 'lib/zoho_service/base.rb', line 16 def connector parent ? parent.connector : self end |
#delete ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/zoho_service/base.rb', line 67 def delete return unless @item_id response = connector.load_by_api(resource_path, nil, { method: :delete }) if response && response['message'] @errors << response['message'] elsif response @item_id = nil @id = nil @full_data = nil else @errors << 'Error while try delete' end self end |
#delete! ⇒ Object
82 83 84 85 86 |
# File 'lib/zoho_service/base.rb', line 82 def delete! delete raise("#{@errors.join("\n")}") if @errors.any? self end |
#full ⇒ Object
33 34 35 36 |
# File 'lib/zoho_service/base.rb', line 33 def full load_full unless @full_data self end |
#get_childs(child_model, childs_class) ⇒ Object
28 29 30 31 |
# File 'lib/zoho_service/base.rb', line 28 def get_childs(child_model, childs_class) @childs[child_model] ||= ApiCollection.new(self, { items_class: childs_class}) @childs[child_model] end |
#get_parent(model, params = {}) ⇒ Object
24 25 26 |
# File 'lib/zoho_service/base.rb', line 24 def get_parent(model, params = {}) @parents[model.to_sym] end |
#init_data(data) ⇒ Object
44 45 46 47 |
# File 'lib/zoho_service/base.rb', line 44 def init_data(data) data.each{ |k, v| @table[k.to_sym] = v; new_ostruct_member(k); } @item_id = id if id && !@item_id end |
#load_full(data = nil) ⇒ Object
38 39 40 41 42 |
# File 'lib/zoho_service/base.rb', line 38 def load_full(data = nil) raise('You must save model before take full data in ZohoService gem.') unless @item_id init_data(data || connector.load_by_api(resource_path)) @full_data = true end |
#parent ⇒ Object
20 21 22 |
# File 'lib/zoho_service/base.rb', line 20 def parent @parents[self.class.default_parent_name] || @parents.values.first end |
#resource_path ⇒ Object
99 100 101 102 |
# File 'lib/zoho_service/base.rb', line 99 def resource_path raise('Cant take resource_path for not saved item in ZohoService gem.') unless @item_id parent.resource_path + self.class.class_path(@item_id) end |
#save ⇒ Object
88 89 90 91 |
# File 'lib/zoho_service/base.rb', line 88 def save update(to_hash) @saved = @errors.any? end |
#save! ⇒ Object
93 94 95 96 97 |
# File 'lib/zoho_service/base.rb', line 93 def save! save raise("#{@errors.join("\n")}") if @errors.any? self end |
#to_hash ⇒ Object
104 105 106 |
# File 'lib/zoho_service/base.rb', line 104 def to_hash @table.to_hash(*args, &block) end |
#update(params) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/zoho_service/base.rb', line 49 def update(params) url = @item_id ? resource_path : parent.resource_path + self.class.class_path response = connector.load_by_api(url, params.to_hash, { method: @item_id ? :patch : :post }) if response if response.kind_of?(Array) raise("ERROR! create item response is Array[#{response.count}]. Try change http to https in api_url :)!") elsif response['message'] @errors << response['message'] else init_data(response.to_hash) @full_data = nil end else @errors << "Error while try update[#{@item_id}] url=[#{url}] params=[#{params&.to_json}]" end self end |