Class: Dox::Api::ResourceHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/dox/api/resource_handler.rb

Defined Under Namespace

Classes: HTTPError, ResourceURIError

Constant Summary collapse

USER_AGENT =
"DoximityAPI/#{Dox::Api::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} (#{RUBY_ENGINE}; #{RUBY_PLATFORM})"
HEADERS =
{
  "Content-Type"  => "application/json; encoding=utf-8",
  "Accept"        => "application/json; encoding=utf-8; q=1",
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, token) ⇒ ResourceHandler


17
18
19
20
# File 'lib/dox/api/resource_handler.rb', line 17

def initialize(base_url, token)
  @base_url = base_url
  @token    = token
end

Class Method Details

.http_clientObject


12
13
14
15
# File 'lib/dox/api/resource_handler.rb', line 12

def self.http_client
  return @http_client if defined?(@http_client)
  @http_client = HTTPClient.new(agent_name: USER_AGENT)
end

Instance Method Details

#delete(model) ⇒ Object


31
32
33
34
35
# File 'lib/dox/api/resource_handler.rb', line 31

def delete(model)
  with_resource_location(model) do |url, specific_uri|
    request(:delete, url) if specific_uri
  end
end

#get(model) ⇒ Object


37
38
39
40
41
# File 'lib/dox/api/resource_handler.rb', line 37

def get(model)
  with_resource_location(model) do |url, specific_uri|
    request(:get, url, model: model) if specific_uri
  end
end

#save(model) ⇒ Object


22
23
24
25
26
27
28
29
# File 'lib/dox/api/resource_handler.rb', line 22

def save(model)
  return false unless model.valid?

  with_resource_location(model) do |url, specific_uri|
    method = specific_uri ? :put : :post
    request(method, url, model: model)
  end
end