Class: Immobilienscout::Request

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

Instance Method Summary collapse

Constructor Details

#initialize(url, params = {}) ⇒ Request

Returns a new instance of Request.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/immobilienscout/request.rb', line 8

def initialize(url, params = {})
  @url = url
  @params = params

  raise ArgumentError unless valid?
end

Instance Method Details

#deleteObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/immobilienscout/request.rb', line 66

def delete
  auth_header = generate_auth_header('DELETE')
  headers = generate_headers(auth_header)

  uri = URI.parse(@url)

  request = Net::HTTP::Delete.new(uri)
  request.initialize_http_header(headers)

  execute_request(uri, request)
end

#getObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/immobilienscout/request.rb', line 15

def get
  auth_header = generate_auth_header('GET', @params)
  headers = generate_headers(auth_header)

  url_with_query = "#{@url}?#{@params.to_query}"
  uri = URI.parse(url_with_query)

  request = Net::HTTP::Get.new(uri)
  request.initialize_http_header(headers)

  execute_request(uri, request)
end

#postObject



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

def post
  auth_header = generate_auth_header('POST')
  headers = generate_headers(auth_header)

  uri = URI.parse(@url)

  request = Net::HTTP::Post.new(uri)
  request.body = @params.to_json
  request.initialize_http_header(headers)

  execute_request(uri, request)
end

#post_with_multipartObject



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

def post_with_multipart
  auth_header = generate_auth_header('POST')

  uri = URI.parse(@url)

  request = Net::HTTP::Post::Multipart.new(uri, @params)
  request.add_field('Authorization', auth_header)
  request.add_field('Accept', 'application/json')

  execute_request(uri, request)
end

#putObject



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

def put
  auth_header = generate_auth_header('PUT')
  headers = generate_headers(auth_header)

  uri = URI.parse(@url)

  request = Net::HTTP::Put.new(uri)
  request.body = @params.to_json
  request.initialize_http_header(headers)

  execute_request(uri, request)
end