Class: Nearmiss::Client

Inherits:
Object
  • Object
show all
Includes:
Authentication, Account, Attachments, Bookmarks, Categories, Companies, Incidents, Notifications, Projects, RateLimit, Users, Configurable, Util
Defined in:
lib/nearmiss-ruby/client.rb,
lib/nearmiss-ruby/client/users.rb,
lib/nearmiss-ruby/client/account.rb,
lib/nearmiss-ruby/client/projects.rb,
lib/nearmiss-ruby/client/bookmarks.rb,
lib/nearmiss-ruby/client/companies.rb,
lib/nearmiss-ruby/client/incidents.rb,
lib/nearmiss-ruby/client/categories.rb,
lib/nearmiss-ruby/client/rate_limit.rb,
lib/nearmiss-ruby/client/attachments.rb,
lib/nearmiss-ruby/client/notifications.rb

Defined Under Namespace

Modules: Account, Attachments, Bookmarks, Categories, Companies, Incidents, Notifications, Projects, RateLimit, Users

Constant Summary collapse

CONVENIENCE_HEADERS =
Set.new([:accept, :content_type])

Instance Attribute Summary collapse

Attributes included from Configurable

#api_endpoint, #api_key, #auto_paginate, #connection_options, #email, #middleware, #password, #per_page, #proxy, #user_agent

Instance Method Summary collapse

Methods included from Attachments

#attachment, #attachments, #create_attachment, #incident_attachments, #update_attachment

Methods included from Companies

#companies, #company, #create_company, #delete_company, #edit_company

Methods included from Users

#delete_user, #edit_user, #update_email, #user, #users, #validate_credentials

Methods included from RateLimit

#rate_limit, #rate_limit!, #rate_limit_remaining, #rate_limit_remaining!

Methods included from Projects

#create_project, #delete_project, #edit_project, #project, #projects

Methods included from Notifications

#notifications

Methods included from Incidents

#create_incident, #create_incident_comment, #delete_incident_comment, #incident, #incident_comment, #incident_comments, #incidents, #project_incidents, #update_incident, #update_incident_comment

Methods included from Categories

#categories, #category, #create_category, #delete_category, #edit_category

Methods included from Bookmarks

#bookmark, #bookmarks, #delete_bookmark

Methods included from Account

#account

Methods included from Configurable

#configure, keys, #options, #reset!

Methods included from Authentication

#basic_authenticated?, #sign_in, #sign_out, #signed_in?, #token_authenticated?, #update_headers

Methods included from Util

#get_string, #url_encode, #uuid?

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



54
55
56
57
58
59
60
61
62
# File 'lib/nearmiss-ruby/client.rb', line 54

def initialize(options = {})

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

   if basic_authenticated?
end

Instance Attribute Details

#access_tokenObject

include Nearmiss::Client::Users include Nearmiss::Client::ProjectLibrary include Nearmiss::Client::Projects include Nearmiss::Client::Templates include Nearmiss::Client::Checklists include Nearmiss::Client::Tasks include Nearmiss::Client::Issues include Nearmiss::Client::Utils



49
50
51
# File 'lib/nearmiss-ruby/client.rb', line 49

def access_token
  @access_token
end

#client_idObject

include Nearmiss::Client::Users include Nearmiss::Client::ProjectLibrary include Nearmiss::Client::Projects include Nearmiss::Client::Templates include Nearmiss::Client::Checklists include Nearmiss::Client::Tasks include Nearmiss::Client::Issues include Nearmiss::Client::Utils



49
50
51
# File 'lib/nearmiss-ruby/client.rb', line 49

def client_id
  @client_id
end

#expiryObject

include Nearmiss::Client::Users include Nearmiss::Client::ProjectLibrary include Nearmiss::Client::Projects include Nearmiss::Client::Templates include Nearmiss::Client::Checklists include Nearmiss::Client::Tasks include Nearmiss::Client::Issues include Nearmiss::Client::Utils



49
50
51
# File 'lib/nearmiss-ruby/client.rb', line 49

def expiry
  @expiry
end

#meObject

include Nearmiss::Client::Users include Nearmiss::Client::ProjectLibrary include Nearmiss::Client::Projects include Nearmiss::Client::Templates include Nearmiss::Client::Checklists include Nearmiss::Client::Tasks include Nearmiss::Client::Issues include Nearmiss::Client::Utils



49
50
51
# File 'lib/nearmiss-ruby/client.rb', line 49

def me
  @me
end

#uidObject

include Nearmiss::Client::Users include Nearmiss::Client::ProjectLibrary include Nearmiss::Client::Projects include Nearmiss::Client::Templates include Nearmiss::Client::Checklists include Nearmiss::Client::Tasks include Nearmiss::Client::Issues include Nearmiss::Client::Utils



49
50
51
# File 'lib/nearmiss-ruby/client.rb', line 49

def uid
  @uid
end

Instance Method Details

#agentSawyer::Agent

Hypermedia agent for the BIM360-Field API

Returns:

  • (Sawyer::Agent)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nearmiss-ruby/client.rb', line 93

def agent
  @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
    # http.headers[:accept] = "image/jpg"
    http.headers[:content_type]         = "application/json"
    http.headers[:user_agent]           = user_agent
    http.headers[:accept]               = "application/json"
    http.headers[:api_key]              = api_key
    http.headers[:'x-client-platform']  = "api"

    if @access_token
      http.headers.merge!({
        :'access-token'   => @access_token,
        :client           => @client_id,
        :expiry           => @expiry,
        :'token-type'     => "Bearer",
        :uid              => @uid
      })
    end

    # if
    # if basic_authenticated?
      # http.basic_auth(@login, @password)
    # elsif token_authenticated?
    #   http.authorization 'token', @access_token
    # end
  end
end

#delete(url, options = {}) ⇒ Object



171
172
173
# File 'lib/nearmiss-ruby/client.rb', line 171

def delete(url, options = {})
  request :delete, url, options
end

#email=(value) ⇒ Object

Set username for authentication

Parameters:

  • value (String)

    Nearmiss-field username



125
126
127
128
# File 'lib/nearmiss-ruby/client.rb', line 125

def email=(value)
  reset_agent
  @email = value
end

#get(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP GET request

Parameters:

Returns:

  • (Sawyer::Resource)


155
156
157
# File 'lib/nearmiss-ruby/client.rb', line 155

def get(url, options = {})
  request :get, url, options
end

#inspectObject

:nodoc:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/nearmiss-ruby/client.rb', line 73

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

#last_responseSawyer::Response

Response for last HTTP request

Returns:

  • (Sawyer::Response)


178
179
180
# File 'lib/nearmiss-ruby/client.rb', line 178

def last_response
  @last_response if defined? @last_response
end

#nearmiss_warn(*message) ⇒ nil

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

Returns:

  • (nil)


222
223
224
225
226
# File 'lib/nearmiss-ruby/client.rb', line 222

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

#paginate(url, options = {}, &block) ⇒ Sawyer::Resource

Make one or more HTTP GET requests, optionally fetching the next page of results from URL in Link response header based on value in Nearmiss::Configurable#auto_paginate.

Parameters:

  • url (String)

    The path, relative to Nearmiss::Configurable#api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

  • block (Block)

    Block to perform the data concatination of the multiple requests. The block is called with two parameters, the first contains the contents of the requests so far and the second parameter contains the latest response.

Returns:

  • (Sawyer::Resource)


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/nearmiss-ruby/client.rb', line 194

def paginate(url, options = {}, &block)
  opts = parse_query_and_convenience_headers(options.dup)
  if @auto_paginate || @per_page
    opts[:query][:per_page] ||=  @per_page || (@auto_paginate ? 100 : nil)
  end

  data = request(:get, url, opts)

  if @auto_paginate
    while @last_response.rels[:next] #&& rate_limit.remaining > 0
      @last_response = @last_response.rels[:next].get
      if block_given?
        yield(data, @last_response)
      else
        data.concat(@last_response.data) if @last_response.data.is_a?(Array)
      end
    end

  end

  data
end

#password=(value) ⇒ Object

Set password for authentication

Parameters:

  • value (String)

    Nearmiss-field password



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

def password=(value)
  reset_agent
  @password = value
end

#patch(url, options = {}) ⇒ Object



167
168
169
# File 'lib/nearmiss-ruby/client.rb', line 167

def patch(url, options = {})
  request :patch, url, options
end

#post(url, options = {}) ⇒ Object



159
160
161
# File 'lib/nearmiss-ruby/client.rb', line 159

def post(url, options = {})
  request :post, url, options
end

#put(url, options = {}) ⇒ Object



163
164
165
# File 'lib/nearmiss-ruby/client.rb', line 163

def put(url, options = {})
  request :put, url, options
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)


68
69
70
# File 'lib/nearmiss-ruby/client.rb', line 68

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