Class: PowerBI::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/power-bi/report.rb

Defined Under Namespace

Classes: ExportToFileError

Instance Attribute Summary collapse

Attributes inherited from Object

#id

Instance Method Summary collapse

Methods inherited from Object

instantiate_from_data, #reload, #set_attributes

Constructor Details

#initialize(tenant, parent, id = nil) ⇒ Report

Returns a new instance of Report.



7
8
9
10
11
# File 'lib/power-bi/report.rb', line 7

def initialize(tenant, parent, id = nil)
  super(tenant, id)
  @workspace = parent
  @pages = PageArray.new(@tenant, self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PowerBI::Object

Instance Attribute Details

#pagesObject (readonly)

Returns the value of attribute pages.



3
4
5
# File 'lib/power-bi/report.rb', line 3

def pages
  @pages
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



3
4
5
# File 'lib/power-bi/report.rb', line 3

def workspace
  @workspace
end

Instance Method Details

#clone(target_workspace, new_report_name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/power-bi/report.rb', line 30

def clone(target_workspace, new_report_name)
  data = @tenant.post("/reports/#{@id}/Clone") do |req|
    req.body = {
      name: new_report_name,
      targetWorkspaceId: target_workspace.id
    }.to_json
  end
  target_workspace.reports.reload
  data[:workspace] = target_workspace
  Report.new(@tenant, data)
end

#data_to_attributes(data) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/power-bi/report.rb', line 17

def data_to_attributes(data)
  {
    id: data[:id],
    report_type: data[:reportType],
    name: data[:name],
    web_url: data[:webUrl],
    embed_url: data[:embedUrl],
    is_from_pbix: data[:isFromPbix],
    is_owned_by_me: data[:isOwnedByMe],
    dataset_id: data[:datasetId],
  }
end

#embed_token(access_level: 'View', lifetime_in_minutes: 60) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/power-bi/report.rb', line 82

def embed_token(access_level: 'View', lifetime_in_minutes: 60)
  data = @tenant.post("/groups/#{workspace.id}/reports/#{id}/GenerateToken") do |req|
    req.body = {
      accessLevel: access_level,
      lifetimeInMinutes: lifetime_in_minutes
    }.to_json
  end
  {
    token: data[:token],
    expiration: Time.parse(data[:expiration])
  }
end

#export_to_file(filename, format: 'PDF', timeout: 300) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/power-bi/report.rb', line 51

def export_to_file(filename, format: 'PDF', timeout: 300)
  # post
  data = @tenant.post("/groups/#{workspace.id}/reports/#{id}/ExportTo") do |req|
    req.body = {
      format: format
    }.to_json
  end
  export_id = data[:id]

  # poll
  success = false
  iterations = 0
  status_history = ''
  old_status = ''
  while !success
    sleep 0.1
    iterations += 1
    raise ExportToFileError.new("Report export to file did not succeed after #{timeout} seconds. Status history:#{status_history}") if iterations > (10 * timeout)
    new_status = @tenant.get("/groups/#{workspace.id}/reports/#{id}/exports/#{export_id}")[:status].to_s
    success = (new_status == "Succeeded")
    if new_status != old_status
      status_history += "\nStatus change after #{iterations/10.0}s: '#{old_status}' --> '#{new_status}'"
      old_status = new_status
    end
  end

  # get and write file
  data = @tenant.get_raw("/groups/#{workspace.id}/reports/#{id}/exports/#{export_id}/file")
  File.open(filename, "wb") { |f| f.write(data) }
end

#get_data(id) ⇒ Object



13
14
15
# File 'lib/power-bi/report.rb', line 13

def get_data(id)
  @tenant.get("/groups/#{@workspace.id}/reports/#{id}")
end

#rebind(target_dataset) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/power-bi/report.rb', line 42

def rebind(target_dataset)
  @tenant.post("/groups/#{workspace.id}/reports/#{id}/Rebind") do |req|
    req.body = {
      datasetId: target_dataset.id
    }.to_json
  end
  true
end