Class: EasyPost::Resource

Inherits:
EasyPostObject show all
Extended by:
Enumerable
Defined in:
lib/easypost/resource.rb

Overview

The Resource object is extended by each EasyPost object.

Instance Attribute Summary

Attributes inherited from EasyPostObject

#api_key, #name, #parent, #unsaved_values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EasyPostObject

#[], #[]=, #as_json, construct_from, #deconstruct_keys, #each, #id, #id=, #initialize, #inspect, #keys, #refresh_from, #to_hash, #to_json, #to_s, #values

Constructor Details

This class inherits a constructor from EasyPost::EasyPostObject

Class Method Details

.all(filters = {}, api_key = nil) ⇒ Object

Retrieve a list of EasyPost objects.


46
47
48
49
# File 'lib/easypost/resource.rb', line 46

def self.all(filters = {}, api_key = nil)
  response = EasyPost.make_request(:get, url, api_key, filters)
  EasyPost::Util.convert_to_easypost_object(response, api_key)
end

.class_nameObject

The class name of an EasyPost object.


8
9
10
11
12
# File 'lib/easypost/resource.rb', line 8

def self.class_name
  camel = name.split('::')[-1]
  snake = camel[0..0] + camel[1..-1].gsub(/([A-Z])/, '_\1')
  snake.downcase
end

.create(params = {}, api_key = nil) ⇒ Object

Create an EasyPost object.


71
72
73
74
75
76
# File 'lib/easypost/resource.rb', line 71

def self.create(params = {}, api_key = nil)
  wrapped_params = {}
  wrapped_params[class_name.to_sym] = params
  response = EasyPost.make_request(:post, url, api_key, wrapped_params)
  EasyPost::Util.convert_to_easypost_object(response, api_key)
end

.each(filters = {}, api_key = EasyPost.api_key, &block) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/easypost/resource.rb', line 51

def self.each(filters = {}, api_key = EasyPost.api_key, &block)
  return to_enum(:each, filters, api_key) unless block_given?

  loop do
    page, has_more = all(filters, api_key).values
    last = page.each(&block).last
    break if page.empty? || !has_more

    filters[:before_id] = last.id
  end
end

.retrieve(id, api_key = nil) ⇒ Object

Retrieve an EasyPost object.


64
65
66
67
68
# File 'lib/easypost/resource.rb', line 64

def self.retrieve(id, api_key = nil)
  instance = new(id, api_key)
  instance.refresh
  instance
end

.urlObject

The instance url of the Resource.


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/easypost/resource.rb', line 15

def self.url
  if class_name == 'resource'
    raise NotImplementedError.new(
      'Resource is an abstract class. You should perform actions on its subclasses (Address, Shipment, etc.)',
    )
  end

  if class_name[-1..-1] == 's' || class_name[-1..-1] == 'h'
    "/v2/#{CGI.escape(class_name.downcase)}es"
  else
    "/v2/#{CGI.escape(class_name.downcase)}s"
  end
end

Instance Method Details

#deleteObject

Delete an EasyPost object.


79
80
81
82
83
# File 'lib/easypost/resource.rb', line 79

def delete
  response = EasyPost.make_request(:delete, url, @api_key)
  refresh_from(response, api_key)
  self
end

#refreshObject

Refresh an object from the API response.


39
40
41
42
43
# File 'lib/easypost/resource.rb', line 39

def refresh
  response = EasyPost.make_request(:get, url, @api_key, @retrieve_options)
  refresh_from(response, api_key)
  self
end

#saveObject

Save (update) and EasyPost object.


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/easypost/resource.rb', line 86

def save
  if @unsaved_values.length.positive?
    values = {}
    @unsaved_values.each { |k| values[k] = @values[k] }

    @unsaved_values.each do |key| # rubocop:disable Style/CombinableLoops
      value = values[key]
      if value.is_a?(EasyPost::EasyPostObject)
        values[key] = value.flatten_unsaved
      end
    end

    wrapped_params = { self.class.class_name => values }

    response = EasyPost.make_request(:put, url, @api_key, wrapped_params)
    refresh_from(response, api_key)
  end
  self
end

#urlObject

The url of the Resource.


30
31
32
33
34
35
36
# File 'lib/easypost/resource.rb', line 30

def url
  unless id
    raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}")
  end

  "#{self.class.url}/#{CGI.escape(id)}"
end