Class: PostyClient::Resources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/posty_client/resources/base.rb

Direct Known Subclasses

ApiKey, Domain, DomainAlias, Summary, Transport, User, UserAlias

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



22
23
24
# File 'lib/posty_client/resources/base.rb', line 22

def attributes
  @attributes
end

#errorsObject (readonly)

Returns the value of attribute errors.



23
24
25
# File 'lib/posty_client/resources/base.rb', line 23

def errors
  @errors
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/posty_client/resources/base.rb', line 21

def name
  @name
end

#new_resourceObject

Returns the value of attribute new_resource.



25
26
27
# File 'lib/posty_client/resources/base.rb', line 25

def new_resource
  @new_resource
end

Class Method Details

.resource_nameObject



123
124
125
# File 'lib/posty_client/resources/base.rb', line 123

def self.resource_name
  self.name.demodulize.tableize
end

Instance Method Details

#createObject



87
88
89
90
91
92
# File 'lib/posty_client/resources/base.rb', line 87

def create
  logger.debug("create #{self.class.name} : #{name}")
  request_with_error_handling do
    RestClient.post(resource_slug, attributes, :content_type => :json, :accept => :json)
  end
end

#deleteObject



94
95
96
97
98
99
# File 'lib/posty_client/resources/base.rb', line 94

def delete
  logger.debug("delete #{self.class.name} : #{name}")
  request_with_error_handling do
    RestClient.delete(slug)
  end
end

#loadObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/posty_client/resources/base.rb', line 37

def load
  @new_resource = true
  @attributes = {}

  response = begin
    RestClient.get(slug)
  rescue RestClient::Exception => e
    e.response
  end

  if response.code == 404
    logger.debug("#{self.class.name} :: load non existing object (#{response.code}) '#{response}'")
    return
  elsif response.code != 200
    logger.error("#{self.class.name} :: load failed with (#{response.code}) '#{response}'")
    return
  end

  @new_resource = false
  self.attributes = JSON.parse(response).flatten.last

  @name = attributes[primary_key]

  self
end

#new_resource?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/posty_client/resources/base.rb', line 27

def new_resource?
  @new_resource
end

#request_with_error_handling(&block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/posty_client/resources/base.rb', line 101

def request_with_error_handling(&block)
  response = begin
    block.call
  rescue RestClient::Exception => e
    e.response
  end

  case response.code
  when 200..299
    true
  else
    @errors = begin
      JSON.parse(response)['error']
    rescue => e
      logger.error(e)
      {'base' => "Unrecoverable error: #{response.code} (#{response})"}
    end

    false
  end
end

#resource_nameObject



127
128
129
# File 'lib/posty_client/resources/base.rb', line 127

def resource_name
  self.class.resource_name
end

#resource_slugObject

Raises:

  • (NotImplementedError)


131
132
133
# File 'lib/posty_client/resources/base.rb', line 131

def resource_slug
  raise NotImplementedError        
end

#saveObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/posty_client/resources/base.rb', line 63

def save
  if new_resource?
    self.attributes[primary_key] = @name
    if create
      @new_resource = false
      return true
    end
  else
    if update
      load
      return true
    end
  end

  return false
end

#slugObject

Raises:

  • (NotImplementedError)


135
136
137
# File 'lib/posty_client/resources/base.rb', line 135

def slug
  raise NotImplementedError
end

#updateObject



80
81
82
83
84
85
# File 'lib/posty_client/resources/base.rb', line 80

def update
  logger.debug("update #{self.class.name} : #{name}")
  request_with_error_handling do
    RestClient.put(slug, attributes)
  end
end