Class: JustFootball::Acceptance::Adapters::Base

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

Direct Known Subclasses

Challenge, Places

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



9
10
11
# File 'lib/adapters/base.rb', line 9

def initialize
  @connection = Faraday.new(:url => endpoint)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/adapters/base.rb', line 7

def connection
  @connection
end

Instance Method Details

#del(url) ⇒ Object



46
47
48
49
50
51
# File 'lib/adapters/base.rb', line 46

def del url
  connection.delete do |request|
    request.url url
    request.headers['Content-Type'] = 'application/json'
  end
end

#get(url) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/adapters/base.rb', line 37

def get url
  response = connection.get do |request|
    request.url url
    request.headers['Content-Type'] = 'application/json'
  end

  make_response response
end

#make_json(input) ⇒ Object



53
54
55
# File 'lib/adapters/base.rb', line 53

def make_json input
  JSON.parse input, { symbolize_names: true }
end

#make_response(response) ⇒ Object



57
58
59
60
61
62
# File 'lib/adapters/base.rb', line 57

def make_response response
  {
    status: response.status,
    body: make_json(response.body)
  }
end

#post(url, body) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/adapters/base.rb', line 17

def post url, body
  response = connection.post do |request|
    request.url url
    request.headers['Content-Type'] = 'application/json'
    request.body = body.to_json
  end

  make_response response
end

#put(url, body) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/adapters/base.rb', line 27

def put url, body
  response = connection.put do |request|
    request.url url
    request.headers['Content-Type'] = 'application/json'
    request.body = body.to_json
  end

  make_response response
end