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



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

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

#inventory_itemObject



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

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

#jobObject



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

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

#manufacturerObject



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

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)


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

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

#projectObject



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

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

#purchase_orderObject



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

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

#purchase_receiptObject



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

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

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



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

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



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

#rentalObject



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

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

#rental_chargeObject



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

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

#rental_itemObject



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

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

#reset_connectionObject



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

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



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

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”



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

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



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

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



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

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