Module: Eloquant::Helpers

Included in:
Client
Defined in:
lib/eloquant/concerns/helpers.rb

Instance Method Summary collapse

Instance Method Details

#check_export_status(sync_uri) ⇒ Object



67
68
69
# File 'lib/eloquant/concerns/helpers.rb', line 67

def check_export_status(sync_uri)
  get("/api/bulk/2.0#{sync_uri}")
end

#count_endpoint(endpoint) ⇒ Object



15
16
17
# File 'lib/eloquant/concerns/helpers.rb', line 15

def count_endpoint(endpoint)
  get("/api/rest/1.0/data/#{endpoint}", count: 1).try(:[], :total)
end

#create_bulk_export(endpoint, params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/eloquant/concerns/helpers.rb', line 27

def create_bulk_export(endpoint, params)
  response = initialize_bulk_export(endpoint, params)

  if response.has_key?(:failures)
    fail Eloquant::Errors::BulkExportCreationError.new(response[:failures])
  end

  export_uri = response[:uri]

  status_response = enqueue_export(export_uri)
  sync_uri        = status_response[:uri]

  status = status_response[:status]

  while status != "success"
    sleep(10)

    status_response = check_export_status(sync_uri)
    status          = status_response[:status]
  end

  sync_uri
end

#csv_custom_headersObject



3
4
5
# File 'lib/eloquant/concerns/helpers.rb', line 3

def csv_custom_headers
  { "Content-Type" => "text/csv" }
end

#describe_endpoint(endpoint) ⇒ Object



19
20
21
# File 'lib/eloquant/concerns/helpers.rb', line 19

def describe_endpoint(endpoint)
  get("/api/bulk/2.0/#{endpoint}/fields")
end

#describe_endpoint_csv(endpoint, params = {}) ⇒ Object



23
24
25
# File 'lib/eloquant/concerns/helpers.rb', line 23

def describe_endpoint_csv(endpoint, params = {})
  get("/api/bulk/2.0/#{endpoint}/fields", params, csv_custom_headers)
end

#enqueue_export(export_uri) ⇒ Object



61
62
63
64
65
# File 'lib/eloquant/concerns/helpers.rb', line 61

def enqueue_export(export_uri)
  params = { syncedInstanceUri: export_uri }

  json_post("/api/bulk/2.0/syncs", params)
end

#get_export_data(sync_uri, offset: nil) ⇒ Object



71
72
73
74
75
76
# File 'lib/eloquant/concerns/helpers.rb', line 71

def get_export_data(sync_uri, offset: nil)
  params = {}
  params[:offset] = offset if offset

  get("/api/bulk/2.0#{sync_uri}/data", params)
end

#initialize_bulk_export(endpoint, name: nil, fields: {}, filter: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/eloquant/concerns/helpers.rb', line 51

def initialize_bulk_export(endpoint, name: nil, fields: {}, filter: nil)
  params = {
    name:   name || "Eloquant #{endpoint.capitalize} Bulk Export",
    fields: fields,
  }
  params[:filter] = filter if !filter.nil?

  json_post("/api/bulk/2.0/#{endpoint}/exports", params)
end

#json_custom_headersObject



7
8
9
# File 'lib/eloquant/concerns/helpers.rb', line 7

def json_custom_headers
  { "Content-Type" => "application/json" }
end

#json_post(path, params) ⇒ Object



11
12
13
# File 'lib/eloquant/concerns/helpers.rb', line 11

def json_post(path, params)
  post(path, JSON.generate(params), json_custom_headers)
end

#list_bulk_exports(endpoint) ⇒ Object



78
79
80
# File 'lib/eloquant/concerns/helpers.rb', line 78

def list_bulk_exports(endpoint)
  get("/api/bulk/2.0/#{endpoint}/exports")
end