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.



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

def attributes
  @attributes
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#new_resourceObject

Returns the value of attribute new_resource.



29
30
31
# File 'lib/posty_client/resources/base.rb', line 29

def new_resource
  @new_resource
end

Class Method Details

.base_uriObject



14
15
16
# File 'lib/posty_client/resources/base.rb', line 14

def self.base_uri
  URI.join(Settings.api_url, Settings.api_version)
end

.resource_nameObject



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

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

Instance Method Details

#base_uriObject



18
19
20
# File 'lib/posty_client/resources/base.rb', line 18

def base_uri
  self.class.base_uri
end

#createObject



91
92
93
94
95
96
# File 'lib/posty_client/resources/base.rb', line 91

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



98
99
100
101
102
103
# File 'lib/posty_client/resources/base.rb', line 98

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

#loadObject



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

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)


31
32
33
# File 'lib/posty_client/resources/base.rb', line 31

def new_resource?
  @new_resource
end

#request_with_error_handling(&block) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/posty_client/resources/base.rb', line 105

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



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

def resource_name
  self.class.resource_name
end

#resource_slugObject

Raises:

  • (NotImplementedError)


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

def resource_slug
  raise NotImplementedError        
end

#saveObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/posty_client/resources/base.rb', line 67

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)


139
140
141
# File 'lib/posty_client/resources/base.rb', line 139

def slug
  raise NotImplementedError
end

#updateObject



84
85
86
87
88
89
# File 'lib/posty_client/resources/base.rb', line 84

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