Class: Glib::JsonCrawler::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/glib/json_crawler/http.rb

Constant Summary collapse

URI_REGEXP =
/\A#{URI::regexp}\z/
VALID_RESPONSE_CODES =
[
  (200..299).to_a,
  ## Note, the JSON API does not allow redirects
  401, ## UNAUTHORIZED Should be used for not-logged-in
  403, ## FORBIDDEN Should be used for logged-in but not allowed to access
].flatten

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, user) ⇒ Http

Returns a new instance of Http.



15
16
17
18
19
20
# File 'lib/glib/json_crawler/http.rb', line 15

def initialize(context, user)
  @context = context
  @history = []
  @response_times = []
  @user = user
end

Instance Attribute Details

#historyObject

Returns the value of attribute history.



4
5
6
# File 'lib/glib/json_crawler/http.rb', line 4

def history
  @history
end

#response_timesObject

Returns the value of attribute response_times.



4
5
6
# File 'lib/glib/json_crawler/http.rb', line 4

def response_times
  @response_times
end

#userObject

Returns the value of attribute user.



4
5
6
# File 'lib/glib/json_crawler/http.rb', line 4

def user
  @user
end

Instance Method Details

#delete(url, controller, params = {}) ⇒ Object



69
70
71
# File 'lib/glib/json_crawler/http.rb', line 69

def delete(url, controller, params = {})
  fetch(:delete, url, controller, {})
end

#get(url, controller) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/glib/json_crawler/http.rb', line 22

def get(url, controller)
  fetch(:get, url, controller)

  # http_header = {
  #   'Client-DeviceOS' => user[:device],
  #   'Client-Version' => user[:version],
  # }

  # # Allow repeating urls because excluding them makes the test results really hard to analyze.
  # # if history.include?(url)

  # if url.blank?
  #   nil
  # else
  #   @context.assert_match URI_REGEXP, url
  #   history << url
  #   @context.get url, params: {}, headers: http_header
  #   response = @context.response
  #   JsonCrawler::Router.log controller, url, response
  #   if (code = response.code.to_i) == 302
  #     redirect_uri = URI(response.headers['Location'])
  #     redirect_uri.query = [redirect_uri.query, "format=json"].compact.join('&')
  #     return get redirect_uri.to_s, controller
  #   else
  #     response_times << response.headers['X-Runtime'].to_f
  #     @context.assert_includes VALID_RESPONSE_CODES, code, "Expected a valid response but was [#{response.code}] #{Rack::Utils::HTTP_STATUS_CODES[response.code.to_i]}:\n#{url}"
  #     if response.content_type == 'application/json'
  #       JSON.parse(response.body)
  #     else
  #       nil
  #     end
  #   end
  # end
end

#patch(url, controller, params) ⇒ Object



61
62
63
# File 'lib/glib/json_crawler/http.rb', line 61

def patch(url, controller, params)
  fetch(:patch, url, controller, params)
end

#post(url, controller, params) ⇒ Object



57
58
59
# File 'lib/glib/json_crawler/http.rb', line 57

def post(url, controller, params)
  fetch(:post, url, controller, params)
end

#put(url, controller, params) ⇒ Object



65
66
67
# File 'lib/glib/json_crawler/http.rb', line 65

def put(url, controller, params)
  fetch(:put, url, controller, params)
end