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

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

#connection ⇒ Object

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

#dataserver ⇒ Object

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

#password ⇒ Object

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

#port ⇒ Object

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

#username ⇒ Object

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

#inspect ⇒ Object

: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

#inventory ⇒ Object



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

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

#inventory_item ⇒ Object



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

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

#manufacturer ⇒ Object



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

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)


197
198
199
200
201
# File 'lib/toolhound-ruby/client.rb', line 197

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

#project ⇒ Object



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

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

#purchase_order ⇒ Object



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

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

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



139
140
141
142
143
144
145
146
147
148
# File 'lib/toolhound-ruby/client.rb', line 139

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



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

#rental ⇒ Object



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

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

#rental_charge ⇒ Object



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

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

#rental_item ⇒ Object



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

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

#reset_connection ⇒ Object



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

#transform_attribute_key(key) ⇒ Object

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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/toolhound-ruby/client.rb', line 174

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



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

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

#vendor ⇒ Object



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

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