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.
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
- #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 27 28 29 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 21 def initialize(response, endpoint_generator = nil) populate_attributes_for response['content'] @raw_data = response['content'] @project_id = response['content']['project_id'] @branch = response['content']['branch'] @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 |
Class Method Details
.create(client, path, params) ⇒ Object
Creates one or multiple records
69 70 71 72 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 69 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
81 82 83 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 81 def destroy(client, path, params = {}) delete(path, client, prepare_params(params))['content'] end |
.find(client, path, params = {}) ⇒ Object
Fetches a single record
64 65 66 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 64 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
36 37 38 39 40 41 42 43 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 36 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]‘
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 49 def supports(*methods) methods.each do |m_data| 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, 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
75 76 77 78 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 75 def update(client, path, params) response = put path, client, prepare_params(params) object_from response, params end |
Instance Method Details
#id_from(response, id_key, data_key) ⇒ Object
152 153 154 155 156 157 158 159 160 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 152 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
123 124 125 126 127 128 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 123 def infer_path_from(response, endpoint_generator = nil) id_key = id_key_for self.class.name.base_class_name data_key = data_key_for 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 130 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
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/ruby-lokalise-api/resources/base.rb', line 164 def populate_attributes_for(content) data_key = data_key_for 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 |