google_reviews 1.0.1 → 1.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: 7a2390398990045326e6bb2fb15d1554cc4584828cb7eaa9c718f15cf4bc5963
4
- data.tar.gz: 18c7d38928b7e37b0e42496459460e6c8fcef006afb7269eac28e31ab52c05e2
3
+ metadata.gz: ca7e65247ded03cfbf602e3fcee265503daaacc8e653196618996ebb771bfcd0
4
+ data.tar.gz: e38088f8c3822c7f21683fc259ce3a00a9ccd11d4e3b2fe61b7cd9bfbbb44400
5
5
  SHA512:
6
- metadata.gz: 55110b305862e5b54dd11298cfaa1828353de938cedb9eed84faf959d02343c497a811082911914d9cc0ff7ed816a374131fc9ae50bb04f73ec33bcd1d897638
7
- data.tar.gz: c88bbdb138bda9198b0ecbdaff8e4399d15be666510eb2ed17cacdb7007f4f733321f728cde5470f1b3f67d313fa42fa8f031103362bdf8169468025dff0898b
6
+ metadata.gz: 89f48ebec6ff0acc71db451657dd5a643635606cf57ebf5776c33783dd53971f379e095de5e27654df58f38915e9fac4c6ab610504e71af0084ab9fe02d4b907
7
+ data.tar.gz: d0f307c5981696f98a16483286f66ae21891ea1b36fedb92d99f7fe584216a6a53e4b83bd9a5627b616232a439861715ce78df3bf6fdf41ea26417e17a31adfc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- google_reviews (1.0.0)
4
+ google_reviews (1.0.1)
5
5
  i18n
6
6
 
7
7
  GEM
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GoogleReviews
4
+ # Response Object
5
+ class Response
6
+ attr_reader :status, :data, :error
7
+
8
+ def initialize(data: [], status: "OK", error: "")
9
+ @data = data
10
+ @status = status
11
+ @error = error
12
+ end
13
+
14
+ def success?
15
+ status == "OK"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GoogleReviews
4
+ # Google Review Object
5
+ class Review
6
+ attr_accessor :text, :time, :rating, :language, :author_url, :translated, :author_name,
7
+ :original_language, :profile_photo_url, :relative_time_description
8
+
9
+ def initialize(data)
10
+ @text = data["text"]
11
+ @time = data["time"]
12
+ @rating = data["rating"]
13
+ @language = data["language"]
14
+ @author_url = data["author_url"]
15
+ @translated = data["translated"]
16
+ @author_name = data["author_name"]
17
+ @original_language = data["original_language"]
18
+ @profile_photo_url = data["profile_photo_url"]
19
+ @relative_time_description = data["relative_time_description"]
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "google_reviews/review"
4
+
3
5
  # GoogleReviews module
4
6
  module GoogleReviews
5
7
  # Various utility methods
@@ -13,28 +15,40 @@ module GoogleReviews
13
15
  end
14
16
 
15
17
  def formatted_reviews_data(reviews)
18
+ return [] if reviews.nil? || reviews.empty?
19
+
16
20
  reviews.map do |review|
17
- {
18
- text: review["text"],
19
- time: review["time"],
20
- rating: review["rating"],
21
- language: review["language"],
22
- author_url: review["author_url"],
23
- translated: review["translated"],
24
- author_name: review["author_name"],
25
- original_language: review["original_language"],
26
- profile_photo_url: review["profile_photo_url"],
27
- relative_time_description: review["relative_time_description"],
28
- }
21
+ Review.new(review)
29
22
  end
30
23
  end
31
24
 
25
+ def execute_place_name_request(url)
26
+ data = ApiRequest.execute(url)
27
+
28
+ raise ApiError, I18n.t("messages.search_place_api_error", exception: data["status"]) unless data["status"] == "OK"
29
+
30
+ data["results"]
31
+ end
32
+
33
+ def execute_place_id_request(url)
34
+ data = ApiRequest.execute(url)
35
+
36
+ error = I18n.t("messages.search_id_api_error", exception: data["status"])
37
+ return GoogleReviews::Response.new(status: data["status"], error: error) unless data["status"] == "OK"
38
+
39
+ reviews = data["result"]["reviews"]
40
+ return GoogleReviews::Response.new if reviews.nil? || reviews.empty?
41
+
42
+ GoogleReviews::Response.new(data: formatted_reviews_data(reviews))
43
+ end
44
+
32
45
  private
33
46
 
34
47
  def encoded_place_name(place_name)
35
48
  URI.encode_www_form_component(place_name)
36
49
  end
37
50
 
38
- module_function :request_uri, :formatted_reviews_data, :encoded_place_name
51
+ module_function :request_uri, :formatted_reviews_data, :encoded_place_name, :execute_place_id_request,
52
+ :execute_place_name_request
39
53
  end
40
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GoogleReviews
4
- VERSION = "1.0.1"
4
+ VERSION = "1.1.2"
5
5
  end
@@ -5,6 +5,7 @@ require "json"
5
5
  require "net/http"
6
6
  require "google_reviews/error"
7
7
  require "google_reviews/utils"
8
+ require "google_reviews/response"
8
9
  require "google_reviews/constants"
9
10
  require "google_reviews/api_request"
10
11
  require "google_reviews/translations"
@@ -16,25 +17,24 @@ module GoogleReviews
16
17
  def self.fetch_reviews_by_place_name(api_key, place_name)
17
18
  url = Utils.request_uri(api_key, place_name: place_name)
18
19
 
19
- data = ApiRequest.execute(url)
20
+ begin
21
+ data = Utils.execute_place_name_request(url)
22
+ return GoogleReviews::Response.new unless data
20
23
 
21
- raise ApiError, I18n.t("messages.search_place_api_error", exception: data["status"]) unless data["status"] == "OK"
22
-
23
- place_id = data["results"].first["place_id"]
24
- fetch_reviews_by_place_id(api_key, place_id)
24
+ fetch_reviews_by_place_id(api_key, data.first["place_id"])
25
+ rescue ApiError => e
26
+ GoogleReviews::Response.new(status: "409", error: e)
27
+ end
25
28
  end
26
29
 
27
30
  def self.fetch_reviews_by_place_id(api_key, place_id)
28
31
  url = Utils.request_uri(api_key, place_id: place_id)
29
32
 
30
- data = ApiRequest.execute(url)
31
-
32
- raise ApiError, I18n.t("messages.search_id_api_error", exception: data["status"]) unless data["status"] == "OK"
33
-
34
- reviews = data["result"]["reviews"]
35
- return [] if reviews.nil? || reviews.empty?
36
-
37
- Utils.formatted_reviews_data(reviews)
33
+ begin
34
+ Utils.execute_place_id_request(url)
35
+ rescue ApiError => e
36
+ GoogleReviews::Response.new(status: "409", error: e)
37
+ end
38
38
  end
39
39
  end
40
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_reviews
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - gullariz12
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-12 00:00:00.000000000 Z
11
+ date: 2023-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -61,6 +61,8 @@ files:
61
61
  - lib/google_reviews/api_request.rb
62
62
  - lib/google_reviews/constants.rb
63
63
  - lib/google_reviews/error.rb
64
+ - lib/google_reviews/response.rb
65
+ - lib/google_reviews/review.rb
64
66
  - lib/google_reviews/translations.rb
65
67
  - lib/google_reviews/utils.rb
66
68
  - lib/google_reviews/version.rb