Module: Quaderno::Behavior::Crud::ClassMethods

Includes:
Helpers::Authentication
Defined in:
lib/quaderno-ruby/behavior/crud.rb

Instance Method Summary collapse

Methods included from Helpers::Authentication

#get_authentication

Instance Method Details

#all(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/quaderno-ruby/behavior/crud.rb', line 37

def all(options = {})
  authentication = get_authentication(options.merge(api_model: api_model))
  filter = options.dup.delete_if { |k,v| %w(auth_token access_token api_url mode api_model).include? k.to_s }

  response = get("#{authentication[:url]}#{api_model.api_path}.json",
    query: filter,
    basic_auth: authentication[:basic_auth],
    headers: default_headers.merge(authentication[:headers])
  )

  handle_all_response(response, authentication, options)
end

#all_from_url(url, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/quaderno-ruby/behavior/crud.rb', line 26

def all_from_url(url, options = {})
  authentication = get_authentication(api_model: api_model)

  response = get(url,
    basic_auth: authentication[:basic_auth],
    headers: default_headers.merge(authentication[:headers])
  )

  handle_all_response(response, authentication, options)
end

#create(params = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/quaderno-ruby/behavior/crud.rb', line 70

def create(params = {})
  authentication = get_authentication(params.merge(api_model: api_model))
  params.dup.delete_if { |k,v| %w(auth_token access_token api_url mode api_model').include? k.to_s }

  response = post("#{authentication[:url]}#{api_model.api_path}.json",
    body: params.to_json,
    basic_auth: authentication[:basic_auth],
    headers: default_headers.merge(authentication[:headers]).merge('Content-Type' => 'application/json')
  )

  check_exception_for(response, { rate_limit: true, subdomain_or_token: true, required_fields: true })
  hash = response.parsed_response
  hash[:authentication_data] = authentication

  api_model.parse_nested(hash) if is_a_document?

  object = new hash
  object.rate_limit_info = response

  object
end

#delete(id, options = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/quaderno-ruby/behavior/crud.rb', line 114

def delete(id, options = {})
  authentication = get_authentication(options.merge(api_model: api_model))

  response = HTTParty.delete("#{authentication[:url]}#{ api_model.api_path }/#{ id }.json",
    basic_auth: authentication[:basic_auth],
    headers: default_headers.merge(authentication[:headers])
  )
  check_exception_for(response, { rate_limit: true, subdomain_or_token: true, id: true, has_documents: true })

  hash = { deleted: true, id: id }

  object = new hash
  object.rate_limit_info = response

  object
end

#find(id, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/quaderno-ruby/behavior/crud.rb', line 50

def find(id, options = {})
  authentication = get_authentication(options.merge(api_model: api_model))

  response = get("#{authentication[:url]}#{api_model.api_path}/#{id}.json",
    basic_auth: authentication[:basic_auth],
    headers: default_headers.merge(authentication[:headers])
  )

  check_exception_for(response, { rate_limit: true, subdomain_or_token: true, id: true })
  hash = response.parsed_response
  hash[:authentication_data] = authentication

  api_model.parse_nested(hash) if is_a_document?

  object = new hash
  object.rate_limit_info = response

  object
end

#parse_nested(element) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/quaderno-ruby/behavior/crud.rb', line 11

def parse_nested(element)
  if element.has_key?('payments')
    payments_collection = Array.new
    (element['payments'] || Array.new).each { |payment| payments_collection << Quaderno::Payment.new(payment) }
    element['payments'] = payments_collection
  end

  items_collection = Array.new
  element['items'].each { |item| items_collection << Quaderno::DocumentItem.new(item) }
  element['items'] = items_collection
  element['contact'] = Quaderno::Contact.new(element['contact'])

  element
end

#update(id, params = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/quaderno-ruby/behavior/crud.rb', line 92

def update(id, params = {})
  authentication = get_authentication(params.merge(api_model: api_model))
  params = params.dup.delete_if { |k,v| %w(auth_token access_token api_url mode api_model').include? k.to_s }

  response = put("#{authentication[:url]}#{api_model.api_path}/#{id}.json",
    body: params.to_json,
    basic_auth: authentication[:basic_auth],
    headers: default_headers.merge(authentication[:headers]).merge('Content-Type' => 'application/json')
  )

  check_exception_for(response, { rate_limit: true, required_fields: true, subdomain_or_token: true, id: true })
  hash = response.parsed_response
  hash[:authentication_data] = authentication

  api_model.parse_nested(hash) if is_a_document?

  object = new hash
  object.rate_limit_info = response

  object
end