Class: Crowdin::Client

Inherits:
Object
  • Object
show all
Includes:
Web::FetchAllExtensions, Web::GraphqlExtensions
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.14.0'

Constants included from Web::FetchAllExtensions

Web::FetchAllExtensions::MAX_ITEMS_COUNT_PER_REQUEST

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Web::GraphqlExtensions

#graphql

Constructor Details

#initialize(&block) ⇒ Client

Returns a new instance of Client.



39
40
41
42
43
44
45
46
# File 'lib/crowdin-api/client/client.rb', line 39

def initialize(&block)
  build_configuration(&block)

  update_logger
  update_rest_client_proxy

  build_connection
end

Instance Attribute Details

#configObject (readonly)

Client configuration, connection, request options, and logger instances



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

def config
  @config
end

#connectionObject (readonly)

Client configuration, connection, request options, and logger instances



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

def connection
  @connection
end

#loggerObject

Client configuration, connection, request options, and logger instances



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

def logger
  @logger
end

#optionsObject (readonly)

Client configuration, connection, request options, and logger instances



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

def options
  @options
end

Instance Method Details

#enterprise_mode?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/crowdin-api/client/client.rb', line 59

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



100
101
102
103
104
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
# File 'lib/crowdin-api/client/client.rb', line 100

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



48
49
50
# File 'lib/crowdin-api/client/client.rb', line 48

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

#logger_enabled?Boolean

Returns:

  • (Boolean)


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

def logger_enabled?
  config.logger_enabled?
end