Skip to content

Commit

Permalink
Merge pull request #6 from lokalise/SRUBY-2_new_attributes
Browse files Browse the repository at this point in the history
New attributes and better chaining
  • Loading branch information
bodrovis authored Jul 8, 2020
2 parents 5c0bfea + 4135900 commit 426f112
Show file tree
Hide file tree
Showing 28 changed files with 407 additions and 177 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 3.1.0 (08-Jul-20)

* Added all recently introduced attributes for the following endpoints: `Contributor` and `Key`
* Better support for method chaining and data reloading
* API now supports only background file uploads, and the `queue` parameter doesn't have any effect anymore. Therefore, removed all code and docs related to sync uploading.

## 3.0.0 (18-May-20)

* **Breaking change** Uploading files in the background is now a preferred method, and the only method in version 3. Synchronous uploading is still supported and allowed in version 2 but will be removed in the near future. Find more info [in the docs](https://github.com/lokalise/ruby-lokalise-api#upload-translation-file).
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ Exports project files as a `.zip` bundle and makes them available to download (t

[Doc](https://app.lokalise.com/api2docs/curl/#transition-upload-a-file-post)

Starting from 9 May 2020, **background uploading is the preferred method of importing translation files**. Version 3 supports only asynchronous uploading. Version 2 still allows synchronous uploading but this feature will be removed in the near future.
Starting from July 2020, **background uploading is the only method of importing translation files**.

```ruby
@client.upload_file(project_id, params) # Input:
Expand Down
19 changes: 15 additions & 4 deletions lib/ruby-lokalise-api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ class Client

def initialize(token, params = {})
@token = token
@timeout = params.fetch(:timeout) { nil }
@open_timeout = params.fetch(:open_timeout) { nil }
@timeout = params.fetch(:timeout, nil)
@open_timeout = params.fetch(:open_timeout, nil)
end

def construct_request(klass, method, endpoint_ids, params = {}, object_key = nil)
# rubocop:disable Metrics/ParameterLists
# Constructs request to perform the specified action
# @param klass The actual class to call the method upon
# @param method [Symbol] The method to call (:new, :update, :create etc)
# @param endpoint_ids [Array, Hash] IDs that are used to generate the proper path to the endpoint
# @param params [Array, Hash] Request parameters
# @param object_key [String, Symbol] Key that should be used to wrap parameters into
# @param initial_ids [Array] IDs that should be used to generate base endpoint path. The base path is used for method chaining
def construct_request(klass, method, endpoint_ids, params = {}, object_key = nil, initial_ids = nil)
path = klass.endpoint(*endpoint_ids)
klass.send method, self, path, format_params(params, object_key)
formatted_params = format_params(params, object_key)
formatted_params[:_initial_path] = klass.endpoint(*initial_ids) if initial_ids
klass.send method, self, path, formatted_params
end
# rubocop:enable Metrics/ParameterLists

# Converts `params` to hash with arrays under the `object_key` key.
# Used in bulk operations
Expand Down
5 changes: 3 additions & 2 deletions lib/ruby-lokalise-api/collections/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Base
extend Lokalise::Utils::EndpointHelpers

attr_reader :total_pages, :total_results, :results_per_page, :current_page, :collection,
:project_id, :team_id, :request_params, :client, :path, :branch
:project_id, :team_id, :request_params, :client, :path, :branch, :user_id

# Initializes a new collection based on the response
#
Expand All @@ -19,9 +19,10 @@ class Base
def initialize(response, params = {})
produce_collection_for response
populate_pagination_data_for response
# Project, team id and branch may not be present in some cases
# Project, team id, user id, and branch may not be present in some cases
@project_id = response['content']['project_id']
@team_id = response['content']['team_id']
@user_id = response['content']['user_id']
@branch = response['content']['branch']
@request_params = params
@client = response['client']
Expand Down
28 changes: 16 additions & 12 deletions lib/ruby-lokalise-api/data/attributes.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"email",
"fullname",
"created_at",
"created_at_timestamp",
"is_admin",
"is_reviewer",
"languages",
"admin_rights",
"created_at_timestamp"
"admin_rights"
],
"custom_translation_status": [
"status_id",
Expand All @@ -39,6 +39,7 @@
"key": [
"key_id",
"created_at",
"created_at_timestamp",
"key_name",
"filenames",
"description",
Expand All @@ -55,7 +56,10 @@
"base_words",
"char_limit",
"custom_attributes",
"created_at_timestamp"
"modified_at",
"modified_at_timestamp",
"translations_modified_at",
"translations_modified_at_timestamp"
],
"language": [
"lang_id",
Expand All @@ -70,6 +74,7 @@
"card_id",
"status",
"created_at",
"created_at_timestamp",
"created_by",
"created_by_email",
"source_language_iso",
Expand All @@ -81,8 +86,7 @@
"translation_tier",
"translation_tier_name",
"briefing",
"total",
"created_at_timestamp"
"total"
],
"payment_card": [
"card_id",
Expand All @@ -96,10 +100,10 @@
"name",
"description",
"created_at",
"created_at_timestamp",
"created_by",
"created_by_email",
"team_id",
"created_at_timestamp",
"base_language_id",
"base_language_iso",
"settings",
Expand Down Expand Up @@ -132,9 +136,9 @@
"snapshot_id",
"title",
"created_at",
"created_at_timestamp",
"created_by",
"created_by_email",
"created_at_timestamp"
"created_by_email"
],
"task": [
"task_id",
Expand Down Expand Up @@ -168,18 +172,18 @@
"team_id",
"name",
"created_at",
"created_at_timestamp",
"plan",
"quota_usage",
"quota_allowed",
"created_at_timestamp"
"quota_allowed"
],
"team_user": [
"user_id",
"email",
"fullname",
"created_at",
"role",
"created_at_timestamp"
"created_at_timestamp",
"role"
],
"team_user_group": [
"group_id",
Expand Down
26 changes: 20 additions & 6 deletions lib/ruby-lokalise-api/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Base
include Lokalise::Utils::AttributeHelpers
extend Lokalise::Utils::EndpointHelpers

attr_reader :raw_data, :project_id, :client, :path, :branch
attr_reader :raw_data, :project_id, :client, :path, :branch, :user_id, :team_id

# Initializes a new resource based on the response.
# `endpoint_generator` is used in cases when a new instance is generated
Expand All @@ -20,10 +20,7 @@ class Base
# @return [Lokalise::Resources::Base]
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']
extract_common_attributes_for response['content']
@client = response['client']
@path = infer_path_from response, endpoint_generator
end
Expand All @@ -48,11 +45,16 @@ def inherited(subclass)
# Usage: `supports :update, :destroy, [:complex_method, '/sub/path', :update]`
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, preserve the initial path to allow further chaining
# 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
Expand Down Expand Up @@ -174,6 +176,18 @@ def populate_attributes_for(content)
instance_variable_set "@#{attr}", value
end
end

# Extracts all common attributes that resources have.
# Some of them may be absent in certain cases.
# rubocop:disable Naming/MemoizedInstanceVariableName
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
# rubocop:enable Naming/MemoizedInstanceVariableName
end
end
end
1 change: 0 additions & 1 deletion lib/ruby-lokalise-api/resources/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def download(client, path, params)
end

def upload(client, path, params)
params[:queue] = true
klass = Lokalise::Resources::QueuedProcess
klass.new post(path, client, params),
->(project_id, id) { klass.endpoint(project_id, id) }
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby-lokalise-api/resources/project_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Resources
class ProjectLanguage < Base
DATA_KEY = 'Language'
ID_KEY = 'lang'
supports :update, :destroy
supports :update, :destroy, [:reload_data, '', :find]

class << self
def endpoint(project_id, language_id = nil)
Expand Down
3 changes: 2 additions & 1 deletion lib/ruby-lokalise-api/resources/team_user_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class TeamUserGroup < Base
[:add_projects, '/projects/add', :update],
[:remove_projects, '/projects/remove', :update],
[:add_users, '/members/add', :update],
[:remove_users, '/members/remove', :update]
[:remove_users, '/members/remove', :update],
[:reload_data, '', :find]

class << self
def endpoint(team_id, team_user_group_id = nil, *actions)
Expand Down
3 changes: 3 additions & 0 deletions lib/ruby-lokalise-api/resources/translation_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
module Lokalise
module Resources
class TranslationProvider < Base
supports [:reload_data, '', :find]
ID_KEY = 'provider'

class << self
def endpoint(team_id, provider_id = nil)
path_from teams: team_id,
Expand Down
8 changes: 4 additions & 4 deletions lib/ruby-lokalise-api/rest/team_user_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def destroy_team_user_group(team_id, group_id)
def add_projects_to_group(team_id, group_id, project_ids)
c_r Lokalise::Resources::TeamUserGroup, :update,
[team_id, group_id, 'projects', 'add'],
project_ids, :projects
project_ids, :projects, [team_id, group_id]
end

# Removes projects from the given group
Expand All @@ -75,7 +75,7 @@ def add_projects_to_group(team_id, group_id, project_ids)
def remove_projects_from_group(team_id, group_id, project_ids)
c_r Lokalise::Resources::TeamUserGroup, :update,
[team_id, group_id, 'projects', 'remove'],
project_ids, :projects
project_ids, :projects, [team_id, group_id]
end

# Adds users to the given group
Expand All @@ -88,7 +88,7 @@ def remove_projects_from_group(team_id, group_id, project_ids)
def add_users_to_group(team_id, group_id, users_ids)
c_r Lokalise::Resources::TeamUserGroup, :update,
[team_id, group_id, 'members', 'add'],
users_ids, :users
users_ids, :users, [team_id, group_id]
end

# Removes users from the given group
Expand All @@ -101,7 +101,7 @@ def add_users_to_group(team_id, group_id, users_ids)
def remove_users_from_group(team_id, group_id, users_ids)
c_r Lokalise::Resources::TeamUserGroup, :update,
[team_id, group_id, 'members', 'remove'],
users_ids, :users
users_ids, :users, [team_id, group_id]
end
end
end
2 changes: 1 addition & 1 deletion lib/ruby-lokalise-api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Lokalise
VERSION = '3.0.0'
VERSION = '3.1.0'
end
Loading

0 comments on commit 426f112

Please sign in to comment.