Class: Embulk::Input::Zendesk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/input/zendesk/client.rb

Constant Summary collapse

PARTIAL_RECORDS_SIZE =
50
AVAILABLE_TARGETS =
%w(
  tickets ticket_events users organizations
  ticket_fields ticket_forms
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



15
16
17
# File 'lib/embulk/input/zendesk/client.rb', line 15

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/embulk/input/zendesk/client.rb', line 7

def config
  @config
end

Instance Method Details

#httpclientObject



19
20
21
22
23
# File 'lib/embulk/input/zendesk/client.rb', line 19

def httpclient
  httpclient = HTTPClient.new
  # httpclient.debug_dev = STDOUT
  return set_auth(httpclient)
end

#ticket_events(partial = true, start_time = 0, &block) ⇒ Object



63
64
65
66
67
# File 'lib/embulk/input/zendesk/client.rb', line 63

def ticket_events(partial = true, start_time = 0, &block)
  # NOTE: ticket_events only have incremental export API
  path = "/api/v2/incremental/ticket_events"
  incremental_export(path, "ticket_events", start_time, [], &block)
end

#ticket_fields(partial = true, start_time = 0, &block) ⇒ Object



69
70
71
72
73
# File 'lib/embulk/input/zendesk/client.rb', line 69

def ticket_fields(partial = true, start_time = 0, &block)
  # NOTE: ticket_fields only have export API (not incremental)
  path = "/api/v2/ticket_fields.json"
  export(path, "ticket_fields", 1000, &block)
end

#ticket_forms(partial = true, start_time = 0, &block) ⇒ Object



75
76
77
78
79
# File 'lib/embulk/input/zendesk/client.rb', line 75

def ticket_forms(partial = true, start_time = 0, &block)
  # NOTE: ticket_forms only have export API (not incremental)
  path = "/api/v2/ticket_forms.json"
  export(path, "ticket_forms", 1000, &block)
end

#validate_configObject



25
26
27
28
# File 'lib/embulk/input/zendesk/client.rb', line 25

def validate_config
  validate_credentials
  validate_target
end

#validate_credentialsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/embulk/input/zendesk/client.rb', line 30

def validate_credentials
  valid = case config[:auth_method]
  when "basic"
    config[:username] && config[:password]
  when "token"
    config[:username] && config[:token]
  when "oauth"
    config[:access_token]
  else
    raise Embulk::ConfigError.new("Unknown auth_method (#{config[:auth_method]}). Should pick one from 'basic', 'token' or 'oauth'.")
  end

  unless valid
    raise Embulk::ConfigError.new("Missing required credentials for #{config[:auth_method]}")
  end
end

#validate_targetObject



47
48
49
50
51
# File 'lib/embulk/input/zendesk/client.rb', line 47

def validate_target
  unless AVAILABLE_TARGETS.include?(config[:target])
    raise Embulk::ConfigError.new("target: '#{config[:target]}' is not supported.")
  end
end