Class: AccessGrid::Client
- Inherits:
-
Object
- Object
- AccessGrid::Client
- Defined in:
- lib/accessgrid.rb
Instance Attribute Summary collapse
-
#account_id ⇒ Object
readonly
Returns the value of attribute account_id.
-
#api_host ⇒ Object
readonly
Returns the value of attribute api_host.
-
#api_secret ⇒ Object
readonly
Returns the value of attribute api_secret.
Instance Method Summary collapse
- #access_cards ⇒ Object
- #console ⇒ Object
-
#initialize(account_id, api_secret, api_host = 'https://api.accessgrid.com') ⇒ Client
constructor
A new instance of Client.
- #make_request(method, path, body = nil, params = nil) ⇒ Object
Constructor Details
#initialize(account_id, api_secret, api_host = 'https://api.accessgrid.com') ⇒ Client
Returns a new instance of Client.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/accessgrid.rb', line 24 def initialize(account_id, api_secret, api_host = 'https://api.accessgrid.com') if account_id.nil? || account_id.empty? raise ArgumentError, "Account ID is required" end if api_secret.nil? || api_secret.empty? raise ArgumentError, "API Secret is required" end @account_id = account_id @api_secret = api_secret @api_host = api_host.chomp('/') end |
Instance Attribute Details
#account_id ⇒ Object (readonly)
Returns the value of attribute account_id.
22 23 24 |
# File 'lib/accessgrid.rb', line 22 def account_id @account_id end |
#api_host ⇒ Object (readonly)
Returns the value of attribute api_host.
22 23 24 |
# File 'lib/accessgrid.rb', line 22 def api_host @api_host end |
#api_secret ⇒ Object (readonly)
Returns the value of attribute api_secret.
22 23 24 |
# File 'lib/accessgrid.rb', line 22 def api_secret @api_secret end |
Instance Method Details
#access_cards ⇒ Object
38 39 40 |
# File 'lib/accessgrid.rb', line 38 def access_cards @access_cards ||= AccessCards.new(self) end |
#console ⇒ Object
42 43 44 |
# File 'lib/accessgrid.rb', line 42 def console @console ||= Console.new(self) end |
#make_request(method, path, body = nil, params = nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/accessgrid.rb', line 46 def make_request(method, path, body = nil, params = nil) uri = URI.parse("#{api_host}#{path}") # Add query parameters if present if params && !params.empty? uri.query = URI.encode_www_form(params) end http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' # Create request object based on method request = case method when :get Net::HTTP::Get.new(uri.request_uri) when :post Net::HTTP::Post.new(uri.request_uri) when :put Net::HTTP::Put.new(uri.request_uri) when :patch Net::HTTP::Patch.new(uri.request_uri) else raise ArgumentError, "Unsupported HTTP method: #{method}" end # Set headers request['Content-Type'] = 'application/json' request['X-ACCT-ID'] = account_id request['User-Agent'] = "accessgrid.rb @ v#{AccessGrid::VERSION}" # Extract resource ID from the path if needed for signature resource_id = nil if method == :get || (method == :post && (body.nil? || body.empty?)) parts = path.strip.split('/') if parts.length >= 2 if ['suspend', 'resume', 'unlink', 'delete'].include?(parts.last) resource_id = parts[-2] else resource_id = parts.last end end end # Handle signature generation if method == :get || (method == :post && (body.nil? || body.empty?)) payload = resource_id ? { id: resource_id }.to_json : '{}' # Include sig_payload in query params if needed if resource_id if params.nil? params = {} end params[:sig_payload] = { id: resource_id }.to_json # Update the URI with the new params uri.query = URI.encode_www_form(params) request = case method when :get Net::HTTP::Get.new(uri.request_uri) when :post Net::HTTP::Post.new(uri.request_uri) end # Reset headers after creating new request request['Content-Type'] = 'application/json' request['X-ACCT-ID'] = account_id request['User-Agent'] = "accessgrid.rb @ v#{AccessGrid::VERSION}" end else payload = body ? body.to_json : "" end # Generate signature request['X-PAYLOAD-SIG'] = generate_signature(payload) # Add the body to the request if body && method != :get request.body = body.to_json end # Make request response = http.request(request) # Parse response handle_response(response) end |