A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://rubydoc.info/github/piotrmurach/github/master/Github/Request/Jsonize below:

Jsonize – Documentation for piotrmurach/github (master) – RubyDoc.info

Class: Github::Request::Jsonize
Inherits:
Faraday::Middleware show all
Defined in:
lib/github_api/request/jsonize.rb
Constant Summary collapse
CONTENT_TYPE =
'Content-Type'.freeze
MIME_TYPE =
'application/json'.freeze
Instance Method Summary collapse Instance Method Details #call(env) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/github_api/request/jsonize.rb', line 13

def call(env)
  if request_with_body?(env)
    env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
    env[:body] = encode_body env[:body] unless env[:body].respond_to?(:to_str)
  elsif safe_to_modify?(env)
        if [:put, :patch, :post].include?(env[:method])
      env[:body] = encode_body({})
    end
  end
  @app.call env
end
#encode_body(value) ⇒ Object
26
27
28
29
30
31
32
# File 'lib/github_api/request/jsonize.rb', line 26

def encode_body(value)
  if MultiJson.respond_to?(:dump)
    MultiJson.dump(value)
  else
    MultiJson.encode(value)
  end
end
#has_body?(env) ⇒ Boolean

Don’t encode bodies in string form

44
45
46
# File 'lib/github_api/request/jsonize.rb', line 44

def has_body?(env)
  body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
end
#request_type(env) ⇒ Object
48
49
50
51
52
# File 'lib/github_api/request/jsonize.rb', line 48

def request_type(env)
  type = env[:request_headers][CONTENT_TYPE].to_s
  type = type.split(';', 2).first if type.index(';')
  type
end
#request_with_body?(env) ⇒ Boolean
34
35
36
# File 'lib/github_api/request/jsonize.rb', line 34

def request_with_body?(env)
  has_body?(env) and safe_to_modify?(env)
end
#safe_to_modify?(env) ⇒ Boolean
38
39
40
41
# File 'lib/github_api/request/jsonize.rb', line 38

def safe_to_modify?(env)
  type = request_type(env)
  type.empty? or type == MIME_TYPE
end

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4