class PostcodeAnywhere::Client
Public Class Methods
new(options = {})
click to toggle source
# File lib/postcode_anywhere/client.rb, line 13 def initialize(options = {}) merged_options = PostcodeAnywhere.options.merge(options) Configuration::VALID_CONFIG_KEYS.each do |key| send("#{key}=", merged_options[key]) end end
Public Instance Methods
connection()
click to toggle source
# File lib/postcode_anywhere/client.rb, line 46 def connection @connection ||= Faraday.new(@endpoint, connection_options) end
connection_options()
click to toggle source
# File lib/postcode_anywhere/client.rb, line 31 def connection_options @connection_options ||= { builder: middleware, headers: { accept: "application/#{@format}", content_type: "application/#{@format}", user_agent: user_agent }, request: { open_timeout: 10, timeout: 30 } } end
get(path, body_hash = {}, params = {})
click to toggle source
Perform an HTTP GET request
# File lib/postcode_anywhere/client.rb, line 22 def get(path, body_hash = {}, params = {}) request(:get, path, params, body_hash) end
middleware()
click to toggle source
# File lib/postcode_anywhere/client.rb, line 50 def middleware @middleware ||= Faraday::RackBuilder.new do |faraday| # Checks for files in the payload, otherwise leaves everything untouched faraday.request :multipart # Encodes as "application/x-www-form-urlencoded" if not already encoded faraday.request :url_encoded # Handle error responses faraday.response :postcode_anywhere_raise_error # Parse JSON response bodies faraday.response :postcode_anywhere_parse_json # Set default HTTP adapter faraday.adapter :net_http end end
post(path, body_hash = {}, params = {})
click to toggle source
Perform an HTTP POST request
# File lib/postcode_anywhere/client.rb, line 27 def post(path, body_hash = {}, params = {}) request(:post, path, params, body_hash) end
Private Instance Methods
attach_api_key_to(params)
click to toggle source
# File lib/postcode_anywhere/client.rb, line 78 def attach_api_key_to(params) params.merge!('Key' => @api_key) unless params.keys.include? 'Key' end
compile_body(body_hash)
click to toggle source
# File lib/postcode_anywhere/client.rb, line 82 def compile_body(body_hash) body_hash.to_json end
request(method, path, params = {}, body_hash = {})
click to toggle source
# File lib/postcode_anywhere/client.rb, line 67 def request(method, path, params = {}, body_hash = {}) attach_api_key_to params connection.send(method.to_sym, path, params) do |request| request.body = compile_body(body_hash) unless body_hash.empty? end.env rescue Faraday::Error::TimeoutError, Timeout::Error => error raise(PostcodeAnywhere::Error::RequestTimeout.new(error)) rescue Faraday::Error::ClientError, JSON::ParserError => error raise(PostcodeAnywhere::Error.new(error)) end