fantasticstay_api 0.1.0 → 0.1.1

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: bdecfb00c6c09a2e935ade7f958df01b634928f4c6bb46baa6d6e0992c01ffaa
4
+ data.tar.gz: ac032f6927c1b9769224c88008b10c6442c00b0e685b343081e96fd7b651c1f1
5
5
  SHA512:
6
- metadata.gz: ee9371e6284a6ec29a4275ee1a28fe430e9604125e3d2ad9c29ff3946d27290e936830ff8cb875dd285ee02ff7fedcde8576012b01b2b84c01fd93a25af52e5b
7
- data.tar.gz: '09de71f00f8d730c8eb8416703e10f8c4d028123ce150fa7fa9ae860e2be9a5d55fc215cec2a3feb381b524f5e4b03e9b073c56aff9cc555c5971875e4fc6ee5'
6
+ metadata.gz: 75c5256ada0d0a1f1b3098ce474820fa6e67a5ca5d997fbab9682be27e745903ea2879dc97797bd940c3f5601c926fe672f053b6adb4b00e8e07bc8657287973
7
+ data.tar.gz: 47658f8667df190a20bcb052a6bb2de69fc3b0ad90fb69c08777a6f47e2fa1e906bb5431ef349cb81a2ebf57d856bb6b523ef5d7bb916eff74030b2fd2d4d4b0
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,80 @@ 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 = nil, 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,
54
+ filters: filters.to_json,
40
55
  sort: sort
41
- }
56
+ }.merge!(global_params),
57
+ cache_ttl: 3600*24
42
58
  )
43
- process_response(response, 'reservations')
59
+ process_response(response)
44
60
  end
45
61
 
46
- def reservation(reservation_id, filters = {}, sort = {})
62
+ def reservation(reservation_id, global_params = {})
47
63
  response = request(
48
64
  http_method: :get,
49
- endpoint: sprintf('reservations/%d', reservation_id)
65
+ endpoint: "reservations/#{reservation_id}",
66
+ params: global_params
50
67
  )
51
- process_single(response, 'reservation')
68
+ process_response(response)
52
69
  end
53
70
 
54
- def guest(guest_id, filters = {}, sort = {})
71
+ def guest(guest_id, global_params = {})
55
72
  response = request(
56
73
  http_method: :get,
57
- endpoint: sprintf('guests/%d', guest_id)
74
+ endpoint: "guests/#{guest_id}",
75
+ params: global_params,
76
+ cache_ttl: 3600*24
58
77
  )
59
- process_single(response, 'guest')
78
+ process_response(response)
60
79
  end
61
80
 
62
- def integrations
81
+ def integrations(global_params = {})
63
82
  response = request(
64
83
  http_method: :get,
65
- endpoint: 'integrations'
84
+ endpoint: 'integrations',
85
+ params: global_params
66
86
  )
67
- process_response(response, 'integrations')
87
+ process_response(response)
68
88
  end
69
89
 
70
90
  protected
71
91
 
72
- def process_response(response, model = 'results')
73
- results = response[model]
74
- results.each do |r|
75
- r.transform_keys!(&:to_sym)
92
+ def process_response(response)
93
+ result = response
94
+ case result
95
+ when Hash
96
+ result.transform_keys!(&:to_sym)
97
+ result.values.each do |r|
98
+ process_response(r)
99
+ end
100
+ when Array
101
+ result.each do |r|
102
+ process_response(r)
103
+ end
76
104
  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 }
105
+ result
85
106
  end
86
107
  end
87
108
  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.1'
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dinis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-11 00:00:00.000000000 Z
11
+ date: 2022-09-14 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