seiun 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91891c974d9bc379c22e7898c44eea9bcd842a83
4
- data.tar.gz: 9adeee1ecea3a7d4be408742db193b8ad4308492
3
+ metadata.gz: fd4d5b8aaf37821bb1efec1af1fb64efe71ef7d0
4
+ data.tar.gz: 8aef3925e79e43429f865114a63a09dccbd40165
5
5
  SHA512:
6
- metadata.gz: fae81b3efd78c7994b9ab669021572597ec4864da079af25e11470abf6dc729286a32caac1c46ccc84bd6849b922ed7816b37a33b6ac7d061001e6be5c9fd96f
7
- data.tar.gz: 303c872863a668447e83fd07d98f1da1a0b9b89162132c9beb1e52433f06c54507460ce8aa5bd8f73ee70db58ad0e9fff71cddfd98b420391fdafd1f09f71232
6
+ metadata.gz: 9fd8129d8785ea5914db0649e597cc04bbcaa56555ba9b472a62644acb48338d6f2b10d9c5340bcfbb7359987524cf4c6033cc668c133e4962fd6bec4f26e48f
7
+ data.tar.gz: 96917774a6a228ad1c1f1acf880582a50d2e0c8b6473f3d0c2991a889ae72199c58a1473ea0179e6ce13bffd198b2838c23025ee540b64cbb2d5182e7b7d93c3
@@ -5,6 +5,10 @@ module Seiun
5
5
  @class = callback_class
6
6
  end
7
7
 
8
+ def job=(job)
9
+ @job = job
10
+ end
11
+
8
12
  def after_build_xml(xml)
9
13
  return unless callback_defined?(:after_build_xml)
10
14
  @class.__send__(callbacks[:after_build_xml], xml)
@@ -27,46 +31,50 @@ module Seiun
27
31
 
28
32
  def mock_response_create_job
29
33
  return unless callback_defined?(:mock_response_create_job)
30
- @class.__send__(callbacks[:mock_response_create_job])
34
+ @class.__send__(callbacks[:mock_response_create_job], operation: operation)
31
35
  end
32
36
 
33
37
  def mock_response_close_job
34
38
  return unless callback_defined?(:mock_response_close_job)
35
- @class.__send__(callbacks[:mock_response_close_job])
39
+ @class.__send__(callbacks[:mock_response_close_job], operation: operation)
36
40
  end
37
41
 
38
42
  def mock_response_add_query
39
43
  return unless callback_defined?(:mock_response_add_query)
40
- @class.__send__(callbacks[:mock_response_add_query])
44
+ @class.__send__(callbacks[:mock_response_add_query], operation: operation)
41
45
  end
42
46
 
43
47
  def mock_response_add_batch
44
48
  return unless callback_defined?(:mock_response_add_batch)
45
- @class.__send__(callbacks[:mock_response_add_batch])
49
+ @class.__send__(callbacks[:mock_response_add_batch], operation: operation)
46
50
  end
47
51
 
48
52
  def mock_response_get_job_details
49
53
  return unless callback_defined?(:mock_response_get_job_details)
50
- @class.__send__(callbacks[:mock_response_get_job_details])
54
+ @class.__send__(callbacks[:mock_response_get_job_details], operation: operation)
51
55
  end
52
56
 
53
57
  def mock_response_get_batch_details
54
58
  return unless callback_defined?(:mock_response_get_batch_details)
55
- @class.__send__(callbacks[:mock_response_get_batch_details])
59
+ @class.__send__(callbacks[:mock_response_get_batch_details], operation: operation)
56
60
  end
57
61
 
58
62
  def mock_response_get_result
59
63
  return unless callback_defined?(:mock_response_get_result)
60
- @class.__send__(callbacks[:mock_response_get_result])
64
+ @class.__send__(callbacks[:mock_response_get_result], operation: operation)
61
65
  end
62
66
 
63
67
  def mock_response_get_query_result
64
68
  return unless callback_defined?(:mock_response_get_query_result)
65
- @class.__send__(callbacks[:mock_response_get_query_result])
69
+ @class.__send__(callbacks[:mock_response_get_query_result], operation: operation)
66
70
  end
67
71
 
68
72
  private
69
73
 
74
+ def operation
75
+ @job.operation(get: false)
76
+ end
77
+
70
78
  def callback_defined?(name)
71
79
  !!callbacks[name]
72
80
  end
data/lib/seiun/client.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  module Seiun
2
2
  class Client
3
- SEC_TO_WAIT_ASYNC = 60*10
4
- private_constant :SEC_TO_WAIT_ASYNC
5
-
6
3
  def initialize(databasedotcom: nil, batch_size: 10_000)
7
4
  @batch_size = batch_size
8
5
  @connection = Seiun::Connection.new(databasedotcom)
@@ -57,30 +54,26 @@ module Seiun
57
54
  operate(:query, table_name, soql: soql, callback: callback_class)
58
55
  end
59
56
 
57
+ def find_job(id, callback_class: nil)
58
+ callback_class = wrap_callback(callback_class)
59
+ Seiun::Job.new(@connection, id: id, callback: callback_class)
60
+ end
61
+
60
62
  private
61
63
 
62
64
  def operate(operation, object, records: [], soql: "", ext_field_name: nil, callback: nil, async: true)
63
- callback = Seiun::Callback::Wrapper.new(callback) if callback
65
+ callback = wrap_callback(callback)
64
66
  records = records.map{|r| Seiun::Callback::RecordWrapper.new(r) }
65
- job = Seiun::Job.new(@connection, operation, object, ext_field_name: ext_field_name, callback: callback)
67
+ job = Seiun::Job.new(@connection, operation: operation, object_name: object, ext_field_name: ext_field_name, callback: callback)
66
68
  job.post_creation
67
69
  operation == :query ? job.add_query(soql) : records.each_slice(@batch_size).each{|chunk| job.add_batch(chunk) }
68
70
  job.post_closing
69
- wait_asyc(job) if operation == :query || async == false
70
- operation == :query ? job.get_query_result : job
71
- end
72
-
73
- def wait_asyc(job)
74
- Timeout.timeout(sec_to_wait_async) do
75
- until job.all_batch_finished?
76
- job.get_batch_details
77
- sleep 1
78
- end
79
- end
71
+ job.wait_finish if async == false
72
+ operation == :query ? job.get_results : job
80
73
  end
81
74
 
82
- def sec_to_wait_async
83
- SEC_TO_WAIT_ASYNC
75
+ def wrap_callback(callback)
76
+ Seiun::Callback::Wrapper.new(callback) if callback
84
77
  end
85
78
  end
86
79
  end
@@ -20,6 +20,10 @@ module Seiun
20
20
  connect(:add_batch, data: data, job_id: job_id, callback: callback)
21
21
  end
22
22
 
23
+ def get_job_details(job_id, callback: nil)
24
+ connect(:get_job_details, job_id: job_id, callback: callback)
25
+ end
26
+
23
27
  def get_batch_details(job_id, callback: nil)
24
28
  connect(:get_batch_details, job_id: job_id, callback: callback)
25
29
  end
@@ -67,7 +71,7 @@ module Seiun
67
71
  case type
68
72
  when :create_job
69
73
  "/services/async/#{api_version}/job"
70
- when :close_job
74
+ when :close_job, :get_job_details
71
75
  "/services/async/#{api_version}/job/#{job_id}"
72
76
  when :add_query, :add_batch, :get_batch_details
73
77
  "/services/async/#{api_version}/job/#{job_id}/batch"
data/lib/seiun/job.rb CHANGED
@@ -1,14 +1,20 @@
1
1
  module Seiun
2
2
  class Job
3
- attr_reader :operation
3
+ SEC_TO_WAIT = 60*10 # 10 minutes
4
+ private_constant :SEC_TO_WAIT
4
5
 
5
- def initialize(connection, operation, object_name, id: nil, ext_field_name: nil, callback: nil)
6
+ attr_reader :id
7
+
8
+ def initialize(connection, operation: nil, object_name: nil, id: nil, ext_field_name: nil, callback: nil)
6
9
  @connection = connection
7
- @operation = operation
8
- @object_name = object_name
9
- @id = id if id
10
- @ext_field_name = ext_field_name if ext_field_name
11
- @callback = callback
10
+ @operation = operation.to_sym if operation
11
+ @object_name = object_name.to_s if object_name
12
+ @id = id
13
+ @ext_field_name = ext_field_name
14
+ if callback
15
+ callback.job = self
16
+ @callback = callback
17
+ end
12
18
  @batches = []
13
19
  end
14
20
 
@@ -32,32 +38,72 @@ module Seiun
32
38
  parse_batch_xml(response_body)
33
39
  end
34
40
 
35
- def get_batch_details
36
- response_body = @connection.get_batch_details(@id, callback: @callback)
37
- parse_batch_xml(response_body)
38
- end
39
-
40
- def get_query_result
41
- result = []
42
- @batches.each do |batch|
41
+ def each_result
42
+ wait_finish
43
+ batches.each do |batch|
43
44
  result_response_body = @connection.get_result(@id, batch.id, callback: @callback)
44
45
  Seiun::XMLParsers::ResultXML.each(result_response_body) do |result_response|
45
- response_body = @connection.get_query_result(@id, batch.id, result_response.result_id, callback: @callback)
46
- Seiun::XMLParsers::RecordXML.each(response_body) do |response|
47
- result << response.to_hash
46
+ if query?
47
+ response_body = @connection.get_query_result(@id, batch.id, result_response.result_id, callback: @callback)
48
+ Seiun::XMLParsers::RecordXML.each(response_body) do |response|
49
+ yield(response.to_hash)
50
+ end
51
+ else
52
+ yield(result_response.to_hash)
48
53
  end
49
54
  end
50
55
  end
51
- result
52
56
  end
53
57
 
54
- def all_batch_finished?
55
- raise "Batches are empty" if @batches.empty?
56
- @batches.all?{|batch| !["Queued", "InProgress"].include?(batch.sf_state) }
58
+ def get_results
59
+ results = []
60
+ each_result{|res| results << res }
61
+ results
62
+ end
63
+
64
+ def operation(get: true)
65
+ return @operation if @operation || get == false
66
+ get_job_details
67
+ @operation
68
+ end
69
+
70
+ def sf_state(get: true)
71
+ return @sf_state if @sf_state || get == false
72
+ get_job_details
73
+ @sf_state
74
+ end
75
+
76
+ def batches(get: true)
77
+ return @batches if !@batches.empty? || get == false
78
+ get_batch_details
79
+ @batches
80
+ end
81
+
82
+ def wait_finish
83
+ Timeout.timeout(sec_to_wait_finish) do
84
+ until closed?
85
+ get_job_details
86
+ sleep 1
87
+ end
88
+ until all_batch_finished?
89
+ get_batch_details
90
+ sleep 1
91
+ end
92
+ end
57
93
  end
58
94
 
59
95
  private
60
96
 
97
+ def get_job_details
98
+ response_body = @connection.get_job_details(@id, callback: @callback)
99
+ parse_job_xml(response_body)
100
+ end
101
+
102
+ def get_batch_details
103
+ response_body = @connection.get_batch_details(@id, callback: @callback)
104
+ parse_batch_xml(response_body)
105
+ end
106
+
61
107
  def create_job_xml
62
108
  Seiun::XMLGenerators::JobXML.create_job(@operation, @object_name, ext_field_name: @ext_field_name, callback: @callback)
63
109
  end
@@ -72,7 +118,9 @@ module Seiun
72
118
 
73
119
  def parse_job_xml(response_body)
74
120
  Seiun::XMLParsers::JobXML.each(response_body) do |response|
75
- @id = response.id || @id
121
+ @id ||= response.id
122
+ @operation = ( @operation || response.operation ).to_sym
123
+ @object_name ||= response.object
76
124
  @sf_created_at = response.created_date || @sf_created_at
77
125
  @sf_updated_at = response.system_modstamp || @sf_updated_at
78
126
  @sf_state = response.state || @sf_state
@@ -94,6 +142,25 @@ module Seiun
94
142
  end
95
143
  end
96
144
 
145
+ def sec_to_wait_finish
146
+ SEC_TO_WAIT
147
+ end
148
+
149
+ def closed?
150
+ sf_state == "Closed"
151
+ end
152
+
153
+ [ :insert, :update, :upsert, :delete, :query ].each do |symbol|
154
+ define_method "#{symbol}?" do
155
+ @operation == symbol
156
+ end
157
+ end
158
+
159
+ def all_batch_finished?
160
+ return true if batches.empty?
161
+ batches.all?{|batch| !["Queued", "InProgress"].include?(batch.sf_state) }
162
+ end
163
+
97
164
  class Batch
98
165
  attr_reader :id
99
166
  attr_accessor :job_id, :sf_state, :sf_state_message, :sf_created_at, :sf_updated_at, :number_records_processed
data/lib/seiun/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Seiun
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -2,8 +2,8 @@ module Seiun
2
2
  module XMLParsers
3
3
  class ResultXML < Base
4
4
  class << self
5
- def each(xml_str, &block)
6
- parse(xml_str, "result", block)
5
+ def each(xml_str, find_tag: "result", &block)
6
+ parse(xml_str, find_tag, block)
7
7
  end
8
8
  end
9
9
 
@@ -12,7 +12,7 @@ module Seiun
12
12
  result
13
13
  end
14
14
 
15
- [ :id, :success ].each do |name|
15
+ [ :id, :success, :created ].each do |name|
16
16
  define_method name do
17
17
  return unless result.is_a?(Hash)
18
18
  result[Seiun::Utils.camelize(name)]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seiun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naoki Watanabe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-10 00:00:00.000000000 Z
11
+ date: 2016-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: databasedotcom