Class: Youtrack::Resources::Base

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

Direct Known Subclasses

Issues, Projects, Users

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Base

Returns a new instance of Base.



6
7
8
# File 'lib/youtrack/resources/base.rb', line 6

def initialize(client:)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



4
5
6
# File 'lib/youtrack/resources/base.rb', line 4

def client
  @client
end

Class Method Details

.descendantsObject



49
50
51
# File 'lib/youtrack/resources/base.rb', line 49

def self.descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

Instance Method Details

#delete_resource(path) ⇒ Object



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

def delete_resource(path)
  client.delete(path)
  true
end

#deserialize_response(response) ⇒ Object



31
32
33
34
35
# File 'lib/youtrack/resources/base.rb', line 31

def deserialize_response(response)
  $stdout.puts response.body if ENV["DEBUG"]
  json = JSON.parse(response.body)
  model.from_json(json)
end

#get_resource_with_fields(fields, path, options = {}) ⇒ Object



10
11
12
13
14
# File 'lib/youtrack/resources/base.rb', line 10

def get_resource_with_fields(fields, path, options = {})
  query_params = query_params_for_fields(fields).merge(options.delete(:params) || {})
  response = client.get(path, { params: query_params })
  deserialize_response(response)
end

#post_resource_with_fields(fields, path, data) ⇒ Object



16
17
18
19
# File 'lib/youtrack/resources/base.rb', line 16

def post_resource_with_fields(fields, path, data)
  response = client.post(path, prepare_payload(data), { params: query_params_for_fields(fields) })
  deserialize_response(response)
end

#prepare_payload(data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/youtrack/resources/base.rb', line 37

def prepare_payload(data)
  if data.respond_to?(:deep_transform_keys)
    data = data.deep_transform_keys { |k| k.to_s.camelize(:lower) }
  end
  if data.respond_to?(:as_payload)
    data = data.as_payload
  elsif data.respond_to?(:to_json)
    data = data.to_json
  end
  data
end

#query_params_for_fields(fields) ⇒ Object



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

def query_params_for_fields(fields)
  return {} unless fields.present?
  { fields: fields.map(&:to_query).join(",") }
end