Class: Asimov::ApiV1::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
HTTParty
Defined in:
lib/asimov/api_v1/base.rb

Overview

Base class for API interface implementations. Currently manages the network logic for the interface.

Instance Method Summary collapse

Constructor Details

#initialize(client: nil) ⇒ Base

Returns a new instance of Base.



18
19
20
# File 'lib/asimov/api_v1/base.rb', line 18

def initialize(client: nil)
  @client = client
end

Instance Method Details

#rest_create_w_json_params(resource:, parameters:) ⇒ Object

Executes a REST create with JSON-encoded parameters for the specified resource.

to create the resource

Parameters:

  • the (String)

    resource to be created.

  • parameters (Hash)

    the parameters to include with the request



75
76
77
78
79
80
81
82
83
# File 'lib/asimov/api_v1/base.rb', line 75

def rest_create_w_json_params(resource:, parameters:)
  wrap_response_with_error_handling do
    self.class.post(
      absolute_path("/#{Array(resource).join('/')}"),
      { headers: headers,
        body: parameters&.to_json }.merge!(request_options)
    )
  end
end

#rest_create_w_multipart_params(resource:, parameters: nil) ⇒ Object

Executes a REST create with multipart-encoded parameters for the specified resource.

to create the resource

Parameters:

  • the (String)

    resource to be created.

  • parameters (Hash) (defaults to: nil)

    the optional parameters to include with the request



93
94
95
96
97
98
99
100
101
# File 'lib/asimov/api_v1/base.rb', line 93

def rest_create_w_multipart_params(resource:, parameters: nil)
  wrap_response_with_error_handling do
    self.class.post(
      absolute_path("/#{Array(resource).join('/')}"),
      { headers: headers("multipart/form-data"),
        body: parameters }.merge!(request_options)
    )
  end
end

#rest_delete(resource:, id:) ⇒ Object

Executes a REST delete on the specified resource.

Parameters:

  • resource (String)

    the pluralized resource name

  • id (String)

    the id of the resource to delete



43
44
45
46
47
48
49
50
# File 'lib/asimov/api_v1/base.rb', line 43

def rest_delete(resource:, id:)
  wrap_response_with_error_handling do
    self.class.delete(
      absolute_path("/#{resource}/#{CGI.escape(id)}"),
      { headers: headers }.merge!(request_options)
    )
  end
end

#rest_get(resource:, id:) ⇒ Object

Executes a REST get on the specified resource.

Parameters:

  • resource (String)

    the pluralized resource name

  • id (String)

    the id of the resource get



58
59
60
61
62
63
64
65
# File 'lib/asimov/api_v1/base.rb', line 58

def rest_get(resource:, id:)
  wrap_response_with_error_handling do
    self.class.get(
      absolute_path("/#{resource}/#{CGI.escape(id)}"),
      { headers: headers }.merge!(request_options)
    )
  end
end

#rest_get_streamed_download(resource:, writer:) ⇒ Object

Executes an REST get on the specified path, streaming the resulting body to the writer in case of success.

Parameters:

  • resource (Array)

    the resource path elements as an array

  • writer (Writer)

    an object, typically a File, that responds to a ‘write` method



110
111
112
113
114
115
116
117
118
119
# File 'lib/asimov/api_v1/base.rb', line 110

def rest_get_streamed_download(resource:, writer:)
  self.class.get(absolute_path("/#{Array(resource).join('/')}"),
                 { headers: headers,
                   stream_body: true }.merge!(request_options)) do |fragment|
    fragment.code == 200 ? writer.write(fragment) : check_for_api_error(fragment)
  end
rescue StandardError => e
  # Any error raised by the HTTP call is a network error
  NetworkErrorTranslator.translate(e)
end

#rest_index(resource:) ⇒ Object

Executes a REST index for the specified resource

Parameters:

  • resource (String)

    the pluralized resource name



28
29
30
31
32
33
34
35
# File 'lib/asimov/api_v1/base.rb', line 28

def rest_index(resource:)
  wrap_response_with_error_handling do
    self.class.get(
      absolute_path("/#{Array(resource).join('/')}"),
      { headers: headers }.merge!(request_options)
    )
  end
end