Sha256: 861387f6a1a190fa855ee4bbb041743541b6c1dc0b42aa8e70b963bd49bdfb43

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

require 'typhoeus'

module LinkShrink
  # @author Jonah Ruiz <jonah@pixelhipsters.com>
  # Handles request and calls parser
  module Request
    def self.included(base)
      base.extend self
    end

    module_function

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

    def process_response(response, options, shrinker, json = JSONParser)
      return response if shrinker.content_type.eql?('text/plain')
      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

    # Calls URL API
    # @see LinkShrink::Shrinkers::Base#api_url
    # @see LinkShrink::Shrinkers::Base#body_parameters
    def request(url, shrinker)
      shrinker.url = url
      Typhoeus::Request.new(
        shrinker.api_url,
        method:  shrinker.http_method,
        body:    shrinker.body_parameters(url),
        headers: { 'Content-Type' => shrinker.content_type }
      ).run
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
link_shrink-0.0.3 lib/link_shrink/request.rb