Class: EasyPost::Services::Report

Inherits:
Service
  • Object
show all
Defined in:
lib/easypost/services/report.rb

Constant Summary collapse

MODEL_CLASS =
EasyPost::Models::Report

Instance Method Summary collapse

Methods inherited from Service

#initialize

Constructor Details

This class inherits a constructor from EasyPost::Services::Service

Instance Method Details

#all(params = {}) ⇒ Object

Retrieve all Report objects



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/easypost/services/report.rb', line 27

def all(params = {})
  unless params[:type]
    raise ArgumentError, "Missing 'type' parameter in params" # TODO: replace the error in the error-handling overhaul PR
  end

  type = params.delete(:type)
  filters = {
    key: 'reports',
    type: type,
  }
  url = "reports/#{type}"
  response = get_all_helper(url, MODEL_CLASS, params, filters)
  response.define_singleton_method(:type) { type }

  response
end

#create(params = {}) ⇒ Object

Create a Report



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/easypost/services/report.rb', line 7

def create(params = {})
  unless params[:type]
    raise ArgumentError, "Missing 'type' parameter in params" # TODO: replace the error in the error-handling overhaul PR
  end

  type = params.delete(:type)
  url = "reports/#{type}"
  response = @client.make_request(:post, url, params)

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end

#get_next_page(collection, page_size = nil) ⇒ Object

Get next page of Report objects



45
46
47
48
49
50
51
52
53
# File 'lib/easypost/services/report.rb', line 45

def get_next_page(collection, page_size = nil)
  raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection)

  params = { before_id: collection.reports.last.id }
  params[:page_size] = page_size unless page_size.nil?
  params.merge!(collection[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).delete(:key)

  all(params)
end

#retrieve(id) ⇒ Object

Retrieve a Report



20
21
22
23
24
# File 'lib/easypost/services/report.rb', line 20

def retrieve(id)
  response = @client.make_request(:get, "reports/#{id}")

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end