class RestfulMapper::EndpointDefinition

Public Class Methods

new(base_url, method, basic_authentication, token) click to toggle source
# File lib/restful_mapper.rb, line 26
def initialize base_url, method, basic_authentication, token
  @base_url=base_url
  @query_parameters=[]
  @method=method
  @basic_authentication=basic_authentication
  @token=token
end

Public Instance Methods

basic_authentication_data(params) click to toggle source
# File lib/restful_mapper.rb, line 106
def basic_authentication_data params
  @basic_authentication.map{|name| Symbol === name ? params[name] : name}
end
body_parameter(body_parameter) click to toggle source
# File lib/restful_mapper.rb, line 46
def body_parameter body_parameter
  @body_parameter=body_parameter
end
call_service(params) click to toggle source
# File lib/restful_mapper.rb, line 62
def call_service params

  puts "base url: %s" % @base_url

  conn = Faraday.new(url: Mustache.render(@base_url, params)) do |faraday|
    faraday.use FaradayMiddleware::FollowRedirects, limit: 5
    if @verbose
      faraday.response :logger
    end
    faraday.adapter Faraday.default_adapter  # make requests with Net::HTTP
  end

  if has_basic_authentication?
    conn.basic_auth(*basic_authentication_data(params))
  end

  if has_token?
    conn.authorization :Bearer, Mustache.render(@token, params)
  end

  path = Mustache.render(@path, params)

  response=conn.run_request(@method, path, nil, {'Content-Type' => 'application/json'}) { |request|
    request.params.update(filter_parameters(params)) if filter_parameters(params)
    if @body_parameter
      request.body=MultiJson.dump(params[@body_parameter].to_structure)
    end
  }

  status=response.status
  status_class=@response_mapping[status]
  status_class||=@response_mapping[0]
  body=response.body
  deserialize body, response.headers['Content-Type'], status_class
end
deserialize(json, content_type, mapping) click to toggle source
# File lib/restful_mapper.rb, line 110
def deserialize json, content_type, mapping
  if mapping.is_a?(Class) && Exception >= mapping
    raise mapping, json
  end
  if content_type && content_type.start_with?('application/json')
    if json && !json.empty?
      mapping.from_structure(MultiJson.load(json))
    else
      if Hash == mapping || Array == mapping || mapping.is_a?(Class)
        mapping.new
      else
        mapping
      end
    end
  elsif mapping.respond_to?(:from_content)
    mapping.from_content(content_type, json)
  else
    mapping
  end

end
filter_parameters(hash) click to toggle source
# File lib/restful_mapper.rb, line 56
def filter_parameters hash
  hash.dup.delete_if do |k,v|
    not (@query_parameters.include?(k.to_sym) || @query_parameters.include?(k.to_s))
  end
end
has_basic_authentication?() click to toggle source
# File lib/restful_mapper.rb, line 98
def has_basic_authentication?
  @basic_authentication
end
has_token?() click to toggle source
# File lib/restful_mapper.rb, line 102
def has_token?
  @token
end
path(path, options={}) click to toggle source
# File lib/restful_mapper.rb, line 34
def path path, options={}
  @path=path
end
query_parameters(parameters) click to toggle source
# File lib/restful_mapper.rb, line 51
def query_parameters parameters
  @query_parameters=parameters.dup
end
responses(response_mapping={}) click to toggle source
# File lib/restful_mapper.rb, line 42
def responses response_mapping={}
  @response_mapping=response_mapping
end
verbose() click to toggle source
# File lib/restful_mapper.rb, line 38
def verbose
  @verbose=true
end