Class: Crowdin::Client

Inherits:
Object
  • Object
show all
Includes:
Web::FetchAllExtensions
Defined in:
lib/crowdin-api/client/client.rb,
lib/crowdin-api/client/version.rb

Overview

Example

require 'crowdin-api'

crowdin = Crowdin::Client.new do |config|
  config.api_token = 'YOUR_API_TOKEN'
end

crowdin.list_projects

Constant Summary collapse

VERSION =
'1.11.0'

Constants included from Web::FetchAllExtensions

Web::FetchAllExtensions::MAX_ITEMS_COUNT_PER_REQUEST

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Client

Returns a new instance of Client.



44
45
46
47
48
49
50
51
# File 'lib/crowdin-api/client/client.rb', line 44

def initialize(&block)
  build_configuration(&block)

  update_logger
  update_rest_client_proxy

  build_connection
end

Instance Attribute Details

#configObject (readonly)

Config instance that includes configuration options for the Client



36
37
38
# File 'lib/crowdin-api/client/client.rb', line 36

def config
  @config
end

#connectionObject (readonly)

Instance with established connection through RestClient to the Crowdin API



38
39
40
# File 'lib/crowdin-api/client/client.rb', line 38

def connection
  @connection
end

#loggerObject

Logger instance



42
43
44
# File 'lib/crowdin-api/client/client.rb', line 42

def logger
  @logger
end

#optionsObject (readonly)

Instance with options and headers for RestClient connection



40
41
42
# File 'lib/crowdin-api/client/client.rb', line 40

def options
  @options
end

Instance Method Details

#enterprise_mode?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/crowdin-api/client/client.rb', line 64

def enterprise_mode?
  !!config.organization_domain
end

#fetch_all(api_resource, opts = {}, retry_opts = {}) ⇒ Object

FetchAll options:

  • limit, Integer, default: 500 | How many records need to load per one request

  • offset, Integer, default: 0 | How many records need to skip

  • request_delay, Integer (seconds), default: 0 | Delay between requests

Note: Please, specify project_id while Client initialization if you need to use methods that need it within FetchAll

Example

@crowdin.fetch_all(:list_projects)

with specified options

@crowdin.fetch_all(:list_projects, { limit: 10, request_delay: 1 })

playing with response per fetch. Note: the block actually don’t make any effect to finite result

@crowdin.fetch_all(:list_projects, { limit: 10, request_delay: 1 }) { |response| puts response['data'] }

also you can specify retry configuration to handle some exceptions

Retry configuration options:

  • request_delay, Integer (seconds), default: 0 | Delay between retries

  • retries_count, Integer, default: 0

  • error_messages, Array

@crowdin.fetch_all(:list_projects, {}, { request_delay: 2, retries_count: 3, error_messages: ['401'] })

fetch all execution will be terminated if response error are same as in error_messages array otherwise system will retry so many times, as indicated at tries_count



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/crowdin-api/client/client.rb', line 105

def fetch_all(api_resource, opts = {}, retry_opts = {})
  unless api_resource.to_s.start_with?('list_')
    raise(Errors::FetchAllProcessingError, "#{api_resource} method aren't supported for FetchAll")
  end

  limit = opts[:limit] || Web::FetchAllExtensions::MAX_ITEMS_COUNT_PER_REQUEST
  offset = opts[:offset] || 0
  request_delay = opts[:request_delay] || 0

  retry_request_delay = retry_opts[:request_delay] || 0
  retries_count = retry_opts[:retries_count] || 0
  retry_error_messages = retry_opts[:error_messages] || []

  result = []
  loop do
    response = case api_resource
    when :list_terms
      send(api_resource, opts[:glossary_id], { limit: limit, offset: offset }.merge(opts))
    when :list_file_revisions
      send(api_resource, opts[:file_id], { limit: limit, offset: offset }.merge(opts))
    else
      send(api_resource, { limit: limit, offset: offset }.merge(opts))
    end

    if response.is_a?(String) && response.match('Something went wrong')
      if retries_count.positive?
        retry_error_messages.each do |message|
          break if response.match(message)
        end

        retries_count -= 1
        sleep retry_request_delay
      else
        raise(Errors::FetchAllProcessingError, response)
      end
    else
      yield response if block_given?
      deserialized_response = response['data']
      result.concat(deserialized_response)
      offset += deserialized_response.size
      break if deserialized_response.size < limit
    end

    sleep request_delay
  end
  result
rescue StandardError => e
  raise(Errors::FetchAllProcessingError, "FetchAll wasn't processed. Details - #{e.message}")
end

#log(message) ⇒ Object



53
54
55
# File 'lib/crowdin-api/client/client.rb', line 53

def log(message)
  !logger_enabled? || logger.debug(message)
end

#logger_enabled?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/crowdin-api/client/client.rb', line 68

def logger_enabled?
  config.logger_enabled?
end