Class: Reddit::Base::Client

Inherits:
BasicClient show all
Defined in:
lib/reddit/base/client.rb

Overview

Client that does everything BasicClient does but also attempts to coerce and parse JSON.

Constant Summary

Constants inherited from BasicClient

BasicClient::DEFAULT_OPTIONS, BasicClient::DEFAULT_URL, BasicClient::DEFAULT_URL_SECURE

Instance Attribute Summary

Attributes inherited from BasicClient

#connection, #options

Instance Method Summary collapse

Methods inherited from BasicClient

#build_connection

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
# File 'lib/reddit/base/client.rb', line 8

def initialize(options)
  super(options)

  connection.builder.insert_before FaradayMiddleware::FollowRedirects, FaradayMiddleware::ParseJson
  connection.builder.insert_before FaradayMiddleware::Reddit::Modhash, Faraday::ManualCache, expires_in: 30
  connection.builder.insert_before Faraday::ManualCache, FaradayMiddleware::Reddit::ForceJson
end

Instance Method Details

#delete(url, **options) ⇒ Object



16
17
18
19
20
21
# File 'lib/reddit/base/client.rb', line 16

def delete(url, **options)
  nocache = options.delete(:nocache)

  response = connection.delete(url, **options)
  mash response
end

#get(url, **options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/reddit/base/client.rb', line 23

def get(url, **options)
  nocache = options.delete(:nocache)

  response = connection.get do |req|
    req.url url
    req.headers['x-faraday-manual-cache'] = 'NOCACHE' if nocache
    req.params = options
  end

  mash response
end

#mash(response) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/reddit/base/client.rb', line 49

def mash(response)
  if response.body.is_a? Array
    response.body.map { |x| Mash.new x }
  else
    Mash.new response.body
  end
end

#post(url, **options) ⇒ Object



35
36
37
38
39
40
# File 'lib/reddit/base/client.rb', line 35

def post(url, **options)
  nocache = options.delete(:nocache)

  response = connection.post(url, **options)
  mash response
end

#put(url, **options) ⇒ Object



42
43
44
45
46
47
# File 'lib/reddit/base/client.rb', line 42

def put(url, **options)
  nocache = options.delete(:nocache)

  response = connection.put(url, **options)
  mash response
end