class NationBuilder::Client

Attributes

client[RW]
client_id[RW]
client_secret[RW]
hostname[RW]
instrumentation[RW]
logger[RW]
password[RW]
token[RW]
username[RW]

Public Class Methods

new(args = {}) click to toggle source
# File lib/nation_builder/client.rb, line 16
def initialize(args = {})
  args.each do |key, value|
    self.send("#{key}=".intern, value)
  end
  if username.present? && password.present?
    self.client = setup_client_from_password
  elsif token.present?
    self.client = setup_client_from_token
  end
end

Public Instance Methods

delete(path, opts={}) click to toggle source
# File lib/nation_builder/client.rb, line 47
def delete(path, opts={})
  instrumented_request(:delete, path, opts)
end
get(path, opts={}) click to toggle source
# File lib/nation_builder/client.rb, line 35
def get(path, opts={})
  instrumented_request(:get, path, opts)
end
people() click to toggle source
# File lib/nation_builder/client.rb, line 27
def people
  NationBuilder::People.new(self)
end
post(path, opts={}) click to toggle source
# File lib/nation_builder/client.rb, line 39
def post(path, opts={})
  instrumented_request(:post, path, opts)
end
put(path, opts={}) click to toggle source
# File lib/nation_builder/client.rb, line 43
def put(path, opts={})
  instrumented_request(:put, path, opts)
end
tags() click to toggle source
# File lib/nation_builder/client.rb, line 31
def tags
  NationBuilder::Tags.new(self)
end

Private Instance Methods

base_uri() click to toggle source
# File lib/nation_builder/client.rb, line 102
def base_uri
  "https://#{hostname}/"
end
headers() click to toggle source
# File lib/nation_builder/client.rb, line 95
def headers
  {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json'
  }
end
instrumented_request(request_type, path, opts) click to toggle source
# File lib/nation_builder/client.rb, line 53
    def instrumented_request(request_type, path, opts)
      if self.instrumentation
        # Normalize path to remove IDs from it
        normalized_path = path.gsub(/\/\d+\//, '/_/')
        stats = { path: normalized_path, request_type: request_type }
        self.instrumentation.call(stats)
      end

      begin
        response = self.client.send(request_type, "#{base_uri}#{path}", opts.merge(headers: headers))
        if !logger.nil?
          log_message = <<-MESSAGE
NationBuilder rate limit headers for #{base_uri}:
X-RateLimit-Limit: #{response.headers["x-ratelimit-limit"]}
X-RateLimit-Remaining: #{response.headers['x-ratelimit-remaining']}
X-RateLimit-Reset: #{Time.at(response.headers["x-ratelimit-reset"].try(:to_i) || 0)}
          MESSAGE
          logger.info(log_message)
        end
        response
      rescue OAuth2::Error => e
        if (!e.code.nil? && e.code.downcase == 'rate_limited') || (e.message =~ /rate_limited/)
          raise RateLimitedError.new(e.response)
        end

        raise
      end
    end
setup_client() click to toggle source
# File lib/nation_builder/client.rb, line 90
def setup_client
  OAuth2::Client.new(client_id, client_secret, site: "https://#{hostname}", authorize_url: "https://#{hostname}/oauth/authorize", token_url: "https://#{hostname}/oauth/token" )
end
setup_client_from_password() click to toggle source
# File lib/nation_builder/client.rb, line 82
def setup_client_from_password
  setup_client.password.get_token(username, password)
end
setup_client_from_token() click to toggle source
# File lib/nation_builder/client.rb, line 86
def setup_client_from_token
  OAuth2::AccessToken.new(setup_client, token)
end