Class: Monokera::SDK::ActiveModel

Inherits:
Model
  • Object
show all
Defined in:
lib/monokera/sdk/active_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#add_error, #as_json, #except_attributes!, model_name, #to_json, #valid?

Methods included from ActiveModelSerializer

#read_attribute_for_serialization

Class Method Details

.all(query_options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/monokera/sdk/active_model.rb', line 68

def all(query_options = {})
  response = client.get(collection_endpoint(query_options), query_options)

  if response.success?
    build_data(response.body)
  else
    raven_context(response)
    raise ClientError, response.body[:errors]
  end
end

.build(body) ⇒ Object



120
121
122
# File 'lib/monokera/sdk/active_model.rb', line 120

def build(body)
  new(body)
end

.build_data(body) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/monokera/sdk/active_model.rb', line 124

def build_data(body)
  if const_defined?(:ADAPTER) && self::ADAPTER == :json_api
    body[:data].map { |resource_body| new(serialize_keys_json_api(resource_body[:attributes])) }
  else
    body.map { |resource_body| new(resource_body) }
  end
end

.collection_endpoint(options = {}) ⇒ Object



111
112
113
# File 'lib/monokera/sdk/active_model.rb', line 111

def collection_endpoint(options = {})
  self::BASE_ENDPOINT % options.to_hash.symbolize_keys
end

.create(attrs, headers = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/monokera/sdk/active_model.rb', line 90

def create(attrs, headers = {})
  response = client.post(collection_endpoint(attrs), attrs, headers)

  if response.success?
    build(response.body)
  else
    new(response.body[:errors])
  end
end

.create!(attrs, headers = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/monokera/sdk/active_model.rb', line 100

def create!(attrs, headers = {})
  resource = create(attrs, headers)

  if resource.persisted?
    resource
  else
    raven_context(resource)
    raise ClientError, resource.errors
  end
end

.find(id, params = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/monokera/sdk/active_model.rb', line 79

def find(id, params = {})
  response = client.get(resource_endpoint(id, params), params)

  if response.success?
    build(response.body)
  else
    raven_context(response)
    raise ResourceNotFound, response.body[:errors]
  end
end

.raven_context(response) ⇒ Object



136
137
138
# File 'lib/monokera/sdk/active_model.rb', line 136

def raven_context(response)
  Raven.extra_context(sdk_response: response.to_hash) if defined?(Raven)
end

.resource_endpoint(id, options = {}) ⇒ Object



115
116
117
118
# File 'lib/monokera/sdk/active_model.rb', line 115

def resource_endpoint(id, options = {})
  endpoint = options.present? ? collection_endpoint(options) : self::BASE_ENDPOINT
  [endpoint, id].join('/')
end

.serialize_keys_json_api(data) ⇒ Object



132
133
134
# File 'lib/monokera/sdk/active_model.rb', line 132

def serialize_keys_json_api(data)
  data.deep_transform_keys! { |key| key.to_s.underscore }
end

Instance Method Details

#destroyObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/monokera/sdk/active_model.rb', line 36

def destroy
  response = self.class.client.destroy(self.class.resource_endpoint(id))

  if response.success?
    true
  else
    self.class.raven_context(response)
    false
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/monokera/sdk/active_model.rb', line 47

def new_record?
  id.blank?
end

#persisted?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/monokera/sdk/active_model.rb', line 51

def persisted?
  !new_record?
end

#saveObject



30
31
32
33
34
# File 'lib/monokera/sdk/active_model.rb', line 30

def save
  create_or_update
rescue ClientError
  false
end

#save!Object



22
23
24
25
26
27
28
# File 'lib/monokera/sdk/active_model.rb', line 22

def save!
  if id
    update!(as_json)
  else
    self.class.create!(as_json)
  end
end

#update(attrs = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/monokera/sdk/active_model.rb', line 6

def update(attrs = {})
  url_params = { country_code: attributes[:country_code] }.merge(attrs)
  response = self.class.client.put(self.class.resource_endpoint(id, url_params), attrs)
  if response.success?
    self.attributes = response.body
    true
  else
    self.class.raven_context(response)
    false
  end
end

#update!(attrs) ⇒ Object



18
19
20
# File 'lib/monokera/sdk/active_model.rb', line 18

def update!(attrs)
  update(attrs) || raise(ClientError, errors)
end