Class: BugherdClient::Resources::V1::Base

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

Direct Known Subclasses

Comment, Project, Task, User

Constant Summary collapse

DEFAULT_HEADER_ATTRS =
{
  'Content-Type' => 'application/json',
  'Accept' => 'application/json'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, opts = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(conn, opts={})
  @connection, @options = conn, opts
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#api_methodsObject

Return a list of available methods in a Resource



17
18
19
# File 'lib/bugherd_client/resources/v1/base.rb', line 17

def api_methods
  self.class.instance_methods(false)
end

#parse_response(response, root_element = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bugherd_client/resources/v1/base.rb', line 39

def parse_response(response, root_element=nil)
  if root_element
    pr = JSON.parse(response)
    if pr.is_a?(Hash) and pr.has_key?(root_element)
      pr[root_element]
    else
      pr
    end
  else
    JSON.parse(response)
  end
end

#send_request(method = 'GET', path = '', params = {}, headers = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/bugherd_client/resources/v1/base.rb', line 26

def send_request(method='GET', path='', params={}, headers={})
  headers = DEFAULT_HEADER_ATTRS.merge(headers)
  params.merge!(headers)
  method_name = method.to_s.downcase
  self.connection[path].__send__(method_name, params)
end