fantasticstay_api 0.1.0 → 0.1.2

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: d6d5c86f2b9a796698def2e4b8801e4801de74335ac43f5e9bc2939814105cba
4
- data.tar.gz: e9f2c55526493914d927c03c4830a8d46024fe38abe6b66a04a15418d5e532a7
3
+ metadata.gz: f0a0159628ff5d6e47b19ff0cbcc1f206809c18bf01a85581c80ce448cc2d8cd
4
+ data.tar.gz: fc4aa632878fad907ddada639f09339d16d90db906c1bd4a2d1bb34a68cab3ac
5
5
  SHA512:
6
- metadata.gz: ee9371e6284a6ec29a4275ee1a28fe430e9604125e3d2ad9c29ff3946d27290e936830ff8cb875dd285ee02ff7fedcde8576012b01b2b84c01fd93a25af52e5b
7
- data.tar.gz: '09de71f00f8d730c8eb8416703e10f8c4d028123ce150fa7fa9ae860e2be9a5d55fc215cec2a3feb381b524f5e4b03e9b073c56aff9cc555c5971875e4fc6ee5'
6
+ metadata.gz: 66c7a86db2f4e5bad774620f8c7f0eaa00af1e63ffffe357a3df59165507bf9e43446252b8a632c5a56da904c9be80a6cc40236919ca6b130d00f49fecd94f8f
7
+ data.tar.gz: ffdd16db301ead53334a991d423cf72714f6c19568d916b607e2304886ed785f76697adc459826fa79676159b0c5cb3ec4c807ba1f5367f0de3d196f75b4b9df
data/Gemfile.lock CHANGED
@@ -2,6 +2,7 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  fantasticstay_api (0.1.0)
5
+ api_cache (~> 0.3.0)
5
6
  faraday (~> 1.3.0)
6
7
  oj (~> 3.11)
7
8
 
@@ -10,6 +11,7 @@ GEM
10
11
  specs:
11
12
  addressable (2.7.0)
12
13
  public_suffix (>= 2.0.2, < 5.0)
14
+ api_cache (0.3.0)
13
15
  ast (2.4.2)
14
16
  crack (0.4.5)
15
17
  rexml
@@ -33,6 +33,8 @@ Gem::Specification.new do |spec|
33
33
  # spec.add_dependency "example-gem", "~> 1.0"
34
34
  spec.add_dependency 'faraday', '~> 1.3.0'
35
35
  spec.add_dependency 'oj', '~> 3.11'
36
+ spec.add_dependency 'api_cache', '~> 0.3.0'
37
+
36
38
  # spec.add_dependency 'dry-configurable', '~> 0.12.1'
37
39
 
38
40
  # For more information and examples about making a new gem, checkout our
@@ -4,6 +4,8 @@ require_relative 'api_exceptions'
4
4
  require_relative 'configuration'
5
5
  require_relative 'constants'
6
6
  require_relative 'http_status_codes'
7
+ require 'api_cache'
8
+ require 'digest/bubblebabble'
7
9
 
8
10
  module FantasticstayApi
9
11
  # Core class responsible for api interface operations
@@ -70,13 +72,16 @@ module FantasticstayApi
70
72
  @client ||= Faraday.new(@api_endpoint) do |client|
71
73
  client.request :url_encoded
72
74
  client.adapter Faraday.default_adapter
75
+ client.headers['Content-Type'] = 'application/json'
73
76
  client.headers['x-api-key'] = @api_token
74
77
  client.response :logger, logger
75
78
  end
76
79
  end
77
80
 
78
- def request(http_method:, endpoint:, params: {})
79
- response = client.public_send(http_method, endpoint, params)
81
+ 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
+ end
80
85
  parsed_response = Oj.load(response.body)
81
86
 
82
87
  return parsed_response if response_successful?(response)
@@ -1,21 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
3
4
  require 'logger'
4
5
  require_relative 'api'
5
6
 
6
7
  module FantasticstayApi
7
8
  # Main client class that implements communication with the API
9
+ # global_params:
10
+ # - include_related_objects: int 0-1 0
11
+ # - page: int positive 1
12
+ # - per_page: int positive 20
8
13
  class Client < API
9
- def listings
14
+ def listings(global_params = {})
10
15
  response = request(
11
16
  http_method: :get,
12
- endpoint: 'listings'
17
+ endpoint: 'listings',
18
+ params: global_params
13
19
  )
14
- process_response(response, 'listings')
20
+ process_response(response)
15
21
  end
16
22
 
17
23
  # FantasticstayApi::Client.new.calendar(38859, '2022-01-01', '2022-07-31')
18
- def calendar(listing_id, start_date = nil, end_date = nil, filters = {})
24
+ def calendar(listing_id, start_date = nil, end_date = nil, filters = {}, global_params = {})
19
25
  response = request(
20
26
  http_method: :get,
21
27
  endpoint: 'calendar',
@@ -23,65 +29,81 @@ module FantasticstayApi
23
29
  listing_id: listing_id,
24
30
  start_date: start_date,
25
31
  end_date: end_date,
26
- filters: filters
27
- }
32
+ filters: filters.to_json
33
+ }.merge(global_params)
28
34
  )
29
- process_response(response, 'calendar')
35
+ process_response(response)
36
+ end
37
+
38
+ def listing(listing_id, global_params = {})
39
+ response = request(
40
+ http_method: :get,
41
+ endpoint: "listings/#{listing_id}",
42
+ params: global_params
43
+ )
44
+ process_response(response)
30
45
  end
31
46
 
32
47
  # FantasticstayApi::Client.new.reservations(38859)
33
- def reservations(listing_id, filters = {}, sort = {})
48
+ def reservations(listing_id, filters = [], sort = { order: 'checkIn', direction: 'desc' }, global_params = {})
34
49
  response = request(
35
50
  http_method: :get,
36
51
  endpoint: 'reservations',
37
52
  params: {
38
53
  listing_id: listing_id,
39
- filters: filters,
40
- sort: sort
41
- }
54
+ filters: filters.to_json,
55
+ sort: sort[:order],
56
+ direction: sort[:direction]
57
+ }.merge!(global_params),
58
+ cache_ttl: 3600 * 24
42
59
  )
43
- process_response(response, 'reservations')
60
+ process_response(response)
44
61
  end
45
62
 
46
- def reservation(reservation_id, filters = {}, sort = {})
63
+ def reservation(reservation_id, global_params = {})
47
64
  response = request(
48
65
  http_method: :get,
49
- endpoint: sprintf('reservations/%d', reservation_id)
66
+ endpoint: "reservations/#{reservation_id}",
67
+ params: global_params
50
68
  )
51
- process_single(response, 'reservation')
69
+ process_response(response)
52
70
  end
53
71
 
54
- def guest(guest_id, filters = {}, sort = {})
72
+ def guest(guest_id, global_params = {})
55
73
  response = request(
56
74
  http_method: :get,
57
- endpoint: sprintf('guests/%d', guest_id)
75
+ endpoint: "guests/#{guest_id}",
76
+ params: global_params,
77
+ cache_ttl: 3600 * 24
58
78
  )
59
- process_single(response, 'guest')
79
+ process_response(response)
60
80
  end
61
81
 
62
- def integrations
82
+ def integrations(global_params = {})
63
83
  response = request(
64
84
  http_method: :get,
65
- endpoint: 'integrations'
85
+ endpoint: 'integrations',
86
+ params: global_params
66
87
  )
67
- process_response(response, 'integrations')
88
+ process_response(response)
68
89
  end
69
90
 
70
91
  protected
71
92
 
72
- def process_response(response, model = 'results')
73
- results = response[model]
74
- results.each do |r|
75
- r.transform_keys!(&:to_sym)
93
+ def process_response(response)
94
+ result = response
95
+ case result
96
+ when Hash
97
+ result.transform_keys!(&:to_sym)
98
+ result.values.each do |r|
99
+ process_response(r)
100
+ end
101
+ when Array
102
+ result.each do |r|
103
+ process_response(r)
104
+ end
76
105
  end
77
- total = response.key?('total') ? response['total'] : 1
78
- { model.to_sym => results, total: total }
79
- end
80
-
81
- def process_single(response, model = 'result')
82
- result = response[model]
83
- result.transform_keys!(&:to_sym)
84
- { model.to_sym => result }
106
+ result
85
107
  end
86
108
  end
87
109
  end
@@ -9,7 +9,7 @@ module FantasticstayApi
9
9
  property :follow_redirects, default: true
10
10
 
11
11
  # The api endpoint used to connect to FantasticstayApi if none is set
12
- property :endpoint, default: 'https://api.fsapp.io'
12
+ property :endpoint, default: 'https://api.fsapp.io/'
13
13
 
14
14
  # The value sent in the http header for 'User-Agent' if none is set
15
15
  property :user_agent, default: "FantasticstayApi API Ruby Gem #{FantasticstayApi::VERSION}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FantasticstayApi
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fantasticstay_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dinis
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-11 00:00:00.000000000 Z
11
+ date: 2022-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: api_cache
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.3.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.3.0
41
55
  description: A gem that implements functions from the FS API available for its users.
42
56
  email:
43
57
  - dinis@lage.pw
@@ -80,7 +94,7 @@ metadata:
80
94
  homepage_uri: https://github.com/dlage/fantasticstay_api_gem
81
95
  source_code_uri: https://github.com/dlage/fantasticstay_api_gem
82
96
  changelog_uri: https://github.com/dlage/fantasticstay_api_gem/blob/master/CHANGELOG.md
83
- post_install_message:
97
+ post_install_message:
84
98
  rdoc_options: []
85
99
  require_paths:
86
100
  - lib
@@ -95,8 +109,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0'
97
111
  requirements: []
98
- rubygems_version: 3.3.7
99
- signing_key:
112
+ rubygems_version: 3.2.33
113
+ signing_key:
100
114
  specification_version: 4
101
115
  summary: FantasticStay API Wrapper.
102
116
  test_files: []