Class: Parliament::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/parliament/request.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: nil) ⇒ Request


5
6
7
8
# File 'lib/parliament/request.rb', line 5

def initialize(base_url: nil)
  @endpoint_parts = []
  @base_url = base_url || self.class.base_url || ENV['PARLIAMENT_BASE_URL']
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params, &block) ⇒ Object


10
11
12
13
14
15
16
17
18
# File 'lib/parliament/request.rb', line 10

def method_missing(method, *params, &block)
  # TODO: Fix this smell
  super if method == :base_url=

  @endpoint_parts << method.to_s
  @endpoint_parts << params
  @endpoint_parts = @endpoint_parts.flatten!
  self
end

Class Attribute Details

.base_urlObject

Returns the value of attribute base_url


74
75
76
# File 'lib/parliament/request.rb', line 74

def base_url
  @base_url
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url


3
4
5
# File 'lib/parliament/request.rb', line 3

def base_url
  @base_url
end

Instance Method Details

#assign_decorator(object) ⇒ Object


60
61
62
63
64
65
66
67
68
69
# File 'lib/parliament/request.rb', line 60

def assign_decorator(object)
  return object unless object.respond_to?(:type)

  object_type = Grom::Helper.get_id(object.type)

  return object unless Parliament::Decorators.constants.include?(object_type.to_sym)

  decorator_module = Object.const_get("Parliament::Decorators::#{object_type}")
  object.extend(decorator_module)
end

#build_parliament_response(response) ⇒ Object


35
36
37
38
39
40
# File 'lib/parliament/request.rb', line 35

def build_parliament_response(response)
  objects = Grom::Reader.new(response.body).objects
  objects.map { |object| assign_decorator(object) }

  Parliament::Response.new(objects)
end

#get(params: nil) ⇒ Object


24
25
26
27
28
29
30
31
32
33
# File 'lib/parliament/request.rb', line 24

def get(params: nil)
  endpoint_uri = URI.parse(api_endpoint)
  endpoint_uri.query = URI.encode_www_form(params.to_a) unless params.nil?

  net_response = Net::HTTP.get_response(URI(endpoint_uri))

  handle_errors(net_response)

  build_parliament_response(net_response)
end

#handle_errors(response) ⇒ Object


42
43
44
45
46
# File 'lib/parliament/request.rb', line 42

def handle_errors(response)
  handle_not_found_error(response)
  handle_server_error(response)
  handle_no_content_error(response)
end

#handle_no_content_error(response) ⇒ Object


56
57
58
# File 'lib/parliament/request.rb', line 56

def handle_no_content_error(response)
  raise Parliament::NoContentError if response.code == '204'
end

#handle_not_found_error(response) ⇒ Object

Raises:

  • (StandardError)

52
53
54
# File 'lib/parliament/request.rb', line 52

def handle_not_found_error(response)
  raise StandardError, 'This is a HTTPClientError' if response.is_a?(Net::HTTPClientError)
end

#handle_server_error(response) ⇒ Object

Raises:

  • (StandardError)

48
49
50
# File 'lib/parliament/request.rb', line 48

def handle_server_error(response)
  raise StandardError, 'This is a HTTPServerError' if response.is_a?(Net::HTTPServerError)
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean


20
21
22
# File 'lib/parliament/request.rb', line 20

def respond_to_missing?(method, include_private = false)
  (method != :base_url=) || super
end