Class: Orange::Rest

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/hung_secondGem/orange.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Rest

Returns a new instance of Rest.



11
12
# File 'lib/hung_secondGem/orange.rb', line 11

def initialize()
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



9
10
11
# File 'lib/hung_secondGem/orange.rb', line 9

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.

Raises:



8
9
10
# File 'lib/hung_secondGem/orange.rb', line 8

def response
  @response
end

Instance Method Details

#debugErr(opts = {}) ⇒ Object



89
90
91
92
93
# File 'lib/hung_secondGem/orange.rb', line 89

def debugErr(opts={})
  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



83
84
85
86
87
# File 'lib/hung_secondGem/orange.rb', line 83

def debugPass(opts={})
  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)


65
66
67
68
69
# File 'lib/hung_secondGem/orange.rb', line 65

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



75
76
77
# File 'lib/hung_secondGem/orange.rb', line 75

def Host
  self.class.base_uri
end

#mergeOpt(opts = {}) ⇒ Object



18
19
20
21
# File 'lib/hung_secondGem/orange.rb', line 18

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:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hung_secondGem/orange.rb', line 23

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:



59
60
61
62
63
# File 'lib/hung_secondGem/orange.rb', line 59

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



53
54
55
56
57
# File 'lib/hung_secondGem/orange.rb', line 53

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

#setFollowRedirect(flag) ⇒ Object



79
80
81
# File 'lib/hung_secondGem/orange.rb', line 79

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

#setHost(host) ⇒ Object



71
72
73
# File 'lib/hung_secondGem/orange.rb', line 71

def setHost(host)
  self.class.base_uri host
end