Class: Lokalise::Resources::Base
- Inherits:
-
Object
- Object
- Lokalise::Resources::Base
- Extended by:
- Lokalise::Request, Utils::AttributeHelpers, Utils::EndpointHelpers
- Includes:
- Utils::AttributeHelpers
- Defined in:
- lib/ruby-lokalise-api/resources/base.rb
Direct Known Subclasses
Branch, Contributor, CustomTranslationStatus, File, Key, KeyComment, Order, PaymentCard, Project, ProjectComment, ProjectLanguage, QueuedProcess, Screenshot, Snapshot, SystemLanguage, Task, Team, TeamUser, TeamUserGroup, Translation, TranslationProvider, Webhook
Constant Summary
Constants included from Lokalise::Request
Lokalise::Request::PAGINATION_HEADERS
Constants included from Connection
Constants included from Utils::AttributeHelpers
Utils::AttributeHelpers::UNIFIED_RESOURCES
Instance Attribute Summary collapse
-
#branch ⇒ Object
readonly
Returns the value of attribute branch.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#project_id ⇒ Object
readonly
Returns the value of attribute project_id.
-
#raw_data ⇒ Object
readonly
Returns the value of attribute raw_data.
-
#team_id ⇒ Object
readonly
Returns the value of attribute team_id.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Class Method Summary collapse
-
.create(client, path, params) ⇒ Object
Creates one or multiple records.
-
.destroy(client, path, params = {}) ⇒ Object
Destroys records by given ids.
-
.find(client, path, params = {}) ⇒ Object
Fetches a single record.
-
.inherited(subclass) ⇒ Object
Dynamically adds attribute readers for each inherited class.
-
.supports(*methods) ⇒ Object
Defines CRUD instance methods.
-
.update(client, path, params) ⇒ Object
Updates one or multiple records.
Instance Method Summary collapse
-
#extract_common_attributes_for(content) ⇒ Object
Extracts all common attributes that resources have.
- #id_from(response, id_key, data_key) ⇒ Object
-
#infer_path_from(response, endpoint_generator = nil) ⇒ Object
Generates path for the individual resource based on the path for the collection.
-
#initialize(response, endpoint_generator = nil) ⇒ Lokalise::Resources::Base
constructor
Initializes a new resource based on the response.
- #path_with_id(response, id_key, data_key, endpoint_generator = nil) ⇒ Object
-
#populate_attributes_for(content) ⇒ Object
Store all resources attributes under the corresponding instance variables.
Methods included from Lokalise::Request
Methods included from JsonHandler
Methods included from Connection
Methods included from Utils::AttributeHelpers
attributes_for, data_key_for, id_key_for
Methods included from Utils::EndpointHelpers
Constructor Details
#initialize(response, endpoint_generator = nil) ⇒ Lokalise::Resources::Base
Initializes a new resource based on the response. ‘endpoint_generator` is used in cases when a new instance is generated from a different resource. For example, restoring from a snapshot creates a totally different project which should have a new path.
21 22 23 24 25 26 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 21 def initialize(response, endpoint_generator = nil) populate_attributes_for response['content'] extract_common_attributes_for response['content'] @client = response['client'] @path = infer_path_from response, endpoint_generator end |
Instance Attribute Details
#branch ⇒ Object (readonly)
Returns the value of attribute branch.
11 12 13 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 11 def branch @branch end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
11 12 13 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 11 def client @client end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 11 def path @path end |
#project_id ⇒ Object (readonly)
Returns the value of attribute project_id.
11 12 13 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 11 def project_id @project_id end |
#raw_data ⇒ Object (readonly)
Returns the value of attribute raw_data.
11 12 13 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 11 def raw_data @raw_data end |
#team_id ⇒ Object (readonly)
Returns the value of attribute team_id.
11 12 13 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 11 def team_id @team_id end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
11 12 13 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 11 def user_id @user_id end |
Class Method Details
.create(client, path, params) ⇒ Object
Creates one or multiple records
71 72 73 74 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 71 def create(client, path, params) response = post path, client, prepare_params(params) object_from response, params end |
.destroy(client, path, params = {}) ⇒ Object
Destroys records by given ids
83 84 85 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 83 def destroy(client, path, params = {}) delete(path, client, prepare_params(params))['content'] end |
.find(client, path, params = {}) ⇒ Object
Fetches a single record
66 67 68 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 66 def find(client, path, params = {}) new get(path, client, prepare_params(params)) end |
.inherited(subclass) ⇒ Object
Dynamically adds attribute readers for each inherited class. Attributes are defined in the ‘data/attributes.json` file. Also sets the `ATTRIBUTES` constant to assign values to each attribute later when the response arrives from the API
33 34 35 36 37 38 39 40 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 33 def inherited(subclass) klass_attributes = attributes_for subclass subclass.class_exec do const_set :ATTRIBUTES, klass_attributes attr_reader(*klass_attributes) end super end |
.supports(*methods) ⇒ Object
Defines CRUD instance methods. In the simplest case it delegates work to the class method. In more complex case it is possible to specify sub-path and the class method name to call. Usage: ‘supports :update, :destroy, [:complex_method, ’/sub/path’, :update]‘
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 46 def supports(*methods) methods.each do |m_data| # `method_name` - the method that the resource should support # `sub_path` - a string that has to be appended to a base path # `c_method` - method name to delegate the work to method_name, sub_path, c_method = m_data.is_a?(Array) ? m_data : [m_data, '', m_data] define_method method_name do |params = {}| path = instance_variable_get(:@path) # If there's a sub_path which is a string, # preserve the initial path to allow further chaining params = params.merge(_initial_path: path) if sub_path self.class.send c_method, instance_variable_get(:@client), path + sub_path, params end end end |
.update(client, path, params) ⇒ Object
Updates one or multiple records
77 78 79 80 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 77 def update(client, path, params) response = put path, client, prepare_params(params) object_from response, params end |
Instance Method Details
#extract_common_attributes_for(content) ⇒ Object
Extracts all common attributes that resources have. Some of them may be absent in certain cases. rubocop:disable Naming/MemoizedInstanceVariableName
183 184 185 186 187 188 189 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 183 def extract_common_attributes_for(content) @raw_data = content @project_id ||= content['project_id'] @user_id ||= content['user_id'] @team_id ||= content['team_id'] @branch ||= content['branch'] end |
#id_from(response, id_key, data_key) ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 154 def id_from(response, id_key, data_key) # Content may be `{"project_id": '123', ...}` or {"snapshot": {"snapshot_id": '123', ...}} # Sometimes there is an `id_key` but it has a value of `null` # (for example when we do not place the actual order but only check its price). # Therefore we must explicitly check if the key is present return response['content'][id_key] if response['content'].key?(id_key) response['content'][data_key][id_key] end |
#infer_path_from(response, endpoint_generator = nil) ⇒ Object
Generates path for the individual resource based on the path for the collection
125 126 127 128 129 130 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 125 def infer_path_from(response, endpoint_generator = nil) id_key = id_key_for self.class.name.base_class_name data_key = data_key_for model_class: self.class.name.base_class_name path_with_id response, id_key, data_key, endpoint_generator end |
#path_with_id(response, id_key, data_key, endpoint_generator = nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 132 def path_with_id(response, id_key, data_key, endpoint_generator = nil) # Some resources do not have ids at all return nil unless response['content'].key?(id_key) || response['content'].key?(data_key) # ID of the resource id = id_from response, id_key, data_key # If `endpoint_generator` is present, generate a new path # based on the fetched id if endpoint_generator path = endpoint_generator.call project_id, id return path.remove_trailing_slash end path = response['path'] || response['base_path'] # If path already has id - just return it return path if path.match?(/#{id}\z/) # Otherwise this looks like a collection path, so append the resource id to it path.remove_trailing_slash + "/#{id}" end |
#populate_attributes_for(content) ⇒ Object
Store all resources attributes under the corresponding instance variables. ‘ATTRIBUTES` is defined inside resource-specific classes
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 166 def populate_attributes_for(content) data_key = data_key_for model_class: self.class.name.base_class_name self.class.const_get(:ATTRIBUTES).each do |attr| value = if content.key?(data_key) && content[data_key].is_a?(Hash) content[data_key][attr] else content[attr] end instance_variable_set "@#{attr}", value end end |