Class: Infrataster::Resources::HttpResource

Inherits:
BaseResource show all
Defined in:
lib/infrataster/resources/http_resource.rb

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseResource

#context_class, #name

Constructor Details

#initialize(url_str, options = {}) ⇒ HttpResource

Returns a new instance of HttpResource.



11
12
13
14
15
16
17
# File 'lib/infrataster/resources/http_resource.rb', line 11

def initialize(url_str, options = {})
  @options = {params: {}, method: :get, headers: {}}.merge(options)
  @uri = URI.parse(url_str)
  unless %w!http https!.include?(@uri.scheme)
    raise Error, "The provided url, '#{@uri}', is not http or https."
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/infrataster/resources/http_resource.rb', line 9

def options
  @options
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/infrataster/resources/http_resource.rb', line 9

def uri
  @uri
end

Instance Method Details

#headersObject



36
37
38
# File 'lib/infrataster/resources/http_resource.rb', line 36

def headers
  @options[:headers]
end

#methodObject



27
28
29
30
31
32
33
34
# File 'lib/infrataster/resources/http_resource.rb', line 27

def method
  valid_methods = [:get, :head, :delete, :post, :put, :patch]
  unless valid_methods.include?(@options[:method])
    raise Error, "#{@options[:method]} is not supported HTTP method."
  end

  @options[:method]
end

#paramsObject



23
24
25
# File 'lib/infrataster/resources/http_resource.rb', line 23

def params
  @options[:params]
end

#to_sObject



19
20
21
# File 'lib/infrataster/resources/http_resource.rb', line 19

def to_s
  "http '#{@uri}' with #{@options}"
end