Class: DefaultRest

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

Class Method Summary collapse

Class Method Details

.base_paramsObject



108
109
110
# File 'lib/api_tools/default_rest.rb', line 108

def base_params
  {} # 子类中复写
end

.basic_request(request_dict, user_options) ⇒ Object



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
# File 'lib/api_tools/default_rest.rb', line 65

def basic_request(request_dict, user_options)
  exception = nil
  user_options[:retry_times].times do
    begin
      response = ::RestClient::Request.execute(request_dict)
      return ::Oj.load(response.body, symbol_keys: true) if user_options[:response_json]
      return response.body
    rescue ::RestClient::ExceptionWithResponse => ex
      raise ex if user_options[:exception_with_response]
      return {
        status: false,
        response_code: ex.response.code,
        response_body: ex.response.body,
        message: ex.message
      }
    rescue RestClient::Exception => e
      exception = e
      next
    end
  end
  raise exception unless user_options[:ensure_no_exception]
  {
    status: false,
    message: ex.message
  }
end

.basic_urlObject



104
105
106
# File 'lib/api_tools/default_rest.rb', line 104

def basic_url
  'http://www.example.com' # 子类中复写
end

.build_similar_get_request(word, path, user_params, user_options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/api_tools/default_rest.rb', line 27

def build_similar_get_request(word, path, user_params, user_options)
  # 生成类get 请求的URL
  path_params = URI.escape(user_params.collect { |k, v| "#{k}=#{v}" }.join('&'))
  tmp = path.include?('?') ? '&' : '?'
  path = path + tmp + path_params
  url = build_whole_url(path)
  {
    method: word,
    url: url,
    headers: user_options[:header],
    timeout: user_options[:timeout]
  }
end

.build_similar_post_request(word, path, user_params, user_options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/api_tools/default_rest.rb', line 41

def build_similar_post_request(word, path, user_params, user_options)
  url = build_whole_url(path)
  payload = user_options[:params_to_json] ? user_params.to_json : user_params
  {
    method: word,
    url: url,
    payload: payload,
    headers: user_options[:header],
    timeout: user_options[:timeout]
  }
end

.build_whole_url(path) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/api_tools/default_rest.rb', line 53

def build_whole_url(path)
  web = basic_url
  return web if path.length.zero?
  return path if path.start_with?("http") # path 是一个绝对路径
  if web[-1] == "/"
    path = path[1..-1] if path[0] == "/"
  else
    path = "/#{path}" if path[0] != "/"
  end
  web + path
end

.default_optionsObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/api_tools/default_rest.rb', line 92

def default_options
  @default_options ||= {
    timeout: 5,
    retry_times: 5,
    response_json: true,
    params_to_json: true,
    ensure_no_exception: false,
    header: { content_type: :json, accept: :json },
    exception_with_response: true
  }
end