discountnetwork 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.hound.yml +3 -0
- data/.rspec +1 -0
- data/.rubocop.yml +629 -0
- data/.sample.pryrc +4 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +405 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/rake +17 -0
- data/bin/rspec +17 -0
- data/bin/setup +6 -0
- data/discountnetwork.gemspec +28 -0
- data/lib/discountnetwork/account.rb +28 -0
- data/lib/discountnetwork/activation.rb +15 -0
- data/lib/discountnetwork/base.rb +20 -0
- data/lib/discountnetwork/booking.rb +23 -0
- data/lib/discountnetwork/client.rb +54 -0
- data/lib/discountnetwork/configuration.rb +17 -0
- data/lib/discountnetwork/destination.rb +9 -0
- data/lib/discountnetwork/password.rb +21 -0
- data/lib/discountnetwork/provider.rb +15 -0
- data/lib/discountnetwork/response.rb +19 -0
- data/lib/discountnetwork/result.rb +15 -0
- data/lib/discountnetwork/rspec.rb +5 -0
- data/lib/discountnetwork/search.rb +15 -0
- data/lib/discountnetwork/session.rb +9 -0
- data/lib/discountnetwork/supplementary.rb +15 -0
- data/lib/discountnetwork/testing/discountnetwork_api.rb +240 -0
- data/lib/discountnetwork/version.rb +3 -0
- data/lib/discountnetwork.rb +16 -0
- data/spec/discountnetwork/account_spec.rb +36 -0
- data/spec/discountnetwork/activation_spec.rb +46 -0
- data/spec/discountnetwork/booking_spec.rb +70 -0
- data/spec/discountnetwork/client_spec.rb +40 -0
- data/spec/discountnetwork/configuration_spec.rb +30 -0
- data/spec/discountnetwork/destination_spec.rb +16 -0
- data/spec/discountnetwork/password_spec.rb +41 -0
- data/spec/discountnetwork/provider_spec.rb +26 -0
- data/spec/discountnetwork/response_spec.rb +16 -0
- data/spec/discountnetwork/result_spec.rb +30 -0
- data/spec/discountnetwork/search_spec.rb +37 -0
- data/spec/discountnetwork/session_spec.rb +18 -0
- data/spec/discountnetwork/supplementary_spec.rb +40 -0
- data/spec/fixtures/booking.json +86 -0
- data/spec/fixtures/destinations.json +22 -0
- data/spec/fixtures/empty.json +0 -0
- data/spec/fixtures/ping.json +3 -0
- data/spec/fixtures/provider.json +16 -0
- data/spec/fixtures/providers.json +32 -0
- data/spec/fixtures/result.json +89 -0
- data/spec/fixtures/results.json +121 -0
- data/spec/fixtures/search.json +19 -0
- data/spec/fixtures/search_created.json +19 -0
- data/spec/fixtures/session_created.json +31 -0
- data/spec/fixtures/supplementaries.json +33 -0
- data/spec/fixtures/supplementary.json +21 -0
- data/spec/fixtures/user.json +31 -0
- data/spec/spec_helper.rb +12 -0
- metadata +189 -0
data/README.md
ADDED
@@ -0,0 +1,405 @@
|
|
1
|
+
# Discount Network
|
2
|
+
|
3
|
+
[](https://travis-ci.org/discountnetwork/discountnetwork-ruby)
|
5
|
+
[](https://codeclimate.com/github/discountnetwork/discountnetwork-ruby)
|
7
|
+
|
8
|
+
Ruby API client for Discount Network API
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem "discountnetwork", github: "discountnetwork/discountnetwork-ruby"
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
```sh
|
21
|
+
$ bundle
|
22
|
+
```
|
23
|
+
|
24
|
+
## Configure
|
25
|
+
|
26
|
+
Once you have your API keys from Discount Network, then you can add an initializer
|
27
|
+
to set up your API keys
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
DiscountNetwork.configure do |config|
|
31
|
+
config.api_key = "YOUR_API_KEY"
|
32
|
+
|
33
|
+
# Default configurations
|
34
|
+
# config.api_host = "https://api.discountnetwork.io/v1"
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
## Usages
|
39
|
+
|
40
|
+
### Session
|
41
|
+
|
42
|
+
Session API allow you to authenticate a subscriber, create a new session or
|
43
|
+
destroying an existing session.
|
44
|
+
|
45
|
+
#### Create a new session
|
46
|
+
|
47
|
+
Creating a new session will verify the subscriber and return the `subscriber`
|
48
|
+
object with subscription details.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
DiscountNetwork::Session.create(
|
52
|
+
name: "username", password: "secret_password"
|
53
|
+
)
|
54
|
+
```
|
55
|
+
|
56
|
+
### Account
|
57
|
+
|
58
|
+
#### Find user account
|
59
|
+
|
60
|
+
To retrieve the user account details using the Discount Network `account` API
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
#
|
64
|
+
# The `auth_token` is optional, if you don't pass any
|
65
|
+
# parameter then it will try to retrieve the subscriber
|
66
|
+
# using the `auth_token` configuration.
|
67
|
+
#
|
68
|
+
|
69
|
+
DiscountNetwork::Account.find(auth_token)
|
70
|
+
```
|
71
|
+
|
72
|
+
#### Update user account
|
73
|
+
|
74
|
+
Once the user is logged in and `auth_token` has been configured properly then
|
75
|
+
we can update the user details using the DiscountNetwork `account` API
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
DiscountNetwork::Account.update(subscriber_attributes)
|
79
|
+
```
|
80
|
+
|
81
|
+
### Activation
|
82
|
+
|
83
|
+
#### Find a subscriber
|
84
|
+
|
85
|
+
The `activation` offers an easier way to find the subscriber details based on
|
86
|
+
the `activation_token`. To find a `subscriber` use
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
DiscountNetwork::Activation.find(activation_token)
|
90
|
+
```
|
91
|
+
|
92
|
+
#### Activate a subscriber
|
93
|
+
|
94
|
+
Once subscriber has provided their required information then we can activate
|
95
|
+
their account using the `Activation` API. The API already has validations in
|
96
|
+
place but it would be easier to implement some validation before sending any
|
97
|
+
activation API request.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
DiscountNetwork::Activation.activate(
|
101
|
+
activation_token, subscriber_attributes
|
102
|
+
)
|
103
|
+
```
|
104
|
+
|
105
|
+
### Password
|
106
|
+
|
107
|
+
#### Forgot password
|
108
|
+
|
109
|
+
If the subscriber forgot their password then developer can allow them to reset
|
110
|
+
their password using the Discount network API.
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
DiscountNetwork::Password.forgot(email_address)
|
114
|
+
```
|
115
|
+
|
116
|
+
#### Validate reset token
|
117
|
+
|
118
|
+
Before allowing subscriber to reset their password, developer can validate if
|
119
|
+
the reset token is valid or not as expired or invalid reset token won't allow
|
120
|
+
subscriber to reset their password.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
DiscountNetwork::Password.validate(reset_token)
|
124
|
+
```
|
125
|
+
|
126
|
+
#### Set new password
|
127
|
+
|
128
|
+
Once subscriber has submitted a valid reset request and followed the instruction
|
129
|
+
then we can allow them to set their password as follow
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
DiscountNetwork::Password.create(reset_token, password_attributes)
|
133
|
+
```
|
134
|
+
|
135
|
+
### Supplementary
|
136
|
+
|
137
|
+
Each Discount Network subscription comes with varies number of supplementary
|
138
|
+
subscribers, that means primary member can add one or more of this friends or
|
139
|
+
family member to his subscription. The supplementary API provides an easier
|
140
|
+
way to manage the supplementary subscribers. Please note, This interface expects
|
141
|
+
that the subscriber has already been authenticated before making any API call.
|
142
|
+
|
143
|
+
#### List supplementaries
|
144
|
+
|
145
|
+
To list all the supplementary subscribers for the authenticated subscriber
|
146
|
+
developer can use the `list` interface for supplementary.
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
DiscountNetwork::Supplementary.list
|
150
|
+
```
|
151
|
+
|
152
|
+
#### Add supplementary
|
153
|
+
|
154
|
+
To add a new supplementary subscriber with authenticated subscriber's
|
155
|
+
subscription developer can use the `create` interface for supplementary.
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
DiscountNetwork::Supplementary.create(
|
159
|
+
subscriber_attributes
|
160
|
+
)
|
161
|
+
|
162
|
+
# subscriber attributes
|
163
|
+
subscriber_attributes = {
|
164
|
+
first_name: "John",
|
165
|
+
last_name: "Green",
|
166
|
+
phone: "+1 123 345 5678",
|
167
|
+
email: "john.green@example.com"
|
168
|
+
}
|
169
|
+
```
|
170
|
+
|
171
|
+
### Destination
|
172
|
+
|
173
|
+
#### List destinations
|
174
|
+
|
175
|
+
Retrieve the list of destinations based on a search term. This is useful when
|
176
|
+
you are planning to show the list of destinations while user start to type
|
177
|
+
destination name
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
DiscountNetwork::Destination.list(term: "bangkok")
|
181
|
+
```
|
182
|
+
|
183
|
+
### Search
|
184
|
+
|
185
|
+
#### Create new search
|
186
|
+
|
187
|
+
You can create a new search using the API. Please note: It's advisable to wait
|
188
|
+
10-15 seconds before trying to retrieve the search results.
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
DiscountNetwork::Search.create(
|
192
|
+
adults: 2,
|
193
|
+
children: 0,
|
194
|
+
room_numbers: 1
|
195
|
+
location_id: 835,
|
196
|
+
check_in: "25/10/2016",
|
197
|
+
check_out: "28/10/2016",
|
198
|
+
location_name: "Bangkok, Thailand",
|
199
|
+
)
|
200
|
+
```
|
201
|
+
|
202
|
+
#### Retrieve search details
|
203
|
+
|
204
|
+
Once, you have created a new search and you have the search id then you can
|
205
|
+
retrieve the search details as
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
DiscountNetwork::Search.find(search_id)
|
209
|
+
```
|
210
|
+
|
211
|
+
### Result
|
212
|
+
|
213
|
+
#### Retrieve search results
|
214
|
+
|
215
|
+
Retrieve the hotel search results from the Discount Network API.
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
DiscountNetwork::Result.where(
|
219
|
+
search_id: search_id
|
220
|
+
)
|
221
|
+
```
|
222
|
+
|
223
|
+
#### Retrieve a hotel details
|
224
|
+
|
225
|
+
Retrieve the hotel details for a specific search
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
DiscountNetwork::Result.find_by(
|
229
|
+
search_id: search_id, hotel_id: hotel_id
|
230
|
+
)
|
231
|
+
```
|
232
|
+
|
233
|
+
### Booking
|
234
|
+
|
235
|
+
#### Creating a new booking
|
236
|
+
|
237
|
+
Submit a new booking request for a specific hotel using the API.
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
# Creating a new booking request for a specific hotel, Pay close
|
241
|
+
# attention to the construction of the booking object
|
242
|
+
#
|
243
|
+
# If you want to add multiple travellers in one booking then
|
244
|
+
# pass an Array for travellers as
|
245
|
+
# `travellers: [traveller_one_attributes, traveller_two_attributes]`
|
246
|
+
#
|
247
|
+
# If you need to add multiple properties in one request, like one
|
248
|
+
# hotel and a condo in one booking request, then pass an Array as
|
249
|
+
#`properties: [property_one_attributes, property_two_attributes]`
|
250
|
+
|
251
|
+
DiscountNetwork::Booking.create(
|
252
|
+
hotel_id: "hotel_id",
|
253
|
+
search_id: "search_id",
|
254
|
+
travellers: traveller_attributes,
|
255
|
+
properties: property_attributes
|
256
|
+
)
|
257
|
+
|
258
|
+
# Traveller attributes
|
259
|
+
|
260
|
+
traveller_attributes = {
|
261
|
+
first_name: "John",
|
262
|
+
last_name: "Doe",
|
263
|
+
phone: "012 345 6789",
|
264
|
+
email: "john.doe@example.com",
|
265
|
+
address: "123 Main Street",
|
266
|
+
city: "New York",
|
267
|
+
state: "New York",
|
268
|
+
zip: "NY10310"
|
269
|
+
}
|
270
|
+
|
271
|
+
# Property attributes
|
272
|
+
|
273
|
+
property_attributes = {
|
274
|
+
property_id: "property_101",
|
275
|
+
provider_name: "Booking.com",
|
276
|
+
name: "Nasa Vagas, Thailand",
|
277
|
+
price: "100.99",
|
278
|
+
description: "Description",
|
279
|
+
review_score: "90",
|
280
|
+
total_reviews: "10000",
|
281
|
+
currency_code: "USD"
|
282
|
+
}
|
283
|
+
```
|
284
|
+
|
285
|
+
### Find a booking
|
286
|
+
|
287
|
+
To find the details for a specific booking request, including the confirmation
|
288
|
+
number, use
|
289
|
+
|
290
|
+
```ruby
|
291
|
+
DiscountNetwork::Booking.find(booking_id)
|
292
|
+
```
|
293
|
+
|
294
|
+
### Providers
|
295
|
+
|
296
|
+
Discount Network partnered with multiple vendors to provide the best possible
|
297
|
+
price in all sort of travel. This will allow developer to add those public
|
298
|
+
provider in their site and let their subscriber search and contact the support
|
299
|
+
to receive their subscriber discount.
|
300
|
+
|
301
|
+
#### Retrieving providers
|
302
|
+
|
303
|
+
This will allow developer to retrieve all of the specified public travel partner
|
304
|
+
so they can show them to their site. Note: They `type` is mandatory and
|
305
|
+
the supported value for `type` are `hotel`, `cruise`, `resort`, `tour`, `package`
|
306
|
+
and `car`.
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
DiscountNetwork::Provider.where(type: "hotel")
|
310
|
+
```
|
311
|
+
|
312
|
+
### Find a provider
|
313
|
+
|
314
|
+
On the provider listing API, one of the most important `attribute` the API
|
315
|
+
returns is `slug`, we can use that `slug` to retrieve the details about any
|
316
|
+
specific provider.
|
317
|
+
|
318
|
+
```ruby
|
319
|
+
DiscountNetwork::Provider.find_by_slug(slug)
|
320
|
+
```
|
321
|
+
|
322
|
+
## Development
|
323
|
+
|
324
|
+
We are following Sandi Metz's Rules for this gem, you can read the
|
325
|
+
[description of the rules here]
|
326
|
+
(http://robots.thoughtbot.com/post/50655960596/sandi-metz-rules-for-developers). All new code should follow these rules. If you make changes in a pre-existing
|
327
|
+
file that violates these rules you should fix the violations as part of
|
328
|
+
your contribution.
|
329
|
+
|
330
|
+
### Setup
|
331
|
+
|
332
|
+
* Clone the repository.
|
333
|
+
|
334
|
+
```sh
|
335
|
+
git clone https://github.com/discountnetwork/discountnetwork-ruby.git
|
336
|
+
```
|
337
|
+
|
338
|
+
* Setup your environment.
|
339
|
+
|
340
|
+
```sh
|
341
|
+
bin/setup
|
342
|
+
```
|
343
|
+
|
344
|
+
* Run the test suite
|
345
|
+
|
346
|
+
```sh
|
347
|
+
bin/rake
|
348
|
+
```
|
349
|
+
|
350
|
+
### PlayBox
|
351
|
+
|
352
|
+
* Setup API keys.
|
353
|
+
|
354
|
+
```sh
|
355
|
+
cp .sample.pryrc .pryrc
|
356
|
+
vim .pryrc
|
357
|
+
```
|
358
|
+
|
359
|
+
* Start your console.
|
360
|
+
|
361
|
+
```sh
|
362
|
+
bin/console
|
363
|
+
```
|
364
|
+
|
365
|
+
* Start playing with it.
|
366
|
+
|
367
|
+
```sh
|
368
|
+
DiscountNetwork::Session.create(name: "username", password: "password")
|
369
|
+
```
|
370
|
+
|
371
|
+
## Testing
|
372
|
+
|
373
|
+
### RSpec
|
374
|
+
|
375
|
+
This gem provides an easier way to test Discount Network API Responses. Simply
|
376
|
+
include the following line in your `spec_helper` and you should have access to
|
377
|
+
all of the test helpers.
|
378
|
+
|
379
|
+
```ruby
|
380
|
+
require "discountnetwork/rspec"
|
381
|
+
```
|
382
|
+
|
383
|
+
## Contributing
|
384
|
+
|
385
|
+
First, thank you for contributing! We love pull requests from everyone. By
|
386
|
+
participating in this project, you hereby grant the right to grant or transfer
|
387
|
+
an unlimited number of non exclusive licenses or sub-licenses to third parties,
|
388
|
+
under the copyright covering the contribution to use the contribution by all
|
389
|
+
means.
|
390
|
+
|
391
|
+
Here are a few technical guidelines to follow:
|
392
|
+
|
393
|
+
1. Open an [issue][issues] to discuss a new feature.
|
394
|
+
1. Write tests to support your new feature.
|
395
|
+
1. Make sure the entire test suite passes locally and on CI.
|
396
|
+
1. Open a Pull Request.
|
397
|
+
1. [Squash your commits][squash] after receiving feedback.
|
398
|
+
1. Party!
|
399
|
+
|
400
|
+
[issues]: https://github.com/discountnetwork/discountnetwork-ruby/issues
|
401
|
+
[squash]: https://github.com/thoughtbot/guides/tree/master/protocol/git#write-a-feature
|
402
|
+
|
403
|
+
## License
|
404
|
+
|
405
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/rake
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'rake' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
16
|
+
|
17
|
+
load Gem.bin_path("rake", "rake")
|
data/bin/rspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'rspec' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
16
|
+
|
17
|
+
load Gem.bin_path("rspec-core", "rspec")
|
data/bin/setup
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "discountnetwork/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "discountnetwork"
|
8
|
+
spec.version = Discountnetwork::VERSION
|
9
|
+
spec.authors = ["Abu Nashir"]
|
10
|
+
spec.email = ["abunashir@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "The Ruby interface to the Discount Network API"
|
13
|
+
spec.description = "The Ruby interface to the Discount Network API"
|
14
|
+
spec.homepage = "https://github.com/discountnetwork/discountnetwork-ruby"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
spec.files = `git ls-files`.split("\n")
|
19
|
+
spec.test_files = `git ls-files -- {spec}/*`.split("\n")
|
20
|
+
|
21
|
+
spec.add_dependency "rest-client", "~> 1.8"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
spec.add_development_dependency "webmock", "~> 2.0"
|
27
|
+
spec.add_development_dependency "pry", "~> 0.10.3"
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module DiscountNetwork
|
2
|
+
class Account < Base
|
3
|
+
def find(auth_token = nil)
|
4
|
+
set_account_auth_token(auth_token)
|
5
|
+
if auth_token_exists?
|
6
|
+
DiscountNetwork.get_resource("account").user
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def update(attributes)
|
11
|
+
if auth_token_exists?
|
12
|
+
DiscountNetwork.put_resource("account", subscriber: attributes)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def auth_token_exists?
|
19
|
+
!DiscountNetwork.configuration.auth_token.nil?
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_account_auth_token(auth_token)
|
23
|
+
if !auth_token.nil?
|
24
|
+
DiscountNetwork.configuration.auth_token = auth_token
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module DiscountNetwork
|
2
|
+
class Activation < Base
|
3
|
+
def find(token)
|
4
|
+
DiscountNetwork.get_resource(
|
5
|
+
["account", "activation", token].join("/"),
|
6
|
+
).user
|
7
|
+
end
|
8
|
+
|
9
|
+
def activate(token, attributes)
|
10
|
+
DiscountNetwork.put_resource(
|
11
|
+
["account", "activation", token].join("/"), user: attributes
|
12
|
+
).user
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module DiscountNetwork
|
2
|
+
class Base
|
3
|
+
def self.method_missing(method_name, *arguments, &block)
|
4
|
+
if new.respond_to?(method_name, include_private: false)
|
5
|
+
new.send(method_name, *arguments, &block)
|
6
|
+
else
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def build_array_params(array_params)
|
14
|
+
array_params = [array_params].flatten
|
15
|
+
array_params.map.each_with_index do |attribute, index|
|
16
|
+
[index, attribute]
|
17
|
+
end.to_h
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module DiscountNetwork
|
2
|
+
class Booking < Base
|
3
|
+
def find(booking_id)
|
4
|
+
DiscountNetwork.get_resource(
|
5
|
+
["bookings", booking_id].join("/")
|
6
|
+
).travel_request
|
7
|
+
end
|
8
|
+
|
9
|
+
def create(search_id:, hotel_id:, travellers:, properties:, **attrs)
|
10
|
+
attributes = attrs.merge(
|
11
|
+
search_id: search_id,
|
12
|
+
travellers_attributes: build_array_params(travellers),
|
13
|
+
properties_attributes: build_array_params(
|
14
|
+
properties.merge(property_id: hotel_id)
|
15
|
+
)
|
16
|
+
)
|
17
|
+
|
18
|
+
DiscountNetwork.post_resource(
|
19
|
+
"bookings", booking: attributes
|
20
|
+
).travel_request
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "rest-client"
|
2
|
+
require "discountnetwork/response"
|
3
|
+
require "discountnetwork/configuration"
|
4
|
+
|
5
|
+
module DiscountNetwork
|
6
|
+
class Client
|
7
|
+
attr_reader :http_method, :end_point, :attributes
|
8
|
+
|
9
|
+
def initialize(http_method, end_point, attributes = {})
|
10
|
+
@http_method = http_method
|
11
|
+
@end_point = end_point
|
12
|
+
@attributes = attributes
|
13
|
+
end
|
14
|
+
|
15
|
+
def execute
|
16
|
+
Response.new(execute_api_request).parse
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def execute_api_request
|
22
|
+
RestClient::Request.execute(
|
23
|
+
method: http_method,
|
24
|
+
url: api_end_point,
|
25
|
+
payload: attributes,
|
26
|
+
headers: custom_headers
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
def api_end_point
|
31
|
+
[DiscountNetwork.configuration.api_host, end_point].join("/")
|
32
|
+
end
|
33
|
+
|
34
|
+
def custom_headers
|
35
|
+
{
|
36
|
+
"DN-API-KEY" => DiscountNetwork.configuration.api_key,
|
37
|
+
"Authorization" =>
|
38
|
+
"Token token=\"#{DiscountNetwork.configuration.auth_token}\""
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.get_resource(end_point, attributes = {})
|
44
|
+
Client.new(:get, end_point, attributes).execute
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.post_resource(end_point, attributes)
|
48
|
+
Client.new(:post, end_point, attributes).execute
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.put_resource(end_point, attributes)
|
52
|
+
Client.new(:put, end_point, attributes).execute
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DiscountNetwork
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :api_host, :api_key, :auth_token
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@api_host ||= "https://api.discountnetwork.io/api/v1"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.configure
|
11
|
+
yield configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configuration
|
15
|
+
@configuration ||= Configuration.new
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module DiscountNetwork
|
2
|
+
class Password < Base
|
3
|
+
def forgot(email)
|
4
|
+
DiscountNetwork.post_resource(
|
5
|
+
"account/resets", account: { email: email }
|
6
|
+
)
|
7
|
+
end
|
8
|
+
|
9
|
+
def validate(token)
|
10
|
+
DiscountNetwork.get_resource(
|
11
|
+
["account", "resets", token].join("/"),
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
def create(token, attributes)
|
16
|
+
DiscountNetwork.put_resource(
|
17
|
+
["account", "passwords", token].join("/"), account: attributes
|
18
|
+
)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module DiscountNetwork
|
2
|
+
class Provider < Base
|
3
|
+
def where(type:)
|
4
|
+
DiscountNetwork.get_resource(
|
5
|
+
"providers", type: type
|
6
|
+
).providers
|
7
|
+
end
|
8
|
+
|
9
|
+
def find_by_slug(slug)
|
10
|
+
DiscountNetwork.get_resource(
|
11
|
+
["providers", slug].join("/"),
|
12
|
+
).provider
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|