class Nearmiss::Client
Constants
- CONVENIENCE_HEADERS
Attributes
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
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
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
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
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
# 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
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
# File lib/nearmiss-ruby/client.rb, line 171 def delete(url, options = {}) request :delete, url, options end
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
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
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
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
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
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
# File lib/nearmiss-ruby/client.rb, line 167 def patch(url, options = {}) request :patch, url, options end
# File lib/nearmiss-ruby/client.rb, line 159 def post(url, options = {}) request :post, url, options end
# File lib/nearmiss-ruby/client.rb, line 163 def put(url, options = {}) request :put, url, options end
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
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
# 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
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
# File lib/nearmiss-ruby/client.rb, line 232 def reset_agent @agent = nil end
# 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