class Nearmiss::Client

Constants

CONVENIENCE_HEADERS

Attributes

access_token[RW]

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

client_id[RW]

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

expiry[RW]

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

me[RW]

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

uid[RW]

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

Public Class Methods

new(options = {}) click to toggle source
# 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

  sign_in if basic_authenticated?
end

Public Instance Methods

agent() click to toggle source

Hypermedia agent for the BIM360-Field API

@return [Sawyer::Agent]

# 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 = {}) click to toggle source
# File lib/nearmiss-ruby/client.rb, line 171
def delete(url, options = {})
  request :delete, url, options
end
email=(value) click to toggle source

Set username for authentication

@param value [String] Nearmiss-field username

# File lib/nearmiss-ruby/client.rb, line 125
def email=(value)
  reset_agent
  @email = value
end
get(url, options = {}) click to toggle source

Make a HTTP GET request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/nearmiss-ruby/client.rb, line 155
def get(url, options = {})
  request :get, url, options
end
last_response() click to toggle source

Response for last HTTP request

@return [Sawyer::Response]

# File lib/nearmiss-ruby/client.rb, line 178
def last_response
  @last_response if defined? @last_response
end
nearmiss_warn(*message) click to toggle source

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

@return [nil]

# File lib/nearmiss-ruby/client.rb, line 222
def nearmiss_warn(*message)
  unless ENV['NEARMISS_SILENT']
    warn message
  end
end
paginate(url, options = {}) { |data, last_response| ... } click to toggle source

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

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @param 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.

@return [Sawyer::Resource]

# 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) click to toggle source

Set password for authentication

@param value [String] Nearmiss-field password

# File lib/nearmiss-ruby/client.rb, line 133
def password=(value)
  reset_agent
  @password = value
end
patch(url, options = {}) click to toggle source
# File lib/nearmiss-ruby/client.rb, line 167
def patch(url, options = {})
  request :patch, url, options
end
post(url, options = {}) click to toggle source
# File lib/nearmiss-ruby/client.rb, line 159
def post(url, options = {})
  request :post, url, options
end
put(url, options = {}) click to toggle source
# File lib/nearmiss-ruby/client.rb, line 163
def put(url, options = {})
  request :put, url, options
end
same_options?(opts) click to toggle source

Compares client options to a Hash of requested options

@param opts [Hash] Options to compare with current client options @return [Boolean]

# File lib/nearmiss-ruby/client.rb, line 68
def same_options?(opts)
  opts.hash == options.hash
end

Private Instance Methods

boolean_from_response(method, path, options = {}) click to toggle source

Executes the request, checking if it was successful

@return [Boolean] True on success, false otherwise

# File lib/nearmiss-ruby/client.rb, line 287
def boolean_from_response(method, path, options = {})
  request(method, path, options)
  @last_response.status == 204
rescue Nearmiss::NotFound
  false
end
parse_query_and_convenience_headers(options) click to toggle source
# File lib/nearmiss-ruby/client.rb, line 295
def parse_query_and_convenience_headers(options)
  headers = options.fetch(:headers, {})
  CONVENIENCE_HEADERS.each do |h|
    if header = options.delete(h)
      headers[h] = header
    end
  end
  query = options.delete(:query)
  opts = {:query => options}
  opts[:query].merge!(query) if query && query.is_a?(Hash)
  opts[:headers] = headers unless headers.empty?

  opts
end
request(method, path, data, options = {}) click to toggle source

Make a HTTP Request

@param method [Symbol] Http method @param path [String] path relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/nearmiss-ruby/client.rb, line 243
def request(method, path, data, options = {})
  if data.is_a?(Hash)
    options[:query]   = data.delete(:query) || {}
    options[:headers] = data.delete(:headers) || {}
    if accept = data.delete(:accept)
      options[:headers][:accept] = accept
    end
  end

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


  @last_response = response = agent.call(method, URI::Parser.new.escape(path.to_s), data, options)

  update_headers(response.headers)

  response.data

end
reset_agent() click to toggle source
# File lib/nearmiss-ruby/client.rb, line 232
def reset_agent
  @agent = nil
end
sawyer_options() click to toggle source
# File lib/nearmiss-ruby/client.rb, line 272
def sawyer_options
  opts = {
    :links_parser => Sawyer::LinkParsers::Simple.new
  }
  conn_opts           = @connection_options
  conn_opts[:builder] = @middleware if @middleware
  conn_opts[:proxy]   = @proxy if @proxy
  opts[:faraday]      = Faraday.new(conn_opts)

  opts
end