fmrest 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 996b7a73eb92f7a6d7df2b0e6f7a5f082005dc258b5ab2ba5589967e9c87b60e
4
- data.tar.gz: 3ac6ecdcd60573186ca63bd30d32202ed33cb04884de8acc5bc572e13d8976f3
3
+ metadata.gz: b3b6d6aed0ef181daf2e8a254809704f09f65e6720f29adad829d028b7429322
4
+ data.tar.gz: 7d7007ff6968884ebb293e99ef901706d70a3c95721a893443cb16c9e69ac7da
5
5
  SHA512:
6
- metadata.gz: a1f892950743e0ef1cdcaf5eccb51131f4de935c6b56b3a09e0893eb39f031b0d54830e34fc88624249f4ec29cebd8aa7993e3e52b0c99c9dfc35436a9150de6
7
- data.tar.gz: 5d030409c8f51aec4bd4cae244e667be3bf758da672837893c3f34a6a82ec8d3e7f2b3461c54057a0849d63a9965bc800e5b7490510bd2e8ca4f50533064318e
6
+ metadata.gz: dd2ffd6cf2f3622b5efff65925dc5a73e3380a8270f0a6650898385620b144e2c8f7199d5512f1cce500bd973ca70e13440b5d219a6abeccb5c28b684b6a190f
7
+ data.tar.gz: ad43d73a77f18dc138b90400fc9b8213f61bce9b8659a7c84ba3c5cedeb00fcd2fca900a461125ef859263548dbf570f263b8212a698a990ab97adedffb6aff9
@@ -0,0 +1,8 @@
1
+ ## Changelog
2
+
3
+ ### 0.2.4
4
+
5
+ * Use `String#=~` instead of `String#match?` for Ruby <2.4 compatibility (Fixes
6
+ [#26](https://github.com/beezwax/fmrest-ruby/issues/26))
7
+ * Deprecate `FmRest.config` in favor of `FmRest.default_connection_settings`
8
+ * Honor Faraday SSL and proxy settings when fetching container files
data/README.md CHANGED
@@ -63,6 +63,42 @@ end
63
63
  For each request fmrest-ruby will first request a session token (using the
64
64
  provided username and password) if it doesn't yet have one in store.
65
65
 
66
+ ## Connection settings
67
+
68
+ In addition to the required `:host`, `:database`, `:username` and `:password`
69
+ connection options, you can also pass `:ssl` and `:proxy`, which are passed to
70
+ the underlying [Faraday](https://github.com/lostisland/faraday) connection.
71
+
72
+ You can use this to, for instance, disable SSL verification:
73
+
74
+ ```ruby
75
+ FmRest::V1.build_connection(
76
+ host: "example.com",
77
+ ...
78
+ ssl: { verify: false }
79
+ )
80
+ ```
81
+
82
+ You can use the `:log` option for basic request logging, see the section on
83
+ [Logging](#Logging) below.
84
+
85
+ ### Default connection settings
86
+
87
+ If you're only connecting to a single FM database you can configure it globally
88
+ through `FmRest.default_connection_settings=`. E.g.:
89
+
90
+ ```ruby
91
+ FmRest.default_connection_settings = {
92
+ host: "example.com",
93
+ database: "database name",
94
+ username: "username",
95
+ password: "password"
96
+ }
97
+ ```
98
+
99
+ This configuration will be used by default by `FmRest::V1.build_connection` as
100
+ well as your models whenever you don't pass a configuration hash explicitly.
101
+
66
102
  ## Session token store
67
103
 
68
104
  By default fmrest-ruby will use a memory-based store for the session tokens.
@@ -556,11 +592,11 @@ for you automatically by Spyke (see [their
556
592
  README](https://github.com/balvig/spyke#log-output)).
557
593
 
558
594
  You can also enable simple STDOUT logging (useful for debugging) by passing
559
- `log: true` in the options hash for either `FmRest.config=` or your models'
560
- `fmrest_config=`, e.g.:
595
+ `log: true` in the options hash for either
596
+ `FmRest.default_connection_settings=` or your models' `fmrest_config=`, e.g.:
561
597
 
562
598
  ```ruby
563
- FmRest.config = {
599
+ FmRest.default_connection_settings = {
564
600
  host: "example.com",
565
601
  database: "My Database",
566
602
  username: "z3r0c00l",
@@ -580,8 +616,6 @@ class LoggyBee < FmRest::Spyke::Base
580
616
  end
581
617
  ```
582
618
 
583
- Note that the log option set in `FmRest.config` is ignored by models.
584
-
585
619
  If you need to set up more complex logging for your models can use the
586
620
  `faraday` block inside your class to inject your own logger middleware into the
587
621
  Faraday connection, e.g.:
@@ -10,6 +10,20 @@ module FmRest
10
10
  class << self
11
11
  attr_accessor :token_store
12
12
 
13
- attr_accessor :config
13
+ attr_writer :default_connection_settings
14
+
15
+ def default_connection_settings
16
+ @default_connection_settings || {}
17
+ end
18
+
19
+ def config=(connection_hash)
20
+ warn "[DEPRECATION] `FmRest.config=` is deprecated, use `FmRest.default_connection_settings=` instead"
21
+ self.default_connection_settings = connection_hash
22
+ end
23
+
24
+ def config
25
+ warn "[DEPRECATION] `FmRest.config` is deprecated, use `FmRest.default_connection_settings` instead"
26
+ default_connection_settings
27
+ end
14
28
  end
15
29
  end
@@ -21,7 +21,7 @@ module FmRest
21
21
 
22
22
  # @return (see FmRest::V1::ContainerFields#fetch_container_data)
23
23
  def download
24
- FmRest::V1.fetch_container_data(url)
24
+ FmRest::V1.fetch_container_data(url, @base.class.connection)
25
25
  end
26
26
 
27
27
  # @param filename_or_io [String, IO] a path to the file to upload or an
@@ -38,7 +38,7 @@ module FmRest
38
38
  private
39
39
 
40
40
  def fmrest_connection
41
- @fmrest_connection ||= FmRest::V1.build_connection(fmrest_config || FmRest.config) do |conn|
41
+ @fmrest_connection ||= FmRest::V1.build_connection(fmrest_config || FmRest.default_connection_settings) do |conn|
42
42
  faraday_block.call(conn) if faraday_block
43
43
 
44
44
  # Pass the class to JsonParser's initializer so it can have
@@ -9,7 +9,14 @@ module FmRest
9
9
  module Connection
10
10
  BASE_PATH = "/fmi/data/v1/databases".freeze
11
11
 
12
- def build_connection(options = FmRest.config, &block)
12
+ # Builds a complete DAPI Faraday connection with middleware already
13
+ # configured to handle authentication, JSON parsing, logging and DAPI
14
+ # error handling. A block can be optionally given for additional
15
+ # middleware configuration
16
+ #
17
+ # @option (see #base_connection)
18
+ # @return (see #base_connection)
19
+ def build_connection(options = FmRest.default_connection_settings, &block)
13
20
  base_connection(options) do |conn|
14
21
  conn.use RaiseErrors
15
22
  conn.use TokenSession, options
@@ -37,7 +44,19 @@ module FmRest
37
44
  end
38
45
  end
39
46
 
40
- def base_connection(options = FmRest.config, &block)
47
+ # Builds a base Faraday connection with base URL constructed from
48
+ # connection options and passes it the given block
49
+ #
50
+ # @option options [String] :host The hostname for the FM server
51
+ # @option options [String] :database The FM database name
52
+ # @option options [String] :username The username for DAPI authentication
53
+ # @option options [String] :password The password for DAPI authentication
54
+ # @option options [String] :ssl SSL options to forward to the Faraday
55
+ # connection
56
+ # @option options [String] :proxy Proxy options to forward to the Faraday
57
+ # connection
58
+ # @return [Faraday] The new Faraday connection
59
+ def base_connection(options = FmRest.default_connection_settings, &block)
41
60
  host = options.fetch(:host)
42
61
 
43
62
  # Default to HTTPS
@@ -50,7 +69,15 @@ module FmRest
50
69
  scheme = uri.scheme
51
70
  end
52
71
 
53
- Faraday.new("#{scheme}://#{host}#{BASE_PATH}/#{URI.escape(options.fetch(:database))}/".freeze, &block)
72
+ faraday_options = {}
73
+ faraday_options[:ssl] = options[:ssl] if options.key?(:ssl)
74
+ faraday_options[:proxy] = options[:proxy] if options.key?(:proxy)
75
+
76
+ Faraday.new(
77
+ "#{scheme}://#{host}#{BASE_PATH}/#{URI.escape(options.fetch(:database))}/".freeze,
78
+ faraday_options,
79
+ &block
80
+ )
54
81
  end
55
82
  end
56
83
  end
@@ -9,13 +9,20 @@ module FmRest
9
9
  # object with its body content (see Ruby's OpenURI for how the IO object
10
10
  # is extended with useful HTTP response information).
11
11
  #
12
- # This method uses Net::HTTP and OpenURI instead of Faraday.
12
+ # This method uses OpenURI instead of Faraday for fetching the actual
13
+ # container file.
13
14
  #
14
15
  # @raise [FmRest::ContainerFieldError] if any step fails
15
- # @param container_field_url [String] the URL to the container to
16
+ # @param container_field_url [String] The URL to the container to
16
17
  # download
17
- # @return [IO] the contents of the container
18
- def fetch_container_data(container_field_url)
18
+ # @param base_connection [Faraday::Connection] An optional Faraday
19
+ # connection to use as base settings for the container requests, useful
20
+ # if you need to set SSL or proxy settings. If given, this connection
21
+ # will not be used directly, but rather a new one with copied SSL and
22
+ # proxy options. If omitted, `FmRest.default_connection_settings`'s
23
+ # `:ssl` and `:proxy` options will be used instead (if available)
24
+ # @return [IO] The contents of the container
25
+ def fetch_container_data(container_field_url, base_connection = nil)
19
26
  require "open-uri"
20
27
 
21
28
  begin
@@ -29,25 +36,29 @@ module FmRest
29
36
  raise FmRest::ContainerFieldError, "Container URL is not HTTP (#{container_field_url})"
30
37
  end
31
38
 
32
- require "net/http"
39
+ conn =
40
+ Faraday.new(nil,
41
+ ssl: base_connection ? base_connection.ssl.to_hash : FmRest.default_connection_settings[:ssl],
42
+ proxy: base_connection ? base_connection.proxy.to_hash : FmRest.default_connection_settings[:proxy]
43
+ )
33
44
 
34
45
  # Requesting the container URL with no cookie set will respond with a
35
46
  # redirect and a session cookie
36
- cookie_response = ::Net::HTTP.get_response(url)
47
+ cookie_response = conn.get url
37
48
 
38
- unless cookie = cookie_response["Set-Cookie"]
49
+ unless cookie = cookie_response.headers["Set-Cookie"]
39
50
  raise FmRest::ContainerFieldError, "Container field's initial request didn't return a session cookie, the URL may be stale (try downloading it again immediately after retrieving the record)"
40
51
  end
41
52
 
42
53
  # Now request the URL again with the proper session cookie using
43
54
  # OpenURI, which wraps the response in an IO object which also responds
44
55
  # to #content_type
45
- url.open("Cookie" => cookie)
56
+ url.open(faraday_connection_to_openuri_options(conn).merge("Cookie" => cookie))
46
57
  end
47
58
 
48
59
  # Handles the core logic of uploading a file into a container field
49
60
  #
50
- # @param connection [Faraday] the Faraday connection to use
61
+ # @param connection [Faraday::Connection] the Faraday connection to use
51
62
  # @param container_path [String] the path to the container
52
63
  # @param filename_or_io [String, IO] a path to the file to upload or an
53
64
  # IO object
@@ -68,6 +79,33 @@ module FmRest
68
79
  request.body = { upload: Faraday::UploadIO.new(filename_or_io, content_type, filename) }
69
80
  end
70
81
  end
82
+
83
+ private
84
+
85
+ # Copies a Faraday::Connection's relevant options to
86
+ # OpenURI::OpenRead#open format
87
+ #
88
+ def faraday_connection_to_openuri_options(conn)
89
+ openuri_opts = {}
90
+
91
+ if !conn.ssl.empty?
92
+ openuri_opts[:ssl_verify_mode] =
93
+ conn.ssl.fetch(:verify, true) ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
94
+
95
+ openuri_opts[:ssl_ca_cert] = conn.ssl.cert_store if conn.ssl.cert_store
96
+ end
97
+
98
+ if conn.proxy && !conn.proxy.empty?
99
+ if conn.proxy.user && conn.proxy.password
100
+ openuri_opts[:proxy_http_basic_authentication] =
101
+ [conn.proxy.uri.tap { |u| u.userinfo = ""}, conn.proxy.user, conn.proxy.password]
102
+ else
103
+ openuri_opts[:proxy] = conn.proxy.uri
104
+ end
105
+ end
106
+
107
+ openuri_opts
108
+ end
71
109
  end
72
110
  end
73
111
  end
@@ -8,7 +8,7 @@ module FmRest
8
8
  HEADER_KEY = "Authorization".freeze
9
9
  TOKEN_STORE_INTERFACE = [:load, :store, :delete].freeze
10
10
 
11
- def initialize(app, options = FmRest.config)
11
+ def initialize(app, options = FmRest.default_connection_settings)
12
12
  super(app)
13
13
  @options = options
14
14
  end
@@ -71,7 +71,7 @@ module FmRest
71
71
  begin
72
72
  # Strip the host part to just the hostname (i.e. no scheme or port)
73
73
  host = @options.fetch(:host)
74
- host = URI(host).hostname if host.match?(/\Ahttps?:\/\//)
74
+ host = URI(host).hostname if host =~ /\Ahttps?:\/\//
75
75
  "#{host}:#{@options.fetch(:database)}"
76
76
  end
77
77
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FmRest
4
- VERSION = "0.2.3"
4
+ VERSION = "0.2.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fmrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Carbajal
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-12 00:00:00.000000000 Z
11
+ date: 2019-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -188,6 +188,7 @@ files:
188
188
  - ".rspec"
189
189
  - ".travis.yml"
190
190
  - ".yardopts"
191
+ - CHANGELOG.md
191
192
  - CODE_OF_CONDUCT.md
192
193
  - Gemfile
193
194
  - LICENSE.txt