Class: Toolhound::Client

Inherits:
Object
  • Object
show all
Includes:
Authentication, Projects, Configurable, Util
Defined in:
lib/toolhound-ruby/client.rb,
lib/toolhound-ruby/client/users.rb,
lib/toolhound-ruby/client/projects.rb

Defined Under Namespace

Modules: Projects, Users

Constant Summary collapse

DB_TYPE_REGEX =
/^(int|dec|var|bol|dte|bin)/

Instance Attribute Summary collapse

Attributes included from Configurable

#timeout

Instance Method Summary collapse

Methods included from Projects

#projects

Methods included from Util

#acronym_regex, #acronyms, #camelize, #demodulize, #underscore

Methods included from Configurable

#configure, keys, #options, #reset!

Methods included from Authentication

#authenticatable?, #reconnect, #sign_in, #sign_out

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



40
41
42
43
44
45
46
47
48
# File 'lib/toolhound-ruby/client.rb', line 40

def initialize(options = {})

  # Use options passed in, but fall back to module defaults
  Toolhound::Configurable.keys.each do |key|
    instance_variable_set(:"@#{key}", options[key] || Toolhound.instance_variable_get(:"@#{key}"))
  end

   if authenticatable?
end

Instance Attribute Details

#connectionObject

def projects

end attr_accessor :access_token, :client_id, :uid, :expiry, :me



38
39
40
# File 'lib/toolhound-ruby/client.rb', line 38

def connection
  @connection
end

#dataserverObject

def projects

end attr_accessor :access_token, :client_id, :uid, :expiry, :me



38
39
40
# File 'lib/toolhound-ruby/client.rb', line 38

def dataserver
  @dataserver
end

#passwordObject

def projects

end attr_accessor :access_token, :client_id, :uid, :expiry, :me



38
39
40
# File 'lib/toolhound-ruby/client.rb', line 38

def password
  @password
end

#portObject

def projects

end attr_accessor :access_token, :client_id, :uid, :expiry, :me



38
39
40
# File 'lib/toolhound-ruby/client.rb', line 38

def port
  @port
end

#usernameObject

def projects

end attr_accessor :access_token, :client_id, :uid, :expiry, :me



38
39
40
# File 'lib/toolhound-ruby/client.rb', line 38

def username
  @username
end

Instance Method Details

#inspectObject

:nodoc:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/toolhound-ruby/client.rb', line 59

def inspect # :nodoc:

  inspected = super

  # mask password
  inspected = inspected.gsub! @password, "*******" if @password
  # Only show last 4 of token, secret
  # if @access_token
  #   inspected = inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}"
  # end
  # if @client_secret
  #   inspected = inspected.gsub! @client_secret, "#{'*'*36}#{@client_secret[36..-1]}"
  # end

  inspected
end

#inventoryObject



111
112
113
# File 'lib/toolhound-ruby/client.rb', line 111

def inventory
  @inventory ||= Toolhound::Inventory.new(self)
end

#inventory_itemObject



115
116
117
# File 'lib/toolhound-ruby/client.rb', line 115

def inventory_item
  @inventory_item ||= Toolhound::InventoryItem.new(self)
end

#jobObject



103
104
105
# File 'lib/toolhound-ruby/client.rb', line 103

def job
  @job ||= Toolhound::Job.new(self)
end

#manufacturerObject



141
142
143
# File 'lib/toolhound-ruby/client.rb', line 141

def manufacturer
  @vendor ||= Toolhound::Manufacturer.new(self)
end

#nearmiss_warn(*message) ⇒ nil

Wrapper around Kernel#warn to print warnings unless TOOLHOUND_SILENT is set to true.

Returns:

  • (nil)


208
209
210
211
212
# File 'lib/toolhound-ruby/client.rb', line 208

def nearmiss_warn(*message)
  unless ENV['TOOLHOUND_SILENT']
    warn message
  end
end

#projectObject



123
124
125
# File 'lib/toolhound-ruby/client.rb', line 123

def project
  @project ||= Toolhound::Project.new(self)
end

#purchase_orderObject



144
145
146
# File 'lib/toolhound-ruby/client.rb', line 144

def purchase_order
  @vendor ||= Toolhound::PurchaseOrder.new(self)
end

#purchase_receiptObject



119
120
121
# File 'lib/toolhound-ruby/client.rb', line 119

def purchase_receipt
  @purchase_receipt ||= Toolhound::PurchaseReceipt.new(self)
end

#query(query, options = {}) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/toolhound-ruby/client.rb', line 150

def query(query, options = {})
  klass = self



  results = connection.execute(query)
  results.map do |row|
    transform_attributes(row)
  end
end

#rentalObject



127
128
129
# File 'lib/toolhound-ruby/client.rb', line 127

def rental
  @rental ||= Toolhound::Rental.new(self)
end

#rental_chargeObject



134
135
136
# File 'lib/toolhound-ruby/client.rb', line 134

def rental_charge
  @rental_charge ||= Toolhound::RentalCharge.new(self)
end

#rental_itemObject



131
132
133
# File 'lib/toolhound-ruby/client.rb', line 131

def rental_item
  @rental_item ||= Toolhound::RentalItem.new(self)
end

#reset_connectionObject



99
100
101
# File 'lib/toolhound-ruby/client.rb', line 99

def reset_connection
  @connection = nil
end

#same_options?(opts) ⇒ Boolean

Compares client options to a Hash of requested options

Parameters:

  • opts (Hash)

    Options to compare with current client options

Returns:

  • (Boolean)


54
55
56
# File 'lib/toolhound-ruby/client.rb', line 54

def same_options?(opts)
  opts.hash == options.hash
end

#transactionObject



107
108
109
# File 'lib/toolhound-ruby/client.rb', line 107

def transaction
  @transaction ||= Toolhound::Transaction.new(self)
end

#transform_attribute_key(key) ⇒ Object

“SELECT MAX(dteStartDate) AS max_date, MIN(dteStartDate) AS min_date FROM tblRentalCharge WHERE intEntityID = 100044”



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/toolhound-ruby/client.rb', line 185

def transform_attribute_key(key)
  # renamed = self.class.renamed_attributes
  # if renamed.include? key
  #   renamed[key].to_sym
  # elsif key == self.class.primary_key
  #   :id
  # else
  # # "varTransferReceiptPrefix"
  #   word = key[3..key.length]
  #   word.underscore.to_sym
  # end
  word = key
  if DB_TYPE_REGEX =~ key
    word = key[3..key.length]
  end
  underscore(word).to_sym
end

#transform_attributes(attrs) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/toolhound-ruby/client.rb', line 161

def transform_attributes(attrs)
  hash = {}
  attrs.each do |k, v|
    key = transform_attribute_key(k)
    if hash.include? key
      hash[:"#{key}1"] = v
    else
      hash[key] = v
    end
  end
  hash
end

#vendorObject



138
139
140
# File 'lib/toolhound-ruby/client.rb', line 138

def vendor
  @vendor ||= Toolhound::Vendor.new(self)
end