Class: OrangeLib::Rest

Inherits:
Object
  • Object
show all
Includes:
HTTParty, CommandLine, Variables
Defined in:
lib/orange_lib/rest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandLine

#execute_command

Methods included from Variables

#clear_variables, #interpret_string, #variable

Constructor Details

#initialize(opts = {}) ⇒ Rest

Returns a new instance of Rest.



15
16
# File 'lib/orange_lib/rest.rb', line 15

def initialize()
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



13
14
15
# File 'lib/orange_lib/rest.rb', line 13

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.

Raises:



12
13
14
# File 'lib/orange_lib/rest.rb', line 12

def response
  @response
end

Instance Method Details

#debugErr(opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


95
96
97
98
99
100
# File 'lib/orange_lib/rest.rb', line 95

def debugErr(opts={})
  raise ArgumentError, 'opts param must an object which responds to #to_hash' unless opts.respond_to?(:to_hash)
  self.class.default_options[:debug_output] = $stderr
  opts = ModuleInheritableAttributes.hash_deep_dup(self.class.default_options).merge(opts.to_hash)
  opts
end

#debugPass(opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
92
93
# File 'lib/orange_lib/rest.rb', line 88

def debugPass(opts={})
  raise ArgumentError, 'opts param must an object which responds to #to_hash' unless opts.respond_to?(:to_hash)
  self.class.default_options[:debug_output] = $stdout
  opts = ModuleInheritableAttributes.hash_deep_dup(self.class.default_options).merge(opts.to_hash)
  opts
end

#headers(h = {}) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
73
# File 'lib/orange_lib/rest.rb', line 69

def headers(h = {})
  raise ArgumentError, 'Headers must an object which responds to #to_hash' unless h.respond_to?(:to_hash)
  self.class.default_options[:headers] ||= {}
  self.class.default_options[:headers].merge!(h.to_hash)
end

#HostObject



80
81
82
# File 'lib/orange_lib/rest.rb', line 80

def Host
  self.class.base_uri
end

#mergeOpt(opts = {}) ⇒ Object



22
23
24
25
# File 'lib/orange_lib/rest.rb', line 22

def mergeOpt(opts={})
  self.class.default_options = self.class.default_options.merge(opts.to_hash)
  puts self.class.default_options
end

#perform_request(http_method, path, options = {}, &block) ⇒ Object

:nodoc:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/orange_lib/rest.rb', line 27

def perform_request(http_method, path, options={}, &block)  #:nodoc:
  method = eval("Net::HTTP::#{http_method.to_s.capitalize}")
  options = ModuleInheritableAttributes.hash_deep_dup(self.class.default_options).merge(options.to_hash)
  begin
    if (!options[:debug_request].nil? && options[:debug_request].to_s.downcase.eql?("true"))
      options = debugPass options
    end

    if (!options[:debug_request].nil? && options[:debug_request].to_s.downcase.eql?("false"))
      options = debugErr options
    end

    process_headers(options)
    process_cookies(options)
    puts options
    @response = HTTParty::Request.new(method, path, options).perform(&block)

  rescue => e
    puts "Rescued #{e.inspect}"
  end

  @response

end

#process_cookies(options) ⇒ Object

:nodoc:



63
64
65
66
67
# File 'lib/orange_lib/rest.rb', line 63

def process_cookies(options) #:nodoc:
  return unless options[:cookies] || self.class.default_cookies.any?
  options[:headers] ||= headers.dup
  options[:headers]['cookie'] = cookies.merge(options.delete(:cookies) || {}).to_cookie_string
end

#process_headers(options) ⇒ Object



57
58
59
60
61
# File 'lib/orange_lib/rest.rb', line 57

def process_headers(options)
  if options[:headers] && headers.any?
    options[:headers] = headers.merge(options[:headers])
  end
end

#setFollowRedirect(flag) ⇒ Object



84
85
86
# File 'lib/orange_lib/rest.rb', line 84

def setFollowRedirect(flag)
  self.class.follow_redirects flag
end

#setHost(host) ⇒ Object

Raises:



75
76
77
78
# File 'lib/orange_lib/rest.rb', line 75

def setHost(host)
  raise OrangeLib::Error.new('host param is null') if host.nil?
  self.class.base_uri host
end