Module: LinkShrink::Request

Included in:
LinkShrink
Defined in:
lib/link_shrink/request.rb

Overview

Handles request and calls parser

Author:

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
# File 'lib/link_shrink/request.rb', line 7

def self.included(base)
  base.extend self
end

.parse(response, options, shrinker) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/link_shrink/request.rb', line 17

def parse(response, options, shrinker)
  if shrinker.content_type.eql?('text/plain')
    response
  else
    process_response(response, options, shrinker)
  end
end

.process_request(url, options, shrinker = LinkShrink::Config.api) ⇒ Object



13
14
15
# File 'lib/link_shrink/request.rb', line 13

def process_request(url, options, shrinker = LinkShrink::Config.api)
  parse(request(url, shrinker).body, options, shrinker)
end

.process_response(response, options, shrinker, json = JSONParser) ⇒ Object



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
# File 'lib/link_shrink/request.rb', line 25

def process_response(response, options, shrinker, json = JSONParser)
  option      = Options.new(options)
  parsed_json = json.parse_json(response)
  plain       = parsed_json['id']

  if option.json? && option.qr_code?
    if option.image_size?
      return parsed_json.merge(qr_code: shrinker.generate_chart_url(plain, options.fetch(:image_size))).to_json
    end
    return parsed_json.merge(qr_code: shrinker.generate_chart_url(plain)).to_json
  end

  case
    when option.json?
      json.cleanup_json(response)
    when option.qr_code?
      if option.image_size?
        return shrinker.generate_chart_url(plain, options.fetch(:image_size))
      end
      shrinker.generate_chart_url(plain)
    when option.image_size?
      shrinker.generate_chart_url(plain, options.fetch(:image_size))
    else
      plain
  end
end

.request(new_url, shrinker) ⇒ Object

Calls URL API



55
56
57
58
59
60
61
62
63
# File 'lib/link_shrink/request.rb', line 55

def request(new_url, shrinker)
  shrinker.url = new_url
  Typhoeus::Request.new(
    shrinker.api_url,
    method:  shrinker.http_method,
    body:    shrinker.body_parameters(shrinker.url),
    headers: { 'Content-Type' => shrinker.content_type }
  ).run
end