fantasticstay_api 0.1.2 → 0.1.5

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: f0a0159628ff5d6e47b19ff0cbcc1f206809c18bf01a85581c80ce448cc2d8cd
4
- data.tar.gz: fc4aa632878fad907ddada639f09339d16d90db906c1bd4a2d1bb34a68cab3ac
3
+ metadata.gz: 9a727acfeab9f3a592ea54d2fe8622a2b116b47868a930bcda001d4d029487bc
4
+ data.tar.gz: afb8961fb5454320a021b6f5891aa1990de185ec1a7b79d2592d7d69f6cdfafd
5
5
  SHA512:
6
- metadata.gz: 66c7a86db2f4e5bad774620f8c7f0eaa00af1e63ffffe357a3df59165507bf9e43446252b8a632c5a56da904c9be80a6cc40236919ca6b130d00f49fecd94f8f
7
- data.tar.gz: ffdd16db301ead53334a991d423cf72714f6c19568d916b607e2304886ed785f76697adc459826fa79676159b0c5cb3ec4c807ba1f5367f0de3d196f75b4b9df
6
+ metadata.gz: 7068b3721b8d27d6e74c1b454c39579014d12cf2c57b28c67210eb70b29f6300dfb9b495683a51923922a4b559a5aadeca307961bd786bbf51956f54648cfe55
7
+ data.tar.gz: 5ea6380129337d84b23a29dadcab7c5be2ef565eb27019c5b65776bc37ffc2467b820564e708374f770f22ce8ffd17a697ab1dbccbf3083992e34f53b9bb4023
data/CHANGELOG.md CHANGED
@@ -0,0 +1 @@
1
+ * 0.1.5 Upgrade to faraday 2.6.0
data/Dockerfile CHANGED
@@ -2,7 +2,11 @@ FROM ruby:3.0
2
2
 
3
3
  WORKDIR /usr/src/app
4
4
 
5
- COPY . .
5
+ COPY Gemfile .
6
+ COPY Gemfile.lock .
7
+ COPY fantasticstay_api.gemspec .
8
+ COPY lib/fantasticstay_api/version.rb lib/fantasticstay_api/version.rb
6
9
 
7
10
  RUN bundle install
8
11
 
12
+ COPY . .
data/Gemfile CHANGED
@@ -5,8 +5,6 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in fantasticstay_api.gemspec
6
6
  gemspec
7
7
 
8
- gem 'dotenv', '~> 2.7'
9
-
10
8
  group :test do
11
9
  gem 'rake', '~> 13.0'
12
10
  gem 'rspec', '~> 3.0'
data/Gemfile.lock CHANGED
@@ -1,51 +1,49 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fantasticstay_api (0.1.0)
4
+ fantasticstay_api (0.1.3)
5
5
  api_cache (~> 0.3.0)
6
- faraday (~> 1.3.0)
6
+ faraday (~> 2.6.0)
7
7
  oj (~> 3.11)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- addressable (2.7.0)
13
- public_suffix (>= 2.0.2, < 5.0)
12
+ addressable (2.8.1)
13
+ public_suffix (>= 2.0.2, < 6.0)
14
14
  api_cache (0.3.0)
15
15
  ast (2.4.2)
16
16
  crack (0.4.5)
17
17
  rexml
18
- diff-lcs (1.4.4)
19
- dotenv (2.7.6)
20
- faraday (1.3.0)
21
- faraday-net_http (~> 1.0)
22
- multipart-post (>= 1.2, < 3)
23
- ruby2_keywords
24
- faraday-net_http (1.0.1)
18
+ diff-lcs (1.5.0)
19
+ dotenv (2.8.1)
20
+ faraday (2.6.0)
21
+ faraday-net_http (>= 2.0, < 3.1)
22
+ ruby2_keywords (>= 0.0.4)
23
+ faraday-net_http (3.0.1)
25
24
  hashdiff (1.0.1)
26
- multipart-post (2.1.1)
27
- oj (3.11.3)
28
- parallel (1.20.1)
29
- parser (3.0.0.0)
25
+ oj (3.13.21)
26
+ parallel (1.22.1)
27
+ parser (3.1.2.1)
30
28
  ast (~> 2.4.1)
31
- public_suffix (4.0.6)
32
- rainbow (3.0.0)
33
- rake (13.0.3)
34
- regexp_parser (2.1.1)
35
- rexml (3.2.4)
36
- rspec (3.10.0)
37
- rspec-core (~> 3.10.0)
38
- rspec-expectations (~> 3.10.0)
39
- rspec-mocks (~> 3.10.0)
40
- rspec-core (3.10.1)
41
- rspec-support (~> 3.10.0)
42
- rspec-expectations (3.10.1)
29
+ public_suffix (5.0.0)
30
+ rainbow (3.1.1)
31
+ rake (13.0.6)
32
+ regexp_parser (2.6.0)
33
+ rexml (3.2.5)
34
+ rspec (3.12.0)
35
+ rspec-core (~> 3.12.0)
36
+ rspec-expectations (~> 3.12.0)
37
+ rspec-mocks (~> 3.12.0)
38
+ rspec-core (3.12.0)
39
+ rspec-support (~> 3.12.0)
40
+ rspec-expectations (3.12.0)
43
41
  diff-lcs (>= 1.2.0, < 2.0)
44
- rspec-support (~> 3.10.0)
45
- rspec-mocks (3.10.2)
42
+ rspec-support (~> 3.12.0)
43
+ rspec-mocks (3.12.0)
46
44
  diff-lcs (>= 1.2.0, < 2.0)
47
- rspec-support (~> 3.10.0)
48
- rspec-support (3.10.2)
45
+ rspec-support (~> 3.12.0)
46
+ rspec-support (3.12.0)
49
47
  rubocop (0.93.1)
50
48
  parallel (~> 1.10)
51
49
  parser (>= 2.7.1.5)
@@ -55,14 +53,14 @@ GEM
55
53
  rubocop-ast (>= 0.6.0)
56
54
  ruby-progressbar (~> 1.7)
57
55
  unicode-display_width (>= 1.4.0, < 2.0)
58
- rubocop-ast (1.4.1)
59
- parser (>= 2.7.1.5)
56
+ rubocop-ast (1.23.0)
57
+ parser (>= 3.1.1.0)
60
58
  ruby-progressbar (1.11.0)
61
- ruby2_keywords (0.0.4)
62
- unicode-display_width (1.7.0)
63
- vcr (6.0.0)
64
- webmock (3.12.2)
65
- addressable (>= 2.3.6)
59
+ ruby2_keywords (0.0.5)
60
+ unicode-display_width (1.8.0)
61
+ vcr (6.1.0)
62
+ webmock (3.18.1)
63
+ addressable (>= 2.8.0)
66
64
  crack (>= 0.3.2)
67
65
  hashdiff (>= 0.4.0, < 2.0.0)
68
66
 
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # FantasticstayApi
2
2
 
3
+ ![main workflow](https://github.com/dlage/fantasticstay_api_gem/actions/workflows/main.yml/badge.svg)
4
+
3
5
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fantasticstay_api`. To experiment with that code, run `bin/console` for an interactive prompt.
4
6
 
5
7
  TODO: Delete this and the text above, and describe your gem
data/Rakefile CHANGED
@@ -3,7 +3,9 @@
3
3
  require 'bundler/gem_tasks'
4
4
  require 'rspec/core/rake_task'
5
5
 
6
- RSpec::Core::RakeTask.new(:spec)
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ end
7
9
 
8
10
  require 'rubocop/rake_task'
9
11
 
data/bin/console CHANGED
@@ -2,7 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'bundler/setup'
5
- require 'dotenv/load'
6
5
  require 'fantasticstay_api'
7
6
 
8
7
  # You can add fixtures and/or initialization code here to make experimenting
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.license = 'MIT'
15
15
  spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
16
16
 
17
- spec.metadata['allowed_push_host'] = "https://rubygems.org"
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
18
 
19
19
  spec.metadata['homepage_uri'] = spec.homepage
20
20
  spec.metadata['source_code_uri'] = 'https://github.com/dlage/fantasticstay_api_gem'
@@ -31,11 +31,11 @@ Gem::Specification.new do |spec|
31
31
 
32
32
  # Uncomment to register a new dependency of your gem
33
33
  # spec.add_dependency "example-gem", "~> 1.0"
34
- spec.add_dependency 'faraday', '~> 1.3.0'
35
- spec.add_dependency 'oj', '~> 3.11'
36
34
  spec.add_dependency 'api_cache', '~> 0.3.0'
35
+ spec.add_dependency 'faraday', '~> 2.6.0'
36
+ spec.add_dependency 'oj', '~> 3.11'
37
37
 
38
- # spec.add_dependency 'dry-configurable', '~> 0.12.1'
38
+ spec.add_dependency 'dry-configurable', '~> 0.13'
39
39
 
40
40
  # For more information and examples about making a new gem, checkout our
41
41
  # guide at: https://bundler.io/guides/creating_gem.html
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'api_exceptions'
4
- require_relative 'configuration'
5
4
  require_relative 'constants'
6
5
  require_relative 'http_status_codes'
6
+ require 'dry-configurable'
7
7
  require 'api_cache'
8
8
  require 'digest/bubblebabble'
9
9
 
@@ -13,22 +13,8 @@ module FantasticstayApi
13
13
  include ApiExceptions
14
14
  include Constants
15
15
  include HttpStatusCodes
16
+ include Dry::Configurable
16
17
 
17
- attr_reader(*FantasticstayApi.configuration.property_names, :token, :endpoint)
18
-
19
- attr_accessor :current_options
20
-
21
- # Callback to update current configuration options
22
- class_eval do
23
- FantasticstayApi.configuration.property_names.each do |key|
24
- define_method "#{key}=" do |arg|
25
- instance_variable_set("@#{key}", arg)
26
- current_options.merge!({ "#{key}": arg })
27
- end
28
- end
29
- end
30
-
31
- API_ENDPOINT = 'https://api.fsapp.io'
32
18
  HTTP_STATUS_MAPPING = {
33
19
  HTTP_BAD_REQUEST_CODE => BadRequestError,
34
20
  HTTP_UNAUTHORIZED_CODE => UnauthorizedError,
@@ -37,19 +23,38 @@ module FantasticstayApi
37
23
  HTTP_UNPROCESSABLE_ENTITY_CODE => UnprocessableEntityError,
38
24
  'default' => ApiError
39
25
  }.freeze
26
+ API_ENDPOINT = 'https://api.fsapp.io'
27
+ API_TOKEN = 'TESTING'
28
+
29
+ setting :follow_redirects, default: true
30
+
31
+ # The api endpoint used to connect to FantasticstayApi if none is set
32
+ setting :endpoint, default: ENV['FANTASTICSTAY_API_ENDPOINT'] || API_ENDPOINT, reader: true
33
+
34
+ # The token included in request header 'x-api-key'
35
+ setting :token, default: ENV['FANTASTICSTAY_API_TOKEN'] || API_TOKEN, reader: true
36
+
37
+ # The value sent in the http header for 'User-Agent' if none is set
38
+ setting :user_agent, default: "FantasticstayApi API Ruby Gem #{FantasticstayApi::VERSION}"
39
+
40
+ # By default uses the Faraday connection options if none is set
41
+ setting :connection_options, default: {}
42
+
43
+ # By default display 30 resources
44
+ setting :per_page, default: 10
45
+
46
+ # Add Faraday::RackBuilder to overwrite middleware
47
+ setting :stack
40
48
 
41
49
  # Create new API
42
50
  #
43
51
  # @api public
44
52
  def initialize(options = {}, &block)
45
- opts = FantasticstayApi.configuration.fetch.merge(options)
46
- @current_options = opts
47
-
48
- FantasticstayApi.configuration.property_names.each do |key|
49
- send("#{key}=", opts[key])
53
+ configure do |c|
54
+ options.each_key do |key|
55
+ c.send("#{key}=", options[key])
56
+ end
50
57
  end
51
- @api_token = opts[:token] || ENV['FANTASTICSTAY_API_TOKEN']
52
- @api_endpoint = opts[:endpoint] || ENV['FANTASTICSTAY_API_ENDPOINT'] || API_ENDPOINT
53
58
 
54
59
  yield_or_eval(&block) if block_given?
55
60
  end
@@ -66,25 +71,28 @@ module FantasticstayApi
66
71
  private
67
72
 
68
73
  def client
69
- # provide your own logger
70
- logger = Logger.new $stderr
71
- logger.level = Logger::DEBUG
72
- @client ||= Faraday.new(@api_endpoint) do |client|
74
+ @client ||= Faraday.new(config.endpoint) do |client|
73
75
  client.request :url_encoded
74
76
  client.adapter Faraday.default_adapter
75
77
  client.headers['Content-Type'] = 'application/json'
76
- client.headers['x-api-key'] = @api_token
77
- client.response :logger, logger
78
+ client.headers['x-api-key'] = config.token
79
+ client.headers['User-Agent'] = config.user_agent
78
80
  end
79
81
  end
80
82
 
81
83
  def request(http_method:, endpoint:, params: {}, cache_ttl: 3600)
82
- response = APICache.get(Digest::SHA256.bubblebabble(@api_token) + http_method.to_s + endpoint + params.to_s, cache: cache_ttl) do
83
- client.public_send(http_method, endpoint, params)
84
+ response = APICache.get(
85
+ Digest::SHA256.bubblebabble(config.token) + http_method.to_s + endpoint + params.to_s,
86
+ cache: cache_ttl
87
+ ) do
88
+ client.public_send(http_method, endpoint, params)
84
89
  end
85
- parsed_response = Oj.load(response.body)
86
90
 
87
- return parsed_response if response_successful?(response)
91
+ process_http_response(response)
92
+ end
93
+
94
+ def process_http_response(response)
95
+ return Oj.load(response.body) if response_successful?(response)
88
96
 
89
97
  raise error_class(response), "Code: #{response.status}, response: #{response.body}"
90
98
  end
@@ -100,24 +108,5 @@ module FantasticstayApi
100
108
  def response_successful?(response)
101
109
  response.status == HTTP_OK_CODE
102
110
  end
103
-
104
- # Responds to attribute query or attribute clear
105
- #
106
- # @api private
107
- def method_missing(method_name, *args, &block)
108
- # :nodoc:
109
- case method_name.to_s
110
- when /^(.*)\?$/
111
- !!send(Regexp.last_match(1).to_s)
112
- when /^clear_(.*)$/
113
- send("#{Regexp.last_match(1)}=", nil)
114
- else
115
- super
116
- end
117
- end
118
-
119
- def respond_to_missing?(method_name, include_private = false)
120
- method_name.to_s.start_with?('clear_') || super
121
- end
122
111
  end
123
112
  end
@@ -26,10 +26,7 @@ module FantasticstayApi
26
26
  http_method: :get,
27
27
  endpoint: 'calendar',
28
28
  params: {
29
- listing_id: listing_id,
30
- start_date: start_date,
31
- end_date: end_date,
32
- filters: filters.to_json
29
+ listing_id: listing_id, start_date: start_date, end_date: end_date, filters: filters.to_json
33
30
  }.merge(global_params)
34
31
  )
35
32
  process_response(response)
@@ -50,10 +47,7 @@ module FantasticstayApi
50
47
  http_method: :get,
51
48
  endpoint: 'reservations',
52
49
  params: {
53
- listing_id: listing_id,
54
- filters: filters.to_json,
55
- sort: sort[:order],
56
- direction: sort[:direction]
50
+ listing_id: listing_id, filters: filters.to_json, sort: sort[:order], direction: sort[:direction]
57
51
  }.merge!(global_params),
58
52
  cache_ttl: 3600 * 24
59
53
  )
@@ -95,13 +89,9 @@ module FantasticstayApi
95
89
  case result
96
90
  when Hash
97
91
  result.transform_keys!(&:to_sym)
98
- result.values.each do |r|
99
- process_response(r)
100
- end
92
+ result.each_value { |r| process_response(r) }
101
93
  when Array
102
- result.each do |r|
103
- process_response(r)
104
- end
94
+ result.each { |r| process_response(r) }
105
95
  end
106
96
  result
107
97
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FantasticstayApi
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.5'
5
5
  end
@@ -4,8 +4,8 @@ require 'faraday'
4
4
  require 'oj'
5
5
  require_relative 'fantasticstay_api/version'
6
6
 
7
+ # Base module for Fantasticstay API
7
8
  module FantasticstayApi
8
- # Base module for Fantasticstay API
9
9
  class Error < StandardError; end
10
10
 
11
11
  LIBNAME = 'fantasticstay_api'
@@ -13,35 +13,6 @@ module FantasticstayApi
13
13
  LIBDIR = File.expand_path(LIBNAME.to_s, __dir__)
14
14
 
15
15
  class << self
16
- # The client configuration
17
- #
18
- # @return [Configuration]
19
- #
20
- # @api public
21
- def configuration
22
- @configuration ||= Configuration.new
23
- end
24
-
25
- alias config configuration
26
-
27
- # Configure options
28
- #
29
- # @example
30
- # FantasticstayApi.configure do |c|
31
- # c.some_option = true
32
- # end
33
- #
34
- # @yield the configuration block
35
- # @yieldparam configuration [FantasticstayApi::Configuration]
36
- # the configuration instance
37
- #
38
- # @return [nil]
39
- #
40
- # @api public
41
- def configure
42
- yield configuration
43
- end
44
-
45
16
  # Alias for FantasticstayApi::Client.new
46
17
  #
47
18
  # @param [Hash] options
@@ -70,20 +41,15 @@ module FantasticstayApi
70
41
  def method_missing(method_name, *args, &block)
71
42
  if new.respond_to?(method_name)
72
43
  new.send(method_name, *args, &block)
73
- elsif configuration.respond_to?(method_name)
74
- FantasticstayApi.configuration.send(method_name, *args, &block)
75
44
  else
76
45
  super.respond_to_missing?
77
46
  end
78
47
  end
79
48
 
80
49
  def respond_to_missing?(method_name, include_private = false)
81
- new.respond_to?(method_name, include_private) ||
82
- configuration.respond_to?(method_name) ||
83
- super(method_name, include_private)
50
+ new.respond_to?(method_name, include_private) || super(method_name, include_private)
84
51
  end
85
52
  end
86
53
  end
87
54
 
88
55
  require_relative 'fantasticstay_api/client'
89
- require_relative 'fantasticstay_api/configuration'
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fantasticstay_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dinis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-26 00:00:00.000000000 Z
11
+ date: 2022-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: api_cache
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: faraday
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: 1.3.0
33
+ version: 2.6.0
20
34
  type: :runtime
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: 1.3.0
40
+ version: 2.6.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: oj
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -39,19 +53,19 @@ dependencies:
39
53
  - !ruby/object:Gem::Version
40
54
  version: '3.11'
41
55
  - !ruby/object:Gem::Dependency
42
- name: api_cache
56
+ name: dry-configurable
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - "~>"
46
60
  - !ruby/object:Gem::Version
47
- version: 0.3.0
61
+ version: '0.13'
48
62
  type: :runtime
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: 0.3.0
68
+ version: '0.13'
55
69
  description: A gem that implements functions from the FS API available for its users.
56
70
  email:
57
71
  - dinis@lage.pw
@@ -77,12 +91,8 @@ files:
77
91
  - fantasticstay_api.gemspec
78
92
  - lib/fantasticstay_api.rb
79
93
  - lib/fantasticstay_api/api.rb
80
- - lib/fantasticstay_api/api/config.rb
81
- - lib/fantasticstay_api/api/config/property.rb
82
- - lib/fantasticstay_api/api/config/property_set.rb
83
94
  - lib/fantasticstay_api/api_exceptions.rb
84
95
  - lib/fantasticstay_api/client.rb
85
- - lib/fantasticstay_api/configuration.rb
86
96
  - lib/fantasticstay_api/constants.rb
87
97
  - lib/fantasticstay_api/http_status_codes.rb
88
98
  - lib/fantasticstay_api/version.rb
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module FantasticstayApi
4
- class API
5
- class Config
6
- # Property objects provide an interface for configuration options
7
- class Property
8
- attr_reader :name, :default, :required
9
-
10
- def initialize(name, options)
11
- @name = name
12
- @default = options.fetch(:default, nil)
13
- @required = options.fetch(:required, nil)
14
- @options = options
15
- end
16
-
17
- # @api private
18
- def define_accessor_methods(properties)
19
- properties.define_reader_method(self, name, :public)
20
- properties.define_writer_method(self, "#{name}=", :public)
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,119 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'set'
4
-
5
- module FantasticstayApi
6
- class API
7
- class Config
8
- # Class responsible for storing configuration properties
9
- class PropertySet
10
- include Enumerable
11
-
12
- attr_reader :parent, :properties
13
-
14
- # Initialize an PropertySet
15
- #
16
- # @param [Object] parent
17
- # @param [Set] properties
18
- #
19
- # @return [undefined]
20
- #
21
- # @api private
22
- def initialize(parent = nil, properties = Set.new)
23
- @parent = parent
24
- @properties = properties
25
- @map = {}
26
- end
27
-
28
- # Iterate over properties
29
- #
30
- # @yield [property]
31
- #
32
- # @yieldparam [Property] property
33
- #
34
- # @return [self]
35
- #
36
- # @api public
37
- def each
38
- return to_enum unless block_given?
39
-
40
- @map.each { |name, property| yield property if name.is_a?(Symbol) }
41
- self
42
- end
43
-
44
- # Adds property to the set
45
- #
46
- # @example
47
- # properties_set << property
48
- #
49
- # @param [Property] property
50
- #
51
- # @return [self]
52
- #
53
- # @api public
54
- def <<(property)
55
- properties << property
56
- update_map(property.name, property.default)
57
- property.define_accessor_methods(self)
58
- self
59
- end
60
-
61
- # Access property by name
62
- #
63
- # @api public
64
- def [](name)
65
- @map[name]
66
- end
67
- alias fetch []
68
-
69
- # Set property value by name
70
- #
71
- # @api public
72
- def []=(name, property)
73
- update_map(name, property)
74
- end
75
-
76
- # Update map with index
77
- #
78
- # @api private
79
- def update_map(name, property)
80
- @map[name.to_sym] = @map[name.to_s.freeze] = property
81
- end
82
-
83
- # Convert properties to a hash of property names and
84
- # corresponding values
85
- #
86
- # @api public
87
- def to_hash
88
- properties.each_with_object({}) do |property, props|
89
- name = property.name
90
- props[name] = self[name]
91
- end
92
- end
93
-
94
- # Check if properties exist
95
- #
96
- # @api public
97
- def empty?
98
- @map.empty?
99
- end
100
-
101
- # @api private
102
- def define_reader_method(property, method_name, visibility)
103
- property_set = self
104
- parent.send(:define_method, method_name) { property_set[property.name] }
105
- parent.send(visibility, method_name)
106
- end
107
-
108
- # @api private
109
- def define_writer_method(property, method_name, visibility)
110
- property_set = self
111
- parent.send(:define_method, method_name) do |value|
112
- property_set[property.name] = value
113
- end
114
- parent.send(visibility, method_name)
115
- end
116
- end
117
- end
118
- end
119
- end
@@ -1,101 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'config/property'
4
- require_relative 'config/property_set'
5
-
6
- module FantasticstayApi
7
- class API
8
- # A base class for constructing api configuration
9
- class Config
10
- # Defines a property on an object's class or instance
11
- #
12
- # @example
13
- # class Configuration < Api::Config
14
- # property :adapter, default: :net_http
15
- # property :user, required: true
16
- # end
17
- #
18
- # @param [Symbol] name
19
- # the name of a property
20
- #
21
- # @param [#to_hash] options
22
- # the extra options
23
- #
24
- # @return [self]
25
- #
26
- # @api public
27
- def self.property(name, options = {})
28
- property_set << Property.new(name, options)
29
- update_subclasses(name, options)
30
- self
31
- end
32
-
33
- def self.update_subclasses(name, options)
34
- @subclasses.each { |klass| klass.property(name, options) } if defined?(@subclasses) && @subclasses
35
- end
36
-
37
- # Check if property is defined
38
- #
39
- # @param [Symbol] name
40
- # the name to check
41
- #
42
- # @return [Boolean]
43
- #
44
- # @api public
45
- def self.property?(name)
46
- property_set.include?(name)
47
- end
48
-
49
- class << self
50
- attr_reader :property_set
51
- end
52
-
53
- instance_variable_set('@property_set', PropertySet.new(self))
54
-
55
- def self.inherited(descendant)
56
- super
57
- (@subclasses ||= Set.new) << descendant
58
- descendant.instance_variable_set(
59
- '@property_set',
60
- PropertySet.new(descendant, property_set.properties.dup)
61
- )
62
- end
63
-
64
- def property_names
65
- self.class.property_set.properties.map(&:name)
66
- end
67
-
68
- def self.property_names
69
- property_set.properties.map(&:name)
70
- end
71
-
72
- # Fetch all the properties and their values
73
- #
74
- # @return [Hash[Symbol]]
75
- #
76
- # @api public
77
- def fetch(value = nil)
78
- if value
79
- self.class.property_set[value]
80
- else
81
- self.class.property_set.to_hash
82
- end
83
- end
84
-
85
- # Provide access to properties
86
- #
87
- # @example
88
- # config.call do |config|
89
- # config.adapter = :net_http
90
- # end
91
- #
92
- # @return [self]
93
- #
94
- # @api private
95
- def call(&block)
96
- block.call(self) if block_given?
97
- self
98
- end
99
- end
100
- end
101
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'api/config'
4
- require_relative 'version'
5
-
6
- module FantasticstayApi
7
- # Stores the configuration
8
- class Configuration < API::Config
9
- property :follow_redirects, default: true
10
-
11
- # The api endpoint used to connect to FantasticstayApi if none is set
12
- property :endpoint, default: 'https://api.fsapp.io/'
13
-
14
- # The value sent in the http header for 'User-Agent' if none is set
15
- property :user_agent, default: "FantasticstayApi API Ruby Gem #{FantasticstayApi::VERSION}"
16
-
17
- # By default uses the Faraday connection options if none is set
18
- property :connection_options, default: {}
19
-
20
- # By default display 30 resources
21
- property :per_page, default: 20
22
-
23
- # Add Faraday::RackBuilder to overwrite middleware
24
- property :stack
25
- end
26
- end