homeaway-api 1.0.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.
@@ -0,0 +1,57 @@
1
+ # Copyright (c) 2015 HomeAway.com, Inc.
2
+ # All rights reserved. http://www.homeaway.com
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ module HomeAway
17
+ module API
18
+ module Domain
19
+ module SubmitReview
20
+
21
+ # Creates a review for the given listing and unit. The review submitted must go through the standard HomeAway
22
+ # review process and may not appear immediately within the list of reviews for the listing. It is recommended
23
+ # that the link to submitting a review be placed off of the details page of the listing and unit in question.
24
+ #
25
+ # analogous to calling a POST on API url /public/submitReview
26
+ #
27
+ # @note user must be logged in via 3 legged oauth to call this function without error
28
+ #
29
+ # @param headline [String] A short summary about the stay.
30
+ # @param body [String] The body of the review.
31
+ # @param locale [String] The locale that the review was written in.
32
+ # @param arrival_date [String, DateTime] The date of arrival for the stay. Can either be a date-parsable string or a DateTime object
33
+ # @param rating [Integer] An overall rating for the stay between 1 and 5.
34
+ # @param listing_id [String] A listing id as supplied by this public API.
35
+ # @param unit_id [String] The unit id within the listing that the review is for.
36
+ # @return [Boolean] true if the review was successfully posted to the moderation queue
37
+ def submit_review(headline, body, locale, arrival_date, rating, listing_id, unit_id, opts={})
38
+ body = {
39
+ 'headline' => headline.to_s,
40
+ 'body' => body.to_s,
41
+ 'locale' => locale.to_s,
42
+ 'arrivalDate' => HomeAway::API::Util::Validators.date(arrival_date),
43
+ 'rating' => HomeAway::API::Util::Validators.integer(rating, 0, 6),
44
+ 'listingId' => listing_id.to_s,
45
+ 'unitId' => unit_id.to_s
46
+ }.merge(HomeAway::API::Util::Validators.query_keys(opts))
47
+ begin
48
+ post '/public/submitReview', body
49
+ true
50
+ rescue => e
51
+ raise e
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,144 @@
1
+ # Copyright (c) 2015 HomeAway.com, Inc.
2
+ # All rights reserved. http://www.homeaway.com
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ module HomeAway
17
+ module API
18
+ # a collection of error classes corresponding to various errors that might be raised by the HomeAway API
19
+ module Errors
20
+
21
+ # @private
22
+ class HomeAwayAPIError < StandardError
23
+
24
+ attr_reader :response
25
+
26
+ # @private
27
+ def initialize(response=nil)
28
+ @response = response
29
+ end
30
+
31
+ # @private
32
+ def to_s
33
+ begin
34
+ buf = "#{self.class.name} #{@response._metadata.status_code} "
35
+ @response.violations.each do |violation|
36
+ buf << "#{violation.description} | "
37
+ end
38
+ buf = buf[0..-3] unless @response.violations.empty?
39
+ return buf
40
+ rescue
41
+ if @response.nil?
42
+ super
43
+ else
44
+ @response.to_s
45
+ end
46
+ end
47
+ end
48
+
49
+ # @private
50
+ def self.method_missing(name, *args, &block)
51
+ if @response.respond_to? name
52
+ @response.send name
53
+ else
54
+ super
55
+ end
56
+ end
57
+ end
58
+
59
+ # raised when the token a client is using has become expired
60
+ class TokenExpiredError < StandardError
61
+ end
62
+
63
+ # @private
64
+ class ClientError < HomeAwayAPIError
65
+ end
66
+
67
+ # represents a HTTP 400
68
+ class BadRequestError < ClientError
69
+ CODE = 400
70
+ end
71
+
72
+ # represents a HTTP 500
73
+ class InternalServerError < HomeAwayAPIError
74
+ CODE = 500
75
+ end
76
+
77
+ # represents a HTTP 404
78
+ class ResourceNotFoundError < ClientError
79
+ CODE = 404
80
+ end
81
+
82
+ # represents a HTTP 401
83
+ class UnauthorizedError < ClientError
84
+ CODE = 401
85
+ end
86
+
87
+ # represents a HTTP 403
88
+ class ForbiddenError < ClientError
89
+ CODE = 403
90
+ end
91
+
92
+ # represents a HTTP 405
93
+ class MethodNotAllowedError < ClientError
94
+ CODE = 405
95
+ end
96
+
97
+ # represents a HTTP 416
98
+ class RequestedRangeNotSatisfiableError < ClientError
99
+ CODE = 416
100
+ end
101
+
102
+ # represents a HTTP 429. This will be raised when a client exceeds their rate limit
103
+ class TooManyRequestsError < ClientError
104
+ CODE = 429
105
+ end
106
+
107
+ # represents a HTTP 503
108
+ class APINotAvailableError < HomeAwayAPIError
109
+ CODE = 503
110
+ end
111
+
112
+ # represents a HTTP 406
113
+ class NotAcceptableError < ClientError
114
+ CODE = 406
115
+ end
116
+
117
+ # represents a HTTP 409
118
+ class ConflictError < ClientError
119
+ CODE = 409
120
+ end
121
+
122
+ # @private
123
+ class ImATeapotError < ClientError
124
+ #hopefully this is never ever used
125
+ CODE = 418
126
+ end
127
+
128
+ # @private
129
+ def self.for_http_code(status_code)
130
+ begin
131
+ self.const_get(self.constants.select do |c|
132
+ const = self.const_get c
133
+ Class === const &&
134
+ const.const_defined?(:CODE) &&
135
+ const.const_get(:CODE) == status_code.to_i
136
+ end.first)
137
+ rescue => _
138
+ HomeAwayAPIError
139
+ end
140
+ end
141
+
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,140 @@
1
+ # Copyright (c) 2015 HomeAway.com, Inc.
2
+ # All rights reserved. http://www.homeaway.com
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ module HomeAway
17
+ module API
18
+ class Paginator
19
+ include Enumerable
20
+
21
+ # @private
22
+ def initialize(client, hashie, auto_pagination=false)
23
+ @hashie = hashie
24
+ @client = client
25
+ @auto_pagination = auto_pagination
26
+ end
27
+
28
+ # supply a block that expects a single parameter to iterate through this paginator
29
+ def each(&block)
30
+ if @auto_pagination
31
+ first_hashie = @hashie.clone
32
+ while true
33
+ cur_hits = entries.clone
34
+ cur_hits.each do |entity|
35
+ if block_given?
36
+ block.call entity
37
+ else
38
+ yield entity
39
+ end
40
+ end
41
+ if @hashie.nextPage && @hashie.page < 300
42
+ begin
43
+ next_page!
44
+ rescue HomeAway::API::Errors::RequestedRangeNotSatisfiableError
45
+ # we have a max page limit
46
+ # reset me to the first so that I can be iterated over again
47
+ @hashie = first_hashie
48
+ break
49
+ end
50
+ else
51
+ # reset me to the first so that I can be iterated over again
52
+ @hashie = first_hashie
53
+ break
54
+ end
55
+ end
56
+ else
57
+ entries.each do |entity|
58
+ if block_given?
59
+ block.call entity
60
+ else
61
+ yield entity
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ # @return [Integer] the size of this paginator
68
+ def size
69
+ return @hashie['size'] if @hashie.has_key? 'size'
70
+ 0
71
+ end
72
+
73
+ alias_method :length, :size
74
+
75
+ # @return [HomeAway::API::Paginator] a paginator object that has the next page of results
76
+ def next_page
77
+ return nil unless next_page?
78
+ next_hashie = @client.get(*parse_url(nextPage))
79
+ self.class.new(@client, next_hashie, @auto_pagination)
80
+ end
81
+
82
+ # updates this paginator to have the next page of results in place
83
+ # @return [Boolean] true if successful
84
+ def next_page!
85
+ return false unless next_page?
86
+ @hashie = @client.get(*parse_url(nextPage))
87
+ true
88
+ end
89
+
90
+ # @return [Boolean] does this paginator have another page?
91
+ def next_page?
92
+ @hashie.has_key? 'nextPage'
93
+ end
94
+
95
+ # @return [HomeAway::API::Paginator] a paginator object that has the previous page of results
96
+ def previous_page
97
+ return nil unless previous_page?
98
+ prev_hashie = @client.get(*parse_url(prevPage))
99
+ self.class.new(@client, prev_hashie, @auto_pagination)
100
+ end
101
+
102
+ # updates this paginator to have the previous page of results in place
103
+ # @return [Boolean] true if successful
104
+ def previous_page!
105
+ return false unless previous_page?
106
+ @hashie = @client.get(*parse_url(prevPage))
107
+ true
108
+ end
109
+
110
+ # @return [Boolean] does this paginator have previous page?
111
+ def previous_page?
112
+ @hashie.has_key? 'prevPage'
113
+ end
114
+
115
+ # @private
116
+ def method_missing(name, *args, &block)
117
+ if @hashie.respond_to? name
118
+ @hashie.send name, *args, &block
119
+ else
120
+ super
121
+ end
122
+ end
123
+
124
+ # @return [Array] the current page of results as an array of entities
125
+ def entries
126
+ entries = @hashie['entries'] ||= nil
127
+ entries.map { |entry| HomeAway::API::Response.new(entry) }
128
+ end
129
+
130
+ alias_method :hits, :entries
131
+
132
+ private
133
+
134
+ def parse_url(input)
135
+ uri = URI.parse(input)
136
+ return uri.path, CGI.parse(uri.query)
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,49 @@
1
+ # Copyright (c) 2015 HomeAway.com, Inc.
2
+ # All rights reserved. http://www.homeaway.com
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ module HomeAway
17
+ module API
18
+ # A container around the response that the HomeAway API returns.
19
+ # This object extends Hashie::Mash and can be accessed in an identical method
20
+ class Response < Hashie::Mash
21
+
22
+ # @private
23
+ def convert_key(key) #:nodoc:
24
+ HomeAway::API::Util::Validators.camel_case(key)
25
+ end
26
+
27
+ # a helper method to determine if this response has
28
+ # any particular fields in its payload, potentially
29
+ # nested
30
+ #
31
+ # @param attrs [Symbol] one or more symbols to search into this response with
32
+ def has?(*attrs)
33
+ entity = self
34
+ attrs.each do |attr|
35
+ return false unless entity.has_key?(attr.to_sym)
36
+ entity = entity.send(attr.to_sym)
37
+ end
38
+ true
39
+ end
40
+
41
+ # Returns information about the request that was just made. This includes
42
+ # things such as connection headers and status code
43
+ # @return [HomeAway::API::Response] information about the request that was made
44
+ def _metadata
45
+ @metadata ||= HomeAway::API::Response.new
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,92 @@
1
+ # Copyright (c) 2015 HomeAway.com, Inc.
2
+ # All rights reserved. http://www.homeaway.com
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'singleton'
17
+
18
+ module HomeAway
19
+ module API
20
+ module Util
21
+ # @private
22
+ class Defaults
23
+ include Singleton
24
+
25
+ def site
26
+ 'https://ws.homeaway.com'
27
+ end
28
+
29
+ def port
30
+ '443'
31
+ end
32
+
33
+ def auth_url
34
+ '/oauth/authenticate?clientId='
35
+ end
36
+
37
+ def token_url
38
+ '/oauth/token?credentials='
39
+ end
40
+
41
+ def logger
42
+ default_logger = Logger.new(STDOUT)
43
+ default_logger.level = Logger::WARN
44
+ default_logger
45
+ end
46
+
47
+ def cache_control
48
+ 'max-age: 0, must-revalidate'
49
+ end
50
+
51
+ def auto_pagination
52
+ true
53
+ end
54
+
55
+ def page_size
56
+ 10
57
+ end
58
+
59
+ def connection_opts
60
+ {}
61
+ end
62
+
63
+ def adapter
64
+ :faraday
65
+ end
66
+
67
+ def auto_reauth
68
+ true
69
+ end
70
+
71
+ def manual_token_supplied
72
+ false
73
+ end
74
+
75
+ def test_mode
76
+ false
77
+ end
78
+
79
+ def to_hash
80
+ keys = self.class.instance_methods(false)
81
+ keys.delete(:to_hash)
82
+ hash = {}
83
+ keys.each do |key|
84
+ hash[key] = send key
85
+ end
86
+ hash
87
+ end
88
+ end.freeze
89
+ Defaults.instance.freeze
90
+ end
91
+ end
92
+ end