Class: Rest

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

Class Method Summary collapse

Class Method Details

.already_exists_error(result) ⇒ Object



140
141
142
# File 'lib/rest_api.rb', line 140

def already_exists_error(result)
  result["code"] == 422 && result["response"].keys.count >= 1 && (result["response"][result["response"].keys[0]][0] == "has already been taken" || result["response"][result["response"].keys[0]][0] =~ /Version Tag is not unique/)
end

.delete(url, options = {}) ⇒ Object



22
23
24
# File 'lib/rest_api.rb', line 22

def delete(url, options = {})
  rest_call(url, "delete", options)
end

.get(url, options = {}) ⇒ Object



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

def get(url, options = {})
  rest_call(url, "get", options)
end

.not_found_error(result) ⇒ Object



144
145
146
# File 'lib/rest_api.rb', line 144

def not_found_error(result)
  result["code"] == 404
end

.post(url, data, options = {}) ⇒ Object



12
13
14
15
# File 'lib/rest_api.rb', line 12

def post(url, data, options = {})
  options[:data] = data
  rest_call(url, "post", options)
end

.put(url, data, options = {}) ⇒ Object



17
18
19
20
# File 'lib/rest_api.rb', line 17

def put(url, data, options = {})
  options[:data] = data
  rest_call(url, "put", options)
end

.rest_call(url, method, options = {}) ⇒ Object

Makes an http method call and returns data in JSON

Attributes

  • url - the url for the request

  • method - the http method [get, put, post]

  • options - a hash of options

    +verbose+: gives verbose output (yes/no)
    +data+: required for put and post methods a hash of post data
    +username+: username for basic http authentication
    +password+: password for basic http authentication
    +suppress_errors+: continues after errors if true
    

Returns

  • returns a hash of the http response with these keys

  • status - success or ERROR

  • message - if status is ERROR this will hold an error message

  • code - the http status code 200=ok, 404=not found, 500=error, 504=not authorized

  • data - the body of the http response



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rest_api.rb', line 46

def rest_call(url, method, options = {})
  methods = %w{get post put delete}
  result = rest_params = {}
  rest_params[:url] = URI.escape(url)

  unless methods.include?(method.downcase)
    BrpmAuto.log "No method named: #{method}"
    result["code"] = -1
    result["status"] = "failure"
    result["message"] = "No method named: #{method}"
    return result
  end

  rest_params[:method] = method.downcase

  begin
    BrpmAuto.log("REST #{method.upcase} #{BrpmAuto.privatize(url, get_token(url))}") unless options.has_key?("quiet")

    if options.has_key?(:username) && options.has_key?(:password)
      rest_params[:user] = options[:username]
      rest_params[:password] = options[:password]
    end

    if %{put post}.include?(method.downcase)
      options[:data] = options["data"] if options.has_key?("data")
      rest_params[:payload] = options[:data].to_json if options.has_key?(:data)
      if !options.has_key?(:data) || rest_params[:payload].length < 4
        result["code"] = -1
        result["status"] = "failure"
        result["message"] = "No Post data"
        return result
      end
      BrpmAuto.log "\tPost Data: #{rest_params[:payload].inspect}" unless options.has_key?("quiet")
    end

    rest_params.merge!({:headers => { :accept => :json, :content_type => :json }})
    rest_params.merge!({:verify_ssl => OpenSSL::SSL::VERIFY_NONE})
    rest_params.merge!({:cookies => options["cookies"] }) if options.has_key?("cookies")

    BrpmAuto.log rest_params.inspect if options.has_key?("verbose")

    response = RestClient::Request.new(rest_params).execute

    BrpmAuto.log "\tParsing response to JSON format ..." if options.has_key?("verbose")
    begin
      parsed_response = JSON.parse(response)
    rescue
      parsed_response = response
    end
    BrpmAuto.log "Parsed response: #{parsed_response.inspect}" if options.has_key?("verbose")

    if response.code < 300
      BrpmAuto.log "\treturn code: #{response.code}"  unless options.has_key?("quiet")
      result["status"] = "success"
      result["code"] = response.code
      result["response"] = parsed_response
      result["cookies"] = response.cookies if options.has_key?("cookies")
    else
      result["status"] = "error"
      result["code"] = response.code
      result["response"] = parsed_response
      result["error_message"] = "REST call returned HTTP code:\n#{response.code}\n#{response}"
      BrpmAuto.log "\tREST call returned HTTP code #{response.code}"  unless options.has_key?("quiet")
    end

  rescue RestClient::Exception => e
    BrpmAuto.log "\tREST call generated an error: #{e.message}" unless options.has_key?("quiet")

    if e.response.nil?
      result["status"] = "error"
      result["code"] = 1000
      result["response"] = "No response received."
      result["error_message"] = "REST call generated a RestClient error:\n#{e.message}\n#{e.backtrace}"
      return
    end

    BrpmAuto.log "\tParsing response to JSON format ..."  unless options.has_key?("quiet")
    begin
      parsed_response = JSON.parse(e.response)
    rescue
      parsed_response = e.response
    end
    BrpmAuto.log "\tParsed response: #{parsed_response.inspect}"

    result["status"] = "error"
    result["code"] = e.response.code
    result["response"] = parsed_response
    result["error_message"] = "REST call generated a RestClient error:\n#{e.response.code}\n#{e.response}\n#{e.message}\n#{e.backtrace}"

    BrpmAuto.log "Back trace:\n#{e.backtrace}" unless already_exists_error(result) or not_found_error(result)
  end
  result
end