Module: Vibe::Request

Extended by:
Configuration
Includes:
Error
Included in:
Client
Defined in:
lib/vibe/request.rb

Constant Summary collapse

METHODS =
[:get, :post, :put, :delete, :patch]

Constants included from Configuration

Configuration::DEFAULT_API_KEY, Configuration::DEFAULT_CACHE, Configuration::DEFAULT_CACHE_DRIVER, Configuration::DEFAULT_ENDPOINT, Configuration::DEFAULT_FORMAT, Configuration::DEFAULT_METHOD, Configuration::DEFAULT_USER_AGENT, Configuration::VALID_CONFIG_KEYS, Configuration::VALID_CONNECTION_KEYS, Configuration::VALID_OPTIONS_KEYS

Instance Method Summary collapse

Methods included from Configuration

configure, extended, options, reset

Instance Method Details

#get_request(path, params = {}, cache_override = false) ⇒ Request.request

Perform a GET HTTP request

Returns:



14
15
16
# File 'lib/vibe/request.rb', line 14

def get_request(path, params = {}, cache_override = false)
  request(:get, path, params, cache_override)
end

#request(method, path, params, cache_override = false) ⇒ String

Execute an HTTP request and return the result Exceptions would be raised based on HTTP status codes

Returns:

  • (String)


22
23
24
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vibe/request.rb', line 22

def request(method, path, params, cache_override = false)
  if !METHODS.include?(method)
   raise ArgumentError, "unkown http method: #{method}"
  end

  # Is caching activated?
  if cache && !cache_override
    cache = Cache.new driver: cache_driver

    # Check if cache present
    param_string = params.map{|k,v| "#{k}-#{v}"}.join(':')
    filename = Digest::MD5.hexdigest(method.to_s.gsub("/", "_")+path.to_s.gsub("/", "_")+param_string).freeze
    response = cache.get(filename)

    if response
      return JSON.parse(response)
    end
  end

  if !api_key
    raise ClientError, 'API key need to be set'
  end

  puts "EXECUTED: #{method} - #{path} with #{params}" if ENV['DEBUG']

  # If API key is passed as a paramter then ignore the one
  # set in config
  if params[:api_key]
    new_api_key = params[:api_key]
    params = params.tap { |hs| hs.delete(:api_key) }
    params.merge!({:key => new_api_key, :user_agent => user_agent})
  else
    params.merge!({:key => api_key, :user_agent => user_agent})
  end

  begin
    puts params if ENV['DEBUG']
    response = RestClient.send("#{method}", endpoint + path.gsub!(/^\//, ""), :params => params){ |response, request, result, &block|
      # Go through redirection based on status code
      if [301, 302, 307].include? response.code
        response.follow_redirection(request, result, &block)
      elsif !response.code.between?(200, 400)
          if response.code == 401
            resp = JSON.parse(response.body)
            raise Unauthorized, {:status => response.code, :method => method, :response_headers => response.headers, :url => endpoint + path, :body => resp['status']}
          elsif response.code == 403
            resp = JSON.parse(response.body)
            raise Forbidden, {:status => response.code, :method => method, :response_headers => response.headers, :url => endpoint + path, :body => resp['status']}
          elsif response.code == 422
            resp = JSON.parse(response.body)
            raise UnprocessableEntity, {:status => response.code, :method => method, :response_headers => response.headers, :url => endpoint + path, :body => resp['status']}
          else
            raise ServiceError, {:status => response.code, :method => method, :response_headers => response.headers, :url => endpoint + path, :body => "API Returned status code #{response.code}"}
          end
      else
        # Is caching activated?
        if cache
          # Put into cache
          if !cache.put(filename, response)
            raise ClientError, 'Unable to write into Cache'
          end
        end

        # Parse JSON response
        JSON.parse(response)
      end
    }
  rescue SocketError => e
    raise VibeError, 'SocketError Occured!'
  end

end