Class: PostcodeAnywhere::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/postcode_anywhere/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
# 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

Instance Method Details

#connectionObject



46
47
48
# File 'lib/postcode_anywhere/client.rb', line 46

def connection
  @connection ||= Faraday.new(@endpoint, connection_options)
end

#connection_optionsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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 = {}) ⇒ Object

Perform an HTTP GET request



22
23
24
# File 'lib/postcode_anywhere/client.rb', line 22

def get(path, body_hash = {}, params = {})
  request(:get, path, params, body_hash)
end

#middlewareObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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 = {}) ⇒ Object

Perform an HTTP POST request



27
28
29
# File 'lib/postcode_anywhere/client.rb', line 27

def post(path, body_hash = {}, params = {})
  request(:post, path, params, body_hash)
end