class Swagger::Request

Attributes

body[RW]
configuration[RW]
format[RW]
headers[RW]
host[RW]
http_method[RW]
params[RW]
path[RW]

Public Class Methods

new(http_method, path, attributes={}, configuration={}) click to toggle source

All requests must have an HTTP method and a path Optionals parameters are :params, :headers, :body, :format, :host

# File lib/swagger/request.rb, line 14
def initialize(http_method, path, attributes={}, configuration={})
  self.configuration = Swagger.configuration.clone()
  self.configuration.update(configuration || {})

  attributes[:format] ||= self.configuration.format
  attributes[:params] ||= {}

  # Set default headers
  default_headers = {
    'Content-Type' => "application/#{attributes[:format].downcase}",
    'Token' => self.configuration.api_key,
    'Source-Id' => "taxamo-ruby/" + Taxamo::VERSION
  }

  # api_key from headers hash trumps the default, even if its value is blank
  if attributes[:headers].present? && attributes[:headers].has_key?(:api_key)
    default_headers.delete(:api_key)
  end

  # api_key from params hash trumps all others (headers and default_headers)
  if attributes[:params].present? && attributes[:params].has_key?(:api_key)
    default_headers.delete(:api_key)
    attributes[:headers].delete(:api_key) if attributes[:headers].present?
  end

  # Merge argument headers into defaults
  attributes[:headers] = default_headers.merge(attributes[:headers] || {})

  self.http_method = http_method.to_sym
  self.path = path
  attributes.each do |name, value|
    send("#{name.to_s.underscore.to_sym}=", value)
  end
end

Public Instance Methods

body=(value) click to toggle source

Massage the request body into a state of readiness If body is a hash, camelize all keys then convert to a json string

# File lib/swagger/request.rb, line 105
def body=(value)
  #if value.is_a?(Hash)
  #  value = value.inject({}) do |memo, (k,v)|
  #    memo[k.to_s.camelize(:lower).to_sym] = v
  #    memo
  #  end
  #end
  @body = value
end
interpreted_path() click to toggle source

Iterate over the params hash, injecting any path values into the path string

e.g. /word.{format}/{word}/entries => /word.json/cat/entries

# File lib/swagger/request.rb, line 71
def interpreted_path
  p = self.path.dup

  # Fill in the path params
  self.params.each_pair do |key, value|
    p = p.gsub("{#{key}}", value.to_s)
  end

  # # Stick a .{format} placeholder into the path if there isn't
  # # one already or an actual format like json or xml
  # # e.g. /words/blah => /words.{format}/blah
  # if Swagger.configuration.inject_format
  #   unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
  #     p = p.sub(/^(\/?\w+)/, "\\1.#{format}")
  #   end
  # end
  #
  # # Stick a .{format} placeholder on the end of the path if there isn't
  # # one already or an actual format like json or xml
  # # e.g. /words/blah => /words/blah.{format}
  # if Swagger.configuration.force_ending_format
  #   unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
  #     p = "#{p}.#{format}"
  #   end
  # end
  #
  # p = p.sub("{format}", self.format.to_s)
  #
  URI.encode [self.configuration.base_path, p].join("/").gsub(/\/+/, '/')
end
make() click to toggle source
# File lib/swagger/request.rb, line 147
def make
  if self.configuration && self.configuration.logger
    self.configuration.logger.debug self.url
  end
  response = case self.http_method.to_sym
  when :get,:GET
    Typhoeus::Request.get(
      self.url,
      :headers => self.headers.stringify_keys,
    )

  when :post,:POST
    Typhoeus::Request.post(
      self.url,
      :body => self.outgoing_body,
      :headers => self.headers.stringify_keys,
    )

  when :put,:PUT
    Typhoeus::Request.put(
      self.url,
      :body => self.outgoing_body,
      :headers => self.headers.stringify_keys,
    )
  
  when :delete,:DELETE
    Typhoeus::Request.delete(
      self.url,
      :body => self.outgoing_body,
      :headers => self.headers.stringify_keys,
    )
  end
  Response.new(response)
end
outgoing_body() click to toggle source

If body is an object, JSONify it before making the actual request.

# File lib/swagger/request.rb, line 117
def outgoing_body
  body.is_a?(String) ? body : body.to_json
end
query_string() click to toggle source

Construct a query string from the query-string-type params

# File lib/swagger/request.rb, line 122
def query_string

  # Iterate over all params,
  # .. removing the ones that are part of the path itself.
  # .. stringifying values so Addressable doesn't blow up.
  query_values = {}
  self.params.each_pair do |key, value|
    next if self.path.include? "{#{key}}"                                   # skip path params
    next if value.blank? && value.class != FalseClass                       # skip empties
    if self.configuration.camelize_params
      key = key.to_s.camelize(:lower).to_sym unless key.to_sym == :api_key    # api_key is not a camelCased param
    end
    query_values[key.to_s] = value.to_s
  end

  # We don't want to end up with '?' as our query string
  # if there aren't really any params
  return "" if query_values.blank?

  # Addressable requires query_values to be set after initialization..
  qs = Addressable::URI.new
  qs.query_values = query_values
  qs.to_s
end
response() click to toggle source
# File lib/swagger/request.rb, line 182
def response
  self.make
end
response_code_pretty() click to toggle source
# File lib/swagger/request.rb, line 186
def response_code_pretty
  return unless @response.present?
  @response.code.to_s    
end
response_headers_pretty() click to toggle source
# File lib/swagger/request.rb, line 191
def response_headers_pretty
  return unless @response.present?
  # JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>') # <- This was for RestClient
  @response.headers.gsub(/\n/, '<br/>') # <- This is for Typhoeus
end
url(options = {}) click to toggle source

Construct a base URL

# File lib/swagger/request.rb, line 51
def url(options = {})
  u = Addressable::URI.new(
    :scheme => self.configuration.scheme,
    :host => self.configuration.host,
    :path => self.interpreted_path,
    :query => self.query_string.sub(/\?/, '')
  ).to_s

  # Drop trailing question mark, if present
  u.sub! /\?$/, ''

  # Obfuscate API key?
  u.sub! /api\_key=\w+/, 'api_key=YOUR_API_KEY' if options[:obfuscated]

  u
end