Module: QuickbooksWebConnector

Extended by:
QuickbooksWebConnector
Included in:
QuickbooksWebConnector
Defined in:
lib/quickbooks_web_connector.rb,
lib/quickbooks_web_connector/job.rb,
lib/quickbooks_web_connector/user.rb,
lib/quickbooks_web_connector/config.rb,
lib/quickbooks_web_connector/engine.rb,
lib/quickbooks_web_connector/failure.rb,
lib/quickbooks_web_connector/version.rb,
lib/quickbooks_web_connector/json_coder.rb,
lib/quickbooks_web_connector/soap_wrapper.rb,
lib/quickbooks_web_connector/soap_wrapper/default.rb,
app/controllers/quickbooks_web_connector/qwc_controller.rb,
app/helpers/quickbooks_web_connector/application_helper.rb,
app/controllers/quickbooks_web_connector/soap_controller.rb,
lib/quickbooks_web_connector/soap_wrapper/defaultServant.rb,
lib/quickbooks_web_connector/soap_wrapper/QBWebConnectorSvc.rb,
lib/quickbooks_web_connector/soap_wrapper/defaultMappingRegistry.rb,
app/controllers/quickbooks_web_connector/quickbooks_web_connector_controller.rb

Defined Under Namespace

Modules: ApplicationHelper, SoapWrapper Classes: Configuration, DecodeException, EncodeException, Engine, Failure, Job, JsonCoder, QuickbooksWebConnectorController, QwcController, SoapController, User

Constant Summary collapse

VERSION =
"0.6.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#coderObject

Encapsulation of encode/decode. Overwrite this to use it across QuickbooksWebConnector. This defaults to JSON for backwards compatibilty.



60
61
62
# File 'lib/quickbooks_web_connector.rb', line 60

def coder
  @coder ||= JsonCoder.new
end

Class Method Details

.configObject

Global settings for QuickbooksWebConnector



13
14
15
# File 'lib/quickbooks_web_connector/config.rb', line 13

def self.config
  @config
end

.configure {|@config ||= QuickbooksWebConnector::Configuration.new| ... } ⇒ Object

Configure global settings for QuickbooksWebConnector

QuickbooksWebConnector.configure do |config|
  config.server_version
end


8
9
10
# File 'lib/quickbooks_web_connector/config.rb', line 8

def self.configure(&block)
  yield @config ||= QuickbooksWebConnector::Configuration.new
end

.reset_configuration!Object



17
18
19
20
# File 'lib/quickbooks_web_connector/config.rb', line 17

def self.reset_configuration!
  @config = QuickbooksWebConnector::Configuration.new
  set_default_configuration
end

.set_default_configurationObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/quickbooks_web_connector/config.rb', line 22

def self.set_default_configuration
  configure do |config|
    config.server_version = '1.0.0'
    config.minimum_web_connector_client_version = nil

    config.parent_controller = 'ApplicationController'

    config.app_name = 'My QBWC App'
    config.app_description = 'My QBWC App Description'
  end
end

Instance Method Details

#clear_job_count_for_sessionObject

Clear the temporarily stored count of jobs for the sync session.



103
104
105
# File 'lib/quickbooks_web_connector.rb', line 103

def clear_job_count_for_session
  redis.del :queue_size
end

#decode(object) ⇒ Object



170
171
172
# File 'lib/quickbooks_web_connector.rb', line 170

def decode(object)
  coder.decode object
end

#dequeue(request_builder, response_handler, *args) ⇒ Object

This method can be used to conveniently remove a job from the queue.



77
78
79
# File 'lib/quickbooks_web_connector.rb', line 77

def dequeue(request_builder, response_handler, *args)
  Job.destroy(request_builder, response_handler, *args)
end

#encode(object) ⇒ Object



166
167
168
# File 'lib/quickbooks_web_connector.rb', line 166

def encode(object)
  coder.encode object
end

#enqueue(request_builder, response_handler, *args) ⇒ Object

This method can be used to conveniently add a job to the queue. It assumes the class you’re passing it is a real Ruby class (not a string or reference).



72
73
74
# File 'lib/quickbooks_web_connector.rb', line 72

def enqueue(request_builder, response_handler, *args)
  Job.create(request_builder, response_handler, *args)
end

#job_count_for_sessionObject

Fetch the saved number of jobs for the session



98
99
100
# File 'lib/quickbooks_web_connector.rb', line 98

def job_count_for_session
  redis.get(:queue_size).to_i
end

#list_range(key, start = 0, stop = -1)) ⇒ Object

Does the dirty work of fetching a range of items from a Redis list and converting them into Ruby objects



160
161
162
163
164
# File 'lib/quickbooks_web_connector.rb', line 160

def list_range(key, start = 0, stop = -1)
  Array(redis.lrange(key, start, stop)).map do |item|
    decode item
  end
end

#peekObject

Returns the next item currently queued, without removing it.



149
150
151
# File 'lib/quickbooks_web_connector.rb', line 149

def peek
  decode redis.lindex :queue, 0
end

#popObject

Pops a job off the queue.

Returns a Ruby object.



139
140
141
# File 'lib/quickbooks_web_connector.rb', line 139

def pop
  decode redis.lpop(:queue)
end

#push(item) ⇒ Object

The ‘item` is expected to be a hash with the following keys:

  xml - The XML to send to Quickbooks as a String.
class - The String name of the response handler.
 args - An Array of arguments to pass the handler. Usually passed
        via `class.to_class.perform(*args)`.

Example

QuickbooksWebConnector.push('xml' => '<some><xml></xml></some>', class' => 'CustomerAddResponseHandler', 'args' => [ 35 ])

Returns nothing



132
133
134
# File 'lib/quickbooks_web_connector.rb', line 132

def push(item)
  redis.rpush :queue, encode(item)
end

#redisObject

Returns the current Redis connection. If none has been created, will create a new one.



52
53
54
55
56
# File 'lib/quickbooks_web_connector.rb', line 52

def redis
  return @redis if @redis
  self.redis = Redis.respond_to?(:connect) ? Redis.connect(:thread_safe => true) : "localhost:6379"
  self.redis
end

#redis=(server) ⇒ Object

Accepts:

1. A 'hostname:port' String
2. A 'hostname:port:db' String (to select the Redis db)
3. A 'hostname:port/namespace' String (to set the Redis namespace)
4. A Redis URL String 'redis://host:port'
5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`,
   or `Redis::Namespace`.


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

def redis=(server)
  case server
  when String
    if server['redis://']
      redis = Redis.connect(:url => server, :thread_safe => true)
    else
      server, namespace = server.split('/', 2)
      host, port, db = server.split(':')
      redis = Redis.new(:host => host, :port => port,
        :thread_safe => true, :db => db)
    end
    namespace ||= :qwc

    @redis = Redis::Namespace.new(namespace, :redis => redis)
  when Redis::Namespace
    @redis = server
  else
    @redis = Redis::Namespace.new(:qwc, :redis => server)
  end
end

#remove(item) ⇒ Object

Delete any matching items



154
155
156
# File 'lib/quickbooks_web_connector.rb', line 154

def remove(item)
  redis.lrem :queue, 0, encode(item)
end

#reserveObject

This method will return a ‘QuickbooksWebConnector::Job` object or a non-true value depending on whether a job can be obtained.



83
84
85
# File 'lib/quickbooks_web_connector.rb', line 83

def reserve
  Job.reserve
end

#session_progressObject

Figure out how many jobs are left based on the queue size when we started and how many of them are left



109
110
111
112
# File 'lib/quickbooks_web_connector.rb', line 109

def session_progress
  completed_jobs_count = job_count_for_session - QuickbooksWebConnector.size
  (completed_jobs_count.fdiv(job_count_for_session) * 100).ceil
end

#sizeObject

Returns an integer representing the size of the queue.



144
145
146
# File 'lib/quickbooks_web_connector.rb', line 144

def size
  redis.llen :queue
end

#store_job_count_for_sessionObject

Store how many jobs we’re starting with so that during the sync, we can determine the progress we’ve made.



93
94
95
# File 'lib/quickbooks_web_connector.rb', line 93

def store_job_count_for_session
  redis.set :queue_size, QuickbooksWebConnector.size
end