gooddata 0.6.0 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (208) hide show
  1. checksums.yaml +13 -5
  2. data/.rubocop.yml +23 -0
  3. data/.travis.yml +9 -4
  4. data/CLI.md +439 -0
  5. data/Gemfile +0 -1
  6. data/README.md +2 -2
  7. data/Rakefile +60 -8
  8. data/doc/templates/default/module/setup.rb +1 -1
  9. data/examples.rb +2 -0
  10. data/gooddata +2 -0
  11. data/gooddata.gemspec +12 -8
  12. data/lib/gooddata.rb +0 -2
  13. data/lib/gooddata/bricks/base_downloader.rb +52 -47
  14. data/lib/gooddata/bricks/brick.rb +20 -31
  15. data/lib/gooddata/bricks/bricks.rb +1 -1
  16. data/lib/gooddata/bricks/middleware/base_middleware.rb +9 -7
  17. data/lib/gooddata/bricks/middleware/bench_middleware.rb +12 -10
  18. data/lib/gooddata/bricks/middleware/bulk_salesforce_middleware.rb +28 -28
  19. data/lib/gooddata/bricks/middleware/fs_upload_middleware.rb +20 -16
  20. data/lib/gooddata/bricks/middleware/gooddata_middleware.rb +21 -19
  21. data/lib/gooddata/bricks/middleware/logger_middleware.rb +10 -8
  22. data/lib/gooddata/bricks/middleware/restforce_middleware.rb +36 -34
  23. data/lib/gooddata/bricks/middleware/stdout_middleware.rb +11 -9
  24. data/lib/gooddata/bricks/middleware/twitter_middleware.rb +14 -12
  25. data/lib/gooddata/bricks/pipeline.rb +28 -0
  26. data/lib/gooddata/bricks/utils.rb +10 -8
  27. data/lib/gooddata/cli/cli.rb +1 -6
  28. data/lib/gooddata/cli/commands/auth_cmd.rb +1 -1
  29. data/lib/gooddata/cli/commands/console_cmd.rb +7 -5
  30. data/lib/gooddata/cli/commands/domain_cmd.rb +45 -0
  31. data/lib/gooddata/cli/commands/process_cmd.rb +42 -5
  32. data/lib/gooddata/cli/commands/project_cmd.rb +96 -36
  33. data/lib/gooddata/cli/commands/projects_cmd.rb +21 -0
  34. data/lib/gooddata/cli/commands/role_cmd.rb +28 -0
  35. data/lib/gooddata/cli/commands/run_ruby_cmd.rb +5 -5
  36. data/lib/gooddata/cli/commands/scaffold_cmd.rb +1 -1
  37. data/lib/gooddata/cli/commands/{profile_cmd.rb → user_cmd.rb} +7 -9
  38. data/lib/gooddata/cli/shared.rb +3 -2
  39. data/lib/gooddata/client.rb +16 -304
  40. data/lib/gooddata/commands/api.rb +13 -5
  41. data/lib/gooddata/commands/auth.rb +47 -40
  42. data/lib/gooddata/commands/base.rb +4 -2
  43. data/lib/gooddata/commands/commands.rb +1 -1
  44. data/lib/gooddata/commands/datasets.rb +20 -7
  45. data/lib/gooddata/commands/domain.rb +23 -0
  46. data/lib/gooddata/commands/process.rb +23 -117
  47. data/lib/gooddata/commands/project.rb +147 -0
  48. data/lib/gooddata/commands/projects.rb +8 -102
  49. data/lib/gooddata/commands/role.rb +26 -0
  50. data/lib/gooddata/commands/runners.rb +41 -38
  51. data/lib/gooddata/commands/scaffold.rb +46 -43
  52. data/lib/gooddata/commands/user.rb +33 -0
  53. data/lib/gooddata/connection.rb +43 -353
  54. data/lib/gooddata/core/connection.rb +389 -0
  55. data/lib/gooddata/core/core.rb +5 -4
  56. data/lib/gooddata/core/logging.rb +48 -0
  57. data/lib/gooddata/core/nil_logger.rb +13 -0
  58. data/lib/gooddata/core/project.rb +70 -0
  59. data/lib/gooddata/core/rest.rb +120 -0
  60. data/lib/gooddata/core/threaded.rb +14 -0
  61. data/lib/gooddata/core/user.rb +19 -0
  62. data/lib/gooddata/data/data.rb +2 -1
  63. data/lib/gooddata/data/guesser.rb +16 -12
  64. data/lib/gooddata/exceptions/command_failed.rb +1 -1
  65. data/lib/gooddata/exceptions/exceptions.rb +2 -1
  66. data/lib/gooddata/exceptions/no_project_error.rb +11 -0
  67. data/lib/gooddata/exceptions/project_not_found.rb +1 -1
  68. data/lib/gooddata/extensions/big_decimal.rb +6 -2
  69. data/lib/gooddata/extract.rb +10 -8
  70. data/lib/gooddata/goodzilla/goodzilla.rb +61 -59
  71. data/lib/gooddata/helpers.rb +15 -9
  72. data/lib/gooddata/models/account_settings.rb +124 -0
  73. data/lib/gooddata/models/attributes/anchor.rb +37 -0
  74. data/lib/gooddata/models/attributes/attributes.rb +8 -0
  75. data/lib/gooddata/models/attributes/date_attribute.rb +25 -0
  76. data/lib/gooddata/models/attributes/time_attribute.rb +24 -0
  77. data/lib/gooddata/models/columns/attribute.rb +71 -0
  78. data/lib/gooddata/models/columns/columns.rb +8 -0
  79. data/lib/gooddata/models/columns/date_column.rb +63 -0
  80. data/lib/gooddata/models/columns/fact_model.rb +54 -0
  81. data/lib/gooddata/models/columns/label.rb +55 -0
  82. data/lib/gooddata/models/columns/reference.rb +57 -0
  83. data/lib/gooddata/models/dashboard_builder.rb +26 -0
  84. data/lib/gooddata/models/data_result.rb +10 -9
  85. data/lib/gooddata/models/domain.rb +131 -0
  86. data/lib/gooddata/models/empty_result.rb +5 -8
  87. data/lib/gooddata/models/facts/facts.rb +8 -0
  88. data/lib/gooddata/models/facts/time_fact.rb +20 -0
  89. data/lib/gooddata/models/folders/attribute_folder.rb +20 -0
  90. data/lib/gooddata/models/folders/fact_folder.rb +20 -0
  91. data/lib/gooddata/models/folders/folders.rb +8 -0
  92. data/lib/gooddata/models/invitation.rb +78 -0
  93. data/lib/gooddata/models/links.rb +6 -6
  94. data/lib/gooddata/models/md_object.rb +25 -0
  95. data/lib/gooddata/models/metadata.rb +160 -62
  96. data/lib/gooddata/models/metadata/attribute.rb +81 -0
  97. data/lib/gooddata/models/metadata/column.rb +61 -0
  98. data/lib/gooddata/models/{dashboard.rb → metadata/dashboard.rb} +12 -7
  99. data/lib/gooddata/models/{data_set.rb → metadata/data_set.rb} +5 -4
  100. data/lib/gooddata/models/metadata/date_dimension.rb +26 -0
  101. data/lib/gooddata/models/metadata/display_form.rb +61 -0
  102. data/lib/gooddata/models/metadata/fact.rb +36 -0
  103. data/lib/gooddata/models/metadata/folder.rb +24 -0
  104. data/lib/gooddata/models/metadata/metadata.rb +8 -0
  105. data/lib/gooddata/models/metadata/metric.rb +197 -0
  106. data/lib/gooddata/models/metadata/report.rb +115 -0
  107. data/lib/gooddata/models/{report_definition.rb → metadata/report_definition.rb} +16 -10
  108. data/lib/gooddata/models/metadata/schema.rb +227 -0
  109. data/lib/gooddata/models/model.rb +38 -1339
  110. data/lib/gooddata/models/models.rb +5 -2
  111. data/lib/gooddata/models/module_constants.rb +29 -0
  112. data/lib/gooddata/models/process.rb +142 -13
  113. data/lib/gooddata/models/profile.rb +4 -6
  114. data/lib/gooddata/models/project.rb +406 -136
  115. data/lib/gooddata/models/project_blueprint.rb +221 -0
  116. data/lib/gooddata/models/project_builder.rb +136 -0
  117. data/lib/gooddata/models/project_creator.rb +138 -0
  118. data/lib/gooddata/models/project_metadata.rb +11 -10
  119. data/lib/gooddata/models/project_role.rb +92 -0
  120. data/lib/gooddata/models/references/date_reference.rb +44 -0
  121. data/lib/gooddata/models/references/references.rb +8 -0
  122. data/lib/gooddata/models/references/time_reference.rb +13 -0
  123. data/lib/gooddata/models/report_data_result.rb +11 -11
  124. data/lib/gooddata/models/schedule.rb +284 -0
  125. data/lib/gooddata/models/schema_blueprint.rb +158 -0
  126. data/lib/gooddata/models/schema_builder.rb +81 -0
  127. data/lib/gooddata/models/tab_builder.rb +23 -0
  128. data/lib/gooddata/models/user.rb +165 -0
  129. data/lib/gooddata/version.rb +1 -1
  130. data/lib/templates/project/data/devs.csv +1 -1
  131. data/lib/templates/project/data/repos.csv +1 -1
  132. data/lib/templates/project/model/model.rb.erb +7 -11
  133. data/spec/bricks/bricks_spec.rb +2 -0
  134. data/spec/data/test-ci-data.csv +2 -0
  135. data/spec/data/test_project_model_spec.json +7 -27
  136. data/spec/helpers/blueprint_helper.rb +2 -0
  137. data/spec/helpers/cli_helper.rb +2 -0
  138. data/spec/helpers/connection_helper.rb +14 -1
  139. data/spec/helpers/project_helper.rb +16 -0
  140. data/spec/helpers/schema_helper.rb +16 -0
  141. data/spec/integration/command_projects_spec.rb +7 -7
  142. data/spec/integration/create_from_template_spec.rb +2 -2
  143. data/spec/integration/full_project_spec.rb +160 -7
  144. data/spec/integration/partial_md_export_import_spec.rb +3 -3
  145. data/spec/logging_in_logging_out_spec.rb +2 -1
  146. data/spec/spec_helper.rb +26 -4
  147. data/spec/unit/bricks/bricks_spec.rb +15 -7
  148. data/spec/unit/bricks/middleware/bench_middleware_spec.rb +2 -0
  149. data/spec/unit/bricks/middleware/bulk_salesforce_middleware_spec.rb +2 -0
  150. data/spec/unit/bricks/middleware/gooddata_middleware_spec.rb +2 -0
  151. data/spec/unit/bricks/middleware/logger_middleware_spec.rb +2 -0
  152. data/spec/unit/bricks/middleware/restforce_middleware_spec.rb +2 -0
  153. data/spec/unit/bricks/middleware/stdout_middleware_spec.rb +2 -0
  154. data/spec/unit/bricks/middleware/twitter_middleware_spec.rb +2 -0
  155. data/spec/unit/cli/cli_spec.rb +2 -0
  156. data/spec/unit/cli/commands/cmd_api_spec.rb +23 -15
  157. data/spec/unit/cli/commands/cmd_auth_spec.rb +8 -4
  158. data/spec/unit/cli/commands/cmd_domain_spec.rb +82 -0
  159. data/spec/unit/cli/commands/cmd_process_spec.rb +29 -13
  160. data/spec/unit/cli/commands/cmd_project_spec.rb +51 -30
  161. data/spec/unit/cli/commands/cmd_role_spec.rb +44 -0
  162. data/spec/unit/cli/commands/cmd_run_ruby_spec.rb +8 -4
  163. data/spec/unit/cli/commands/cmd_scaffold_spec.rb +48 -11
  164. data/spec/unit/cli/commands/cmd_user_spec.rb +29 -0
  165. data/spec/unit/commands/command_api_spec.rb +1 -1
  166. data/spec/unit/commands/command_auth_spec.rb +100 -18
  167. data/spec/unit/commands/command_dataset_spec.rb +4 -0
  168. data/spec/unit/commands/command_process_spec.rb +9 -4
  169. data/spec/unit/commands/command_projects_spec.rb +10 -6
  170. data/spec/unit/commands/command_scaffold_spec.rb +5 -1
  171. data/spec/unit/commands/command_user_spec.rb +22 -0
  172. data/spec/unit/core/connection_spec.rb +35 -6
  173. data/spec/unit/core/logging_spec.rb +65 -0
  174. data/spec/unit/core/nil_logger_spec.rb +9 -0
  175. data/spec/unit/core/project_spec.rb +51 -0
  176. data/spec/unit/core/rest_spec.rb +33 -0
  177. data/spec/unit/data/guesser_spec.rb +5 -0
  178. data/spec/unit/godzilla/goodzilla_spec.rb +2 -0
  179. data/spec/unit/models/account_settings_spec.rb +28 -0
  180. data/spec/unit/models/anchor_spec.rb +32 -0
  181. data/spec/unit/models/attribute_column_spec.rb +7 -0
  182. data/spec/unit/models/domain_spec.rb +45 -0
  183. data/spec/unit/models/invitation_spec.rb +13 -0
  184. data/spec/unit/models/md_object_spec.rb +47 -0
  185. data/spec/unit/models/metric.rb +92 -0
  186. data/spec/unit/{model → models}/model_spec.rb +9 -7
  187. data/spec/unit/models/project_blueprint_spec.rb +202 -0
  188. data/spec/unit/models/project_creator.rb +73 -0
  189. data/spec/unit/models/project_role_spec.rb +90 -0
  190. data/spec/unit/models/project_spec.rb +143 -0
  191. data/spec/unit/models/schedule_spec.rb +491 -0
  192. data/spec/unit/{model → models}/schema_builder_spec.rb +2 -0
  193. data/spec/unit/{model → models}/tools_spec.rb +13 -7
  194. data/spec/unit/models/user_spec.rb +16 -0
  195. data/test/test_upload.rb +2 -0
  196. metadata +189 -86
  197. data/lib/gooddata/commands/profile.rb +0 -11
  198. data/lib/gooddata/models/attribute.rb +0 -29
  199. data/lib/gooddata/models/display_form.rb +0 -9
  200. data/lib/gooddata/models/fact.rb +0 -19
  201. data/lib/gooddata/models/metric.rb +0 -99
  202. data/lib/gooddata/models/report.rb +0 -89
  203. data/spec/data/blueprint_valid.json +0 -37
  204. data/spec/unit/cli/commands/cmd_profile_spec.rb +0 -16
  205. data/spec/unit/commands/command_profile_spec.rb +0 -18
  206. data/spec/unit/core/core_spec.rb +0 -7
  207. data/spec/unit/model/blueprint_spec.rb +0 -132
  208. data/spec/unit/model/project_blueprint_spec.rb +0 -44
@@ -7,11 +7,14 @@ module GoodData
7
7
  ProjectMetadata[:all].keys
8
8
  end
9
9
 
10
- def [](key)
10
+ def [](key, options = {})
11
11
  if key == :all
12
12
  uri = "/gdc/projects/#{GoodData.project.pid}/dataload/metadata"
13
13
  res = GoodData.get(uri)
14
- res['metadataItems']['items'].reduce({}) { |memo, i| memo[i['metadataItem']['key']] = i['metadataItem']['value']; memo }
14
+ res['metadataItems']['items'].reduce({}) do |memo, i|
15
+ memo[i['metadataItem']['key']] = i['metadataItem']['value']
16
+ memo
17
+ end
15
18
  else
16
19
  uri = "/gdc/projects/#{GoodData.project.pid}/dataload/metadata/#{key}"
17
20
  res = GoodData.get(uri)
@@ -22,13 +25,11 @@ module GoodData
22
25
  alias_method :get, :[]
23
26
  alias_method :get_key, :[]
24
27
 
25
- def has_key?(key)
26
- begin
27
- ProjectMetadata[key]
28
- true
29
- rescue RestClient::ResourceNotFound => e
30
- false
31
- end
28
+ def key?(key)
29
+ ProjectMetadata[key]
30
+ true
31
+ rescue RestClient::ResourceNotFound
32
+ false
32
33
  end
33
34
 
34
35
  def []=(key, val)
@@ -41,7 +42,7 @@ module GoodData
41
42
  uri = "/gdc/projects/#{GoodData.project.pid}/dataload/metadata/"
42
43
  update_uri = uri + key
43
44
 
44
- if has_key?(key)
45
+ if key?(key)
45
46
  GoodData.put(update_uri, data)
46
47
  else
47
48
  GoodData.post(uri, data)
@@ -0,0 +1,92 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative 'account_settings'
4
+
5
+ module GoodData
6
+ class ProjectRole
7
+ def initialize(json)
8
+ @json = json
9
+ end
10
+
11
+ # Gets Project Role Identifier
12
+ #
13
+ # @return [string] Project Role
14
+ def identifier
15
+ @json['projectRole']['meta']['identifier']
16
+ end
17
+
18
+ # Gets Project Role Author
19
+ #
20
+ # @return [GoodData::AccountSettings] Project Role author
21
+ def author
22
+ url = @json['projectRole']['meta']['author']
23
+ tmp = GoodData.get url
24
+ GoodData::AccountSettings.new(tmp)
25
+ end
26
+
27
+ # Gets Project Role Contributor
28
+ #
29
+ # @return [GoodData::AccountSettings] Project Role Contributor
30
+ def contributor
31
+ url = @json['projectRole']['meta']['contributor']
32
+ tmp = GoodData.get url
33
+ GoodData::AccountSettings.new(tmp)
34
+ end
35
+
36
+ # Gets DateTime time when created
37
+ #
38
+ # @return [DateTime] Date time of creation
39
+ def created
40
+ DateTime.parse(@json['projectRole']['meta']['created'])
41
+ end
42
+
43
+ # Gets Project Role Permissions
44
+ #
45
+ # @return [string] Project Role
46
+ def permissions
47
+ @json['projectRole']['permissions']
48
+ end
49
+
50
+ # Gets Project Role Title
51
+ #
52
+ # @return [string] Project Role Title
53
+ def title
54
+ @json['projectRole']['meta']['title']
55
+ end
56
+
57
+ # Gets Project Role Summary
58
+ #
59
+ # @return [string] Project Role Summary
60
+ def summary
61
+ @json['projectRole']['meta']['summary']
62
+ end
63
+
64
+ # Gets DateTime time when updated
65
+ #
66
+ # @return [DateTime] Date time of last update
67
+ def updated
68
+ DateTime.parse(@json['projectRole']['meta']['updated'])
69
+ end
70
+
71
+ # Gets Users with this Role
72
+ #
73
+ # @return [Array<GoodData::AccountSettings>] List of users
74
+ def users
75
+ res = []
76
+ url = @json['projectRole']['links']['roleUsers']
77
+ tmp = GoodData.get url
78
+ tmp['associatedUsers']['users'].each do |user_url|
79
+ user = GoodData.get user_url
80
+ res << GoodData::AccountSettings.new(user)
81
+ end
82
+ res
83
+ end
84
+
85
+ # Gets Raw object URI
86
+ #
87
+ # @return [string] URI of this project role
88
+ def uri
89
+ @json['projectRole']['links']['roleUsers'].split('/')[0...-1].join('/')
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../columns/reference'
4
+
5
+ module GoodData
6
+ module Model
7
+ ##
8
+ # Date as a reference to a date dimension
9
+ #
10
+ class DateReference < Reference
11
+ attr_accessor :format, :output_format, :urn
12
+
13
+ def initialize(column, schema)
14
+ super column, schema
15
+ @output_format = column['format'] || 'dd/MM/yyyy'
16
+ @format = @output_format.gsub('yyyy', '%Y').gsub('MM', '%m').gsub('dd', '%d')
17
+ @urn = column[:urn] || 'URN:GOODDATA:DATE'
18
+ end
19
+
20
+ def identifier
21
+ @identifier ||= "#{@schema_ref}.#{DATE_ATTRIBUTE}"
22
+ end
23
+
24
+ def to_manifest_part(mode)
25
+ {
26
+ 'populates' => ["#{identifier}.#{DATE_ATTRIBUTE_DEFAULT_DISPLAY_FORM}"],
27
+ 'mode' => mode,
28
+ 'constraints' => { 'date' => output_format },
29
+ 'columnName' => name,
30
+ 'referenceKey' => 1
31
+ }
32
+ end
33
+
34
+ # def to_maql_create
35
+ # # urn:chefs_warehouse_fiscal:date
36
+ # super_maql = super
37
+ # maql = ""
38
+ # # maql = "# Include date dimensions\n"
39
+ # # maql += "INCLUDE TEMPLATE \"#{urn}\" MODIFY (IDENTIFIER \"#{name}\", TITLE \"#{title || name}\");\n"
40
+ # maql += super_maql
41
+ # end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'pathname'
4
+
5
+ base = Pathname(__FILE__).dirname.expand_path
6
+ Dir.glob(base + '*.rb').each do |file|
7
+ require file
8
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../columns/reference'
4
+
5
+ module GoodData
6
+ module Model
7
+ ##
8
+ # Time as a reference to a time-of-a-day dimension
9
+ #
10
+ class TimeReference < Reference
11
+ end
12
+ end
13
+ end
@@ -34,7 +34,7 @@ module GoodData
34
34
  to_table.each { |line| yield line }
35
35
  end
36
36
 
37
- alias :each_row :each_line
37
+ alias_method :each_row, :each_line
38
38
 
39
39
  def each_column
40
40
  table.each { |line| yield line }
@@ -48,11 +48,11 @@ module GoodData
48
48
  table.transpose
49
49
  end
50
50
 
51
- def [](index)
51
+ def [](index, options = {})
52
52
  to_table[index]
53
53
  end
54
54
 
55
- alias :row :[]
55
+ alias_method :row, :[]
56
56
 
57
57
  def column(index)
58
58
  table[index]
@@ -66,13 +66,12 @@ module GoodData
66
66
  table.include?(row)
67
67
  end
68
68
 
69
- def == (otherDataResult)
70
- result = true
69
+ def ==(other)
71
70
  csv_table = to_table
72
71
  len = csv_table.length
73
- table = otherDataResult.respond_to?(:to_table) ? otherDataResult.to_table : otherDataResult
72
+ table = other.respond_to?(:to_table) ? other.to_table : other
74
73
  return false if len != table.length
75
- diff(otherDataResult).empty?() ? true : false
74
+ diff(other).empty? ? true : false
76
75
  end
77
76
 
78
77
  def diff(otherDataResult)
@@ -81,12 +80,13 @@ module GoodData
81
80
  differences = []
82
81
 
83
82
  csv_table.each do |row|
84
- differences << row unless other_table.detect { |r| r == row }
83
+ differences << row unless other_table.find { |r| r == row }
85
84
  end
86
85
  differences
87
86
  end
88
87
 
89
88
  private
89
+
90
90
  def each_level(table, level, children, lookup)
91
91
  max_level = level + 1
92
92
  children.each do |kid|
@@ -101,8 +101,8 @@ module GoodData
101
101
  table[first + i][level] = lookup[level][kid['id'].to_s]
102
102
  end
103
103
  end
104
- if (!kid['children'].empty?)
105
- new_level = each_level(table, level+1, kid['children'], lookup)
104
+ unless kid['children'].empty?
105
+ new_level = each_level(table, level + 1, kid['children'], lookup)
106
106
  max_level = [max_level, new_level].max
107
107
  end
108
108
  end
@@ -136,7 +136,7 @@ module GoodData
136
136
  return headers, size # rubocop:disable RedundantReturn
137
137
  end
138
138
 
139
- def assemble_table()
139
+ def assemble_table
140
140
  # puts "=== COLUMNS === #{column_headers.size}x#{headers_height}"
141
141
  (column_headers.size).times do |i|
142
142
  (headers_height).times do |j|
@@ -0,0 +1,284 @@
1
+ # encoding: UTF-8
2
+
3
+ module GoodData
4
+ class Schedule
5
+ attr_reader :dirty
6
+
7
+ class << self
8
+ # Looks for schedule
9
+ # @param id [String] URL, ID of schedule or :all
10
+ # @return [GoodData::Schedule|Array<GoodData::Schedule>] List of schedules
11
+ def [](id, options = {})
12
+ if id == :all
13
+ GoodData::Schedule.all
14
+ else
15
+ if id =~ %r{\/gdc\/projects\/[a-zA-Z\d]+\/schedules\/?[a-zA-Z\d]*}
16
+ url = id
17
+ tmp = GoodData.get url
18
+ return GoodData::Schedule.new(tmp)
19
+ end
20
+
21
+ tmp = GoodData.get "/gdc/projects/#{GoodData.project.pid}/schedules/#{id}"
22
+ GoodData::Schedule.new(tmp)
23
+ end
24
+ end
25
+
26
+ # Returns list of all schedules for active project
27
+ # @return [Array<GoodData::Schedule>] List of schedules
28
+ def all
29
+ tmp = GoodData.get "/gdc/projects/#{GoodData.project.pid}/schedules"
30
+ tmp['schedules']['items'].map { |schedule| GoodData::Schedule.new(schedule) }
31
+ end
32
+
33
+ # Creates new schedules from parameters passed
34
+ #
35
+ # @param process_id [String] Process ID
36
+ # @param cron [String] Cron Settings
37
+ # @param executable [String] Execution executable
38
+ # @param options [Hash] Optional options
39
+ # @return [GoodData::Schedule] New GoodData::Schedule instance
40
+ def create(process_id, cron, executable, options = {})
41
+ default_opts = {
42
+ :type => 'MSETL',
43
+ :timezone => 'UTC',
44
+ :cron => cron,
45
+ :params => {
46
+ :process_id => process_id,
47
+ :executable => executable
48
+ },
49
+ :hidden_params => {}
50
+ }
51
+
52
+ inject_schema = {
53
+ :hidden_params => 'hiddenParams'
54
+ }
55
+
56
+ inject_params = {
57
+ :process_id => 'PROCESS_ID',
58
+ :executable => 'EXECUTABLE'
59
+ }
60
+
61
+ default_params = default_opts[:params].reduce({}) do |new_hash, (k, v)|
62
+ key = inject_params[k] || k
63
+ new_hash[key] = v
64
+ new_hash
65
+ end
66
+
67
+ default = default_opts.reduce({}) do |new_hash, (k, v)|
68
+ key = inject_schema[k] || k
69
+ new_hash[key] = v
70
+ new_hash
71
+ end
72
+
73
+ default[:params] = default_params
74
+
75
+ json = {
76
+ 'schedule' => default.merge(options)
77
+ }
78
+
79
+ tmp = json['schedule'][:params]['PROCESS_ID']
80
+ fail 'Process ID has to be provided' if tmp.nil? || tmp.empty?
81
+
82
+ tmp = json['schedule'][:params]['EXECUTABLE']
83
+ fail 'Executable has to be provided' if tmp.nil? || tmp.empty?
84
+
85
+ tmp = json['schedule'][:cron]
86
+ fail 'Cron schedule has to be provided' if tmp.nil? || tmp.empty?
87
+
88
+ tmp = json['schedule'][:timezone]
89
+ fail 'A timezone has to be provided' if tmp.nil? || tmp.empty?
90
+
91
+ tmp = json['schedule'][:type]
92
+ fail 'Schedule type has to be provided' if tmp.nil? || tmp.empty?
93
+
94
+ url = "/gdc/projects/#{GoodData.project.pid}/schedules"
95
+ res = GoodData.post url, json
96
+
97
+ fail 'Unable to create new schedule' if res.nil?
98
+
99
+ new_obj_json = GoodData.get res['schedule']['links']['self']
100
+ GoodData::Schedule.new(new_obj_json)
101
+ end
102
+ end
103
+
104
+ # Initializes object from raw json
105
+ #
106
+ # @param json [Object] Raw JSON
107
+ # @return [GoodData::Schedule] New GoodData::Schedule instance
108
+ def initialize(json)
109
+ @json = json
110
+ end
111
+
112
+ # Deletes schedule
113
+ def delete
114
+ GoodData.delete uri
115
+ end
116
+
117
+ # Executes schedule
118
+ #
119
+ # @return [Object] Raw Response
120
+ def execute
121
+ data = {
122
+ :execution => {}
123
+ }
124
+ GoodData.post execution_url, data
125
+ end
126
+
127
+ # Returns execution URL
128
+ #
129
+ # @return [String] Executions URL
130
+ def execution_url
131
+ @json['schedule']['links']['executions']
132
+ end
133
+
134
+ # Returns execution state
135
+ #
136
+ # @return [String] Execution state
137
+ def state
138
+ @json['schedule']['state']
139
+ end
140
+
141
+ # Returns execution timezone
142
+ #
143
+ # @return [String] Execution timezone
144
+ def timezone
145
+ @json['schedule']['timezone']
146
+ end
147
+
148
+ # Assigns execution timezone
149
+ #
150
+ # @param new_timezone [String] Timezone to be set
151
+ def timezone=(new_timezone)
152
+ @json['schedule']['timezone'] = new_timezone
153
+ @dirty = true
154
+ end
155
+
156
+ # Returns execution type
157
+ #
158
+ # @return [String] Execution type
159
+ def type
160
+ @json['schedule']['type']
161
+ end
162
+
163
+ # Assigns execution type
164
+ #
165
+ # @param new_type [String] Execution type to be set
166
+ def type=(new_type)
167
+ @json['schedule']['type'] = new_type
168
+ @dirty = true
169
+ end
170
+
171
+ # Returns execution cron settings
172
+ #
173
+ # @return [String] Cron settings
174
+ def cron
175
+ @json['schedule']['cron']
176
+ end
177
+
178
+ # Assigns execution cron settings
179
+ #
180
+ # @param new_cron [String] Cron settings to be set
181
+ def cron=(new_cron)
182
+ @json['schedule']['cron'] = new_cron
183
+ @dirty = true
184
+ end
185
+
186
+ # Returns execution process ID
187
+ #
188
+ # @return [String] Process ID
189
+ def process_id
190
+ @json['schedule']['params']['PROCESS_ID']
191
+ end
192
+
193
+ def process_id=(new_project_id)
194
+ @json['schedule']['params']['PROCESS_ID'] = new_project_id
195
+ @dirty = true
196
+ end
197
+
198
+ # Returns execution executable
199
+ #
200
+ # @return [String] Executable (graph) name
201
+ def executable
202
+ @json['schedule']['params']['EXECUTABLE']
203
+ end
204
+
205
+ # Assigns execution executable
206
+ #
207
+ # @param new_executable [String] Executable to be set.
208
+ def executable=(new_executable)
209
+ @json['schedule']['params']['EXECUTABLE'] = new_executable
210
+ @dirty = true
211
+ end
212
+
213
+ # Returns list of executions
214
+ #
215
+ # @return [Array] Raw Executions JSON
216
+ def executions
217
+ if @json
218
+ url = @json['schedule']['links']['executions']
219
+ res = GoodData.get url
220
+ res['executions']['items']
221
+ end
222
+ end
223
+
224
+ # Returns params as Hash
225
+ #
226
+ # @return [Hash] Parameters
227
+ def params
228
+ @json['schedule']['params']
229
+ end
230
+
231
+ # Assigns execution parameters
232
+ #
233
+ # @param params [String] Params to be set
234
+ def params=(new_param)
235
+ @json['schedule']['params'].merge!(new_param)
236
+ @dirty = true
237
+ end
238
+
239
+ # Returns hidden_params as Hash
240
+ #
241
+ # @return [Hash] Hidden Parameters
242
+ def hidden_params
243
+ @json['schedule']['hiddenParams'] || {}
244
+ end
245
+
246
+ # Assigns hidden parameters
247
+ #
248
+ # @param new_hidden_param [String] Hidden parameters to be set
249
+ def hidden_params=(new_hidden_param)
250
+ @json['schedule']['hiddenParams'] = hidden_params.merge(new_hidden_param)
251
+ @dirty = true
252
+ end
253
+
254
+ # Saves object if dirty
255
+ #
256
+ # @return [Boolean] True if saved
257
+ def save
258
+ if @dirty
259
+ update_json = {
260
+ 'schedule' => {
261
+ 'type' => @json['schedule']['type'],
262
+ 'timezone' => @json['schedule']['timezone'],
263
+ 'cron' => @json['schedule']['cron'],
264
+ 'params' => @json['schedule']['params'],
265
+ 'hiddenParams' => @json['schedule']['hiddenParams']
266
+ }
267
+ }
268
+ res = GoodData.put uri, update_json
269
+
270
+ @json = res
271
+ @dirty = false
272
+ return true
273
+ end
274
+ false
275
+ end
276
+
277
+ # Returns URL
278
+ #
279
+ # @return [String] Schedule URL
280
+ def uri
281
+ @json['schedule']['links']['self'] if @json && @json['schedule'] && @json['schedule']['links']
282
+ end
283
+ end
284
+ end