Class: BugherdClient::Resources::V2::Base

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

Direct Known Subclasses

Attachment, Comment, Organization, Project, Task, User, Webhook

Constant Summary collapse

DEFAULT_HEADER_ATTRS =
{
  :content_type => :json,
  :accept => :json
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



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

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

Instance Attribute Details

#connectionObject

store a reference to the HTTP connection



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

def connection
  @connection
end

#optionsObject

Returns the value of attribute options.



24
25
26
# File 'lib/bugherd_client/resources/v2/base.rb', line 24

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/v2/base.rb', line 17

def api_methods
  self.class.instance_methods(false)
end

#converter(body) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/bugherd_client/resources/v2/base.rb', line 45

def converter(body)
  case body
  when Hash
    ::Hashie::Mash.new(body)
  when Array
    body.map { |item| item.is_a?(Hash) ? converter(item) : item }
  else
    body
  end
end

#parse_response(response, root_element = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/bugherd_client/resources/v2/base.rb', line 56

def parse_response(response, root_element=nil)
  parsed = if root_element
    p = JSON.parse(response)
    p.key?(root_element.to_s) ? p[root_element.to_s] : p
  else
    JSON.parse(response)
  end
  converter(parsed)
end

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



30
31
32
33
34
35
36
37
# File 'lib/bugherd_client/resources/v2/base.rb', line 30

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)
rescue RestClient::Exception => e
  raise(BugherdClient::Errors::HttpRequestError.new(e.message, e.http_code))
end