Class: RestfulMapper::EndpointDefinition
- Defined in:
- lib/restful_mapper.rb
Instance Method Summary collapse
- #basic_authentication_data(params) ⇒ Object
- #body_parameter(body_parameter) ⇒ Object
- #call_service(params) ⇒ Object
- #deserialize(json, mapping) ⇒ Object
- #filter_parameters(hash) ⇒ Object
- #has_basic_authentication? ⇒ Boolean
-
#initialize(base_url, method, basic_authentication) ⇒ EndpointDefinition
constructor
A new instance of EndpointDefinition.
- #path(path, options = {}) ⇒ Object
- #query_parameters(parameters) ⇒ Object
- #responses(response_mapping = {}) ⇒ Object
Constructor Details
#initialize(base_url, method, basic_authentication) ⇒ EndpointDefinition
Returns a new instance of EndpointDefinition.
20 21 22 23 24 25 |
# File 'lib/restful_mapper.rb', line 20 def initialize base_url, method, basic_authentication @base_url=base_url @query_parameters=[] @method=method @basic_authentication=basic_authentication end |
Instance Method Details
#basic_authentication_data(params) ⇒ Object
86 87 88 |
# File 'lib/restful_mapper.rb', line 86 def basic_authentication_data params @basic_authentication.map{|name| Symbol === name ? params[name] : name} end |
#body_parameter(body_parameter) ⇒ Object
35 36 37 |
# File 'lib/restful_mapper.rb', line 35 def body_parameter body_parameter @body_parameter=body_parameter end |
#call_service(params) ⇒ Object
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 |
# File 'lib/restful_mapper.rb', line 51 def call_service params conn = Faraday.new(:url => @base_url) do |faraday| # faraday.response :logger # log requests to STDOUT faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end if has_basic_authentication? conn.basic_auth(*basic_authentication_data(params)) end response=conn.run_request(@method, Mustache.render(@path, params), 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 result=deserialize body, status_class if Exception === result raise result end result end |
#deserialize(json, mapping) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/restful_mapper.rb', line 91 def deserialize json, mapping 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 end |
#filter_parameters(hash) ⇒ Object
45 46 47 48 49 |
# File 'lib/restful_mapper.rb', line 45 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? ⇒ Boolean
82 83 84 |
# File 'lib/restful_mapper.rb', line 82 def has_basic_authentication? @basic_authentication end |
#path(path, options = {}) ⇒ Object
27 28 29 |
# File 'lib/restful_mapper.rb', line 27 def path path, ={} @path=path end |
#query_parameters(parameters) ⇒ Object
40 41 42 |
# File 'lib/restful_mapper.rb', line 40 def query_parameters parameters @query_parameters=parameters.dup end |
#responses(response_mapping = {}) ⇒ Object
31 32 33 |
# File 'lib/restful_mapper.rb', line 31 def responses response_mapping={} @response_mapping=response_mapping end |