Class: Resto::Request::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Header, Option, Uri
Defined in:
lib/resto/request/base.rb

Instance Attribute Summary

Attributes included from Uri

#scheme

Instance Method Summary collapse

Methods included from Option

#close_on_empty_response, #open_timeout, #options, #read_timeout, #set_debug_output, #timeout

Methods included from Uri

#composed_path, #hash_to_params, #normalize_uri, #parse_url, #read_host, #read_port

Methods included from Header

#accept, #basic_auth, #composed_headers, #content_type, #format, #formatter, #headers

Constructor Details

#initialize(request = Resto::Request::Factory) ⇒ Base

Returns a new instance of Base.



22
23
24
25
# File 'lib/resto/request/base.rb', line 22

def initialize(request=Resto::Request::Factory)
  @request_klass = request
  @request = @request_klass.new(self)
end

Instance Method Details

#append_path(append_path) ⇒ Object



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

def append_path(append_path)
  tap { @append_path = append_path }
end

#body(body) ⇒ Object



73
74
75
# File 'lib/resto/request/base.rb', line 73

def body(body)
  tap { @body = body }
end

#construct_path(options) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/resto/request/base.rb', line 27

def construct_path(options)
  new_path = @path.clone

  @path_substitute_keys.each do |substitute|
    new_path.gsub!(/:#{substitute}/, options[substitute].to_s)
  end

  Copy.request_base(self).path(new_path)
end

#current_formatterObject



89
90
91
# File 'lib/resto/request/base.rb', line 89

def current_formatter
  @formatter ||= Resto::Format.get(@symbol || :default)
end

#host(host) ⇒ Object



41
42
43
# File 'lib/resto/request/base.rb', line 41

def host(host)
  tap { @host = host }
end

#params(hash) ⇒ Object



64
65
66
67
# File 'lib/resto/request/base.rb', line 64

def params(hash)
  @params ||= {}
  tap { @params.update(hash) }
end

#params!(hash) ⇒ Object



69
70
71
# File 'lib/resto/request/base.rb', line 69

def params!(hash)
  tap { @params = hash }
end

#path(path) ⇒ Object



49
50
51
52
53
54
# File 'lib/resto/request/base.rb', line 49

def path(path)
  @path = path
  keys = path.scan(/\/(:\w+)/).flatten
  @path_substitute_keys = keys.map {|key| key.gsub(/:/, "").to_sym }
  self
end

#port(port) ⇒ Object



45
46
47
# File 'lib/resto/request/base.rb', line 45

def port(port)
  tap { @port = port }
end

#query(query) ⇒ Object



60
61
62
# File 'lib/resto/request/base.rb', line 60

def query(query)
  tap { @query = query }
end

#read_bodyObject



82
83
84
85
86
87
# File 'lib/resto/request/base.rb', line 82

def read_body
  if @body
    body = @translator ? @translator.call(@body) : @body
    current_formatter.encode(body)
  end
end

#translator(translator) ⇒ Object



77
78
79
80
# File 'lib/resto/request/base.rb', line 77

def translator(translator)
  @translator = Resto::Translator::RequestFactory.create(translator)
  self
end

#url(url) ⇒ Object



37
38
39
# File 'lib/resto/request/base.rb', line 37

def url(url)
  tap { parse_url(url) }
end