yotpo 0.0.2

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.
Files changed (45) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +20 -0
  3. data/.rspec +3 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +4 -0
  7. data/Gemfile +16 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +51 -0
  10. data/Rakefile +11 -0
  11. data/lib/yotpo/api/account.rb +45 -0
  12. data/lib/yotpo/api/account_platform.rb +20 -0
  13. data/lib/yotpo/api/product.rb +25 -0
  14. data/lib/yotpo/api/purchase.rb +38 -0
  15. data/lib/yotpo/api/reminder.rb +14 -0
  16. data/lib/yotpo/api/review.rb +67 -0
  17. data/lib/yotpo/api/user.rb +64 -0
  18. data/lib/yotpo/client.rb +134 -0
  19. data/lib/yotpo/core/response_parser.rb +84 -0
  20. data/lib/yotpo/version.rb +3 -0
  21. data/lib/yotpo.rb +58 -0
  22. data/spec/api/account_platform_spec.rb +33 -0
  23. data/spec/api/account_spec.rb +45 -0
  24. data/spec/api/product_spec.rb +44 -0
  25. data/spec/api/purchase_spec.rb +100 -0
  26. data/spec/api/reminder_spec.rb +22 -0
  27. data/spec/api/review_spec.rb +55 -0
  28. data/spec/api/user_spec.rb +63 -0
  29. data/spec/fixtures/bearer_token.json +4 -0
  30. data/spec/fixtures/check_subdomain.json +12 -0
  31. data/spec/fixtures/get_all_bottom_lines.json +29 -0
  32. data/spec/fixtures/get_list_of_purchases.json +39 -0
  33. data/spec/fixtures/get_login_url.json +10 -0
  34. data/spec/fixtures/get_product_bottom_line.json +12 -0
  35. data/spec/fixtures/get_product_reviews.json +356 -0
  36. data/spec/fixtures/new_account_platform.json +23 -0
  37. data/spec/fixtures/new_purchase.json +4 -0
  38. data/spec/fixtures/new_review.json +64 -0
  39. data/spec/fixtures/new_user.json +12 -0
  40. data/spec/fixtures/send_test_email.json +6 -0
  41. data/spec/fixtures/update_account.json +53 -0
  42. data/spec/helper.rb +65 -0
  43. data/spec/yotpo_spec.rb +4 -0
  44. data/yotpo.gemspec +29 -0
  45. metadata +193 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Y2I3YTE0MTMxMmIxZjRiZTRiMWI4YTFmMWRmNmY4OTU2NmQwNGIyZQ==
5
+ data.tar.gz: !binary |-
6
+ ZDZhOGQ1MTViNDM3MjYyZDk4N2JkY2UxM2NiMGEyMjNiYzk5NjdjYQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MGEwNzY5YjRjYjAwMGFkZjJkY2ZmODQ2OGJiMGY3ZTM3YTFhNDhkZGI0MzIx
10
+ OTEwYjI5NzE1MWZjNWM2MmMxMzYyZTFhNTIzZTMyNWZlODI4ZTE4MDA2ZDQ4
11
+ OTE1ODQ5YjcxMWUzNzU5ODc3M2IwN2RiYWRkNTE0NDc1ZjhiMTA=
12
+ data.tar.gz: !binary |-
13
+ NTM0ODg4YmM0MTM1MzUwYTUyOWNkNTY0Yzg3NTMyNmE2MTIyODI0Njc5ZjJk
14
+ YjQ4NzQ0MTZkMTY4ZTVkYTBjZjU4Mjk3MDBjYzUyMDM1ZmQ1ZDNlYjZmZDJi
15
+ NjUzNGFmMzI2NjJkNTNhNjIyZDU5MzhlNzA3NDA0NWI1MmRkODg=
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ coverage
12
+ InstalledFiles
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --fail-fast
3
+ --order random
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ yotpo-api-connector
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p194
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+ gem 'yard'
5
+ gem 'redcarpet'
6
+
7
+ group :test do
8
+ gem 'coveralls', :require => false
9
+ gem 'rspec'
10
+ gem 'simplecov', :require => false
11
+ gem 'webmock'
12
+ gem 'ffaker'
13
+ end
14
+
15
+ # Specify your gem's dependencies in yotpo-api-connector.gemspec
16
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Vladislav Shub
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Yotpo
2
+
3
+ A Ruby interface to the YOTPO API
4
+
5
+ [gem]: http://rubygems.org/gems/yotpo
6
+ [travis]: http://travis-ci.org/YotpoLtd/yotpo-ruby
7
+ [gemnasium]: https://gemnasium.com/YotpoLtd/yotpo-ruby
8
+ [codeclimate]: https://codeclimate.com/github/YotpoLtd/yotpo-ruby
9
+ [coveralls]: https://coveralls.io/r/YotpoLtd/yotpo-ruby
10
+
11
+ [![Gem Version](https://badge.fury.io/rb/yotpo.png)][gem]
12
+ [![Build Status](https://secure.travis-ci.org/YotpoLtd/yotpo-ruby.png?branch=master)][travis]
13
+ [![Dependency Status](https://gemnasium.com/YotpoLtd/yotpo-ruby.png?travis)][gemnasium]
14
+ [![Code Climate](https://codeclimate.com/github/YotpoLtd/yotpo-ruby.png)][codeclimate]
15
+ [![Coverage Status](https://coveralls.io/repos/YotpoLtd/yotpo-ruby/badge.png?branch=master)][coveralls]
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ gem 'yotpo'
22
+
23
+ And then execute:
24
+
25
+ $ bundle install
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install yotpo
30
+
31
+ ## Usage
32
+
33
+ First [register your application with Yotpo][register]
34
+ Then copy and past the app_key and secret
35
+ ```ruby
36
+ Yotpo.configure do |config|
37
+ config.app_key = APP_KEY
38
+ config.secret = SECRET
39
+ end
40
+ ```
41
+ That is it you are ready
42
+
43
+ [register]: https://www.yotpo.com/register
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :test => :spec
8
+ task :default => :spec
9
+
10
+ require 'yard'
11
+ YARD::Rake::YardocTask.new
@@ -0,0 +1,45 @@
1
+ module Yotpo
2
+ module Account
3
+
4
+ #
5
+ # Updates account at Yotpo
6
+ #
7
+ # @param params [Hash] the account basic information
8
+ # @option params [String] :minisite_website_name the name to display at the yotpo-minisite
9
+ # @option params [String] :minisite_website the website to link the minisite to
10
+ # @option params [String] :minisite_subdomain the subdomain at yotpo.me to use for this account
11
+ # @option params [String] :minisite_cname the cname that will point to the subdomain.yotpo.com
12
+ # @option params [Boolean Integer] :minisite_subdomain_active enable or disable the feature
13
+ # @option params [String] :utoken the login token of the account owner
14
+ # @option params [String] :app_key the account app key that was created at registration
15
+ def update_account(params)
16
+ request = {
17
+ account: {
18
+ minisite_website_name: params[:minisite_website_name],
19
+ minisite_website: params[:minisite_website],
20
+ minisite_subdomain: params[:minisite_subdomain],
21
+ minisite_cname: params[:minisite_cname],
22
+ minisite_subdomain_active: params[:minisite_subdomain_active]
23
+ },
24
+ utoken: params[:utoken]
25
+ }
26
+ app_key = params[:app_key]
27
+ put("/apps/#{app_key}", request)
28
+ end
29
+
30
+ #
31
+ # Check if the minisite subdomain at yotpo.me is vacant
32
+ #
33
+ # @param params [Hash]
34
+ # @option params [String] :utoken the login token of the account owner
35
+ # @option params [String] :app_key the account app key that was created at registration
36
+ # @option params [String] :subdomain the subdomain to check
37
+ def check_minisite_subdomain(params)
38
+ app_key = params[:app_key]
39
+ subdomain = params[:subdomain]
40
+ utoken = params[:utoken]
41
+ get("/apps/#{app_key}/subomain_check/#{subdomain}?utoken=#{utoken}")
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,20 @@
1
+ module Yotpo
2
+ module AccountPlatform
3
+ def create_account_platform(params)
4
+ request ={
5
+ account_platform: {
6
+ shop_token: params[:shop_token],
7
+ shop_domain: params[:shop_domain],
8
+ plan_name: params[:plan_name],
9
+ platform_type_id: params[:platform_type_id],
10
+ deleted: false
11
+ },
12
+ utoken: params[:utoken]
13
+ }
14
+ app_key = params[:app_key]
15
+ post("/apps/#{app_key}/account_platform", request)
16
+ end
17
+ end
18
+ end
19
+
20
+
@@ -0,0 +1,25 @@
1
+ module Yotpo
2
+ module Product
3
+ def get_all_bottom_lines(params)
4
+ request = {
5
+ utoken: params[:utoken],
6
+ since_date: params[:since_date],
7
+ since_id: params[:since_id]
8
+ }
9
+ request.delete_if{|key,val| val.nil?}
10
+ app_key = params[:app_key]
11
+ get("/apps/#{app_key}/bottom_lines", request)
12
+ end
13
+
14
+ #
15
+ # Retrieves the bottom line of a product
16
+ # @param params [Hash]
17
+ # @option params [String] :app_key the accounts app key that was given to it upon registration
18
+ # @option params [String] :product_id the id of the product
19
+ def get_product_bottom_line(params)
20
+ app_key = params[:app_key]
21
+ sku = params[:product_id]
22
+ get("/products/#{app_key}/#{sku}/bottomline")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,38 @@
1
+ module Yotpo
2
+ module Purchase
3
+ def create_new_purchase(params)
4
+ request = {
5
+ utoken: params[:utoken],
6
+ email: params[:email],
7
+ customer_name: params[:customer_name],
8
+ order_id: params[:order_id],
9
+ platform: params[:platform],
10
+ products: params[:products]
11
+ }
12
+ app_key = params[:app_key]
13
+ post("/apps/#{app_key}/purchases", request)
14
+ end
15
+
16
+ def create_new_purchases(params)
17
+ request = {
18
+ utoken: params[:utoken],
19
+ orders: params[:orders]
20
+ }
21
+ app_key = params[:app_key]
22
+ post("/apps/#{app_key}/purchases/mass_create", request)
23
+ end
24
+
25
+ def get_purchases(params)
26
+ request ={
27
+ utoken: params[:utoken],
28
+ since_id: params[:since_id],
29
+ since_date: params[:since_date],
30
+ page: params[:page] || 1,
31
+ count: params[:count] || 10
32
+ }
33
+ request.delete_if{|key, value| value.nil? }
34
+ app_key = params[:app_key]
35
+ get("/apps/#{app_key}/purchases", request)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ module Yotpo
2
+ module Reminder
3
+ def send_test_reminder(params)
4
+ request ={
5
+ utoken: params[:utoken],
6
+ email: params[:email]
7
+ }
8
+ app_key = params[:app_key]
9
+ post("/apps/#{app_key}/reminders/send_test_email", request)
10
+ end
11
+ end
12
+ end
13
+
14
+
@@ -0,0 +1,67 @@
1
+ module Yotpo
2
+ module Review
3
+
4
+ # Creates a new review in Yotpo
5
+ #
6
+ # @param [Hash] params
7
+ # @option params [String] :app_key the app key of the account for which the review is created
8
+ # @option params [String] :product_id the id of the product
9
+ # @option params [String] :shop_domain the domain of the account
10
+ # @option params [String] :product_title the name of the product for which the review is created
11
+ # @option params [String] :product_description the description of the product for which the review is created
12
+ # @option params [String] :product_url the url of the product for which the review is created
13
+ # @option params [String] :product_image_url the image url of the product for which the review is created
14
+ # @option params [String] :user_display_name the author name
15
+ # @option params [String] :user_email the author email
16
+ # @option params [String] :review_body the review itself
17
+ # @option params [String] :review_title the review title
18
+ # @option params [String Integer] :review_score the rating of the review
19
+ # @option params [String] :utoken the token of the user who wrote the review (if one exists)
20
+ # @return [::Hashie::Rash] The new review with all of it's date
21
+ def create_review(params)
22
+ request = {
23
+ app_key: params[:app_key],
24
+ sku: params[:product_id],
25
+ domain: params[:shop_domain],
26
+ product_title: params[:product_title],
27
+ product_description: params[:product_description],
28
+ product_url: params[:product_url],
29
+ product_image_url: params[:product_image_url],
30
+ display_name: params[:user_display_name],
31
+ email: params[:user_email],
32
+ review_content: params[:review_body],
33
+ review_title: params[:review_title],
34
+ review_score: params[:review_score],
35
+ utoken: params[:utoken]
36
+ }
37
+ request.delete_if {|element, value| value.nil? }
38
+ get('/reviews/dynamic_create', request)
39
+ end
40
+
41
+
42
+ #
43
+ # Gets reviews of a specific product
44
+ #
45
+ # @param [Hash] params
46
+ # @option params [String] :app_key the app key of the account for which the review is created
47
+ # @option params [String] :product_id the id of the product
48
+ # @option params [Integer] :count the amount of reviews per page
49
+ # @option params [Integer] :page the page number
50
+ # @option params [String] :since_id the id from which to start retrieving reviews
51
+ # @option params [String] :since_date the date from which to start retrieving reviews
52
+ # @option params [String] :utoken the users utoken to get the reviews that are most relevant to that user
53
+ def get_product_reviews(params)
54
+ app_key = params[:app_key]
55
+ sku = params[:product_id]
56
+ request = {
57
+ page: params[:page] || 1,
58
+ count: params[:per_page] || 5,
59
+ since_date: params[:since_date],
60
+ since_id: params[:since_id],
61
+ utoken: params[:utoken]
62
+ }
63
+ request.delete_if{|key,val| val.nil? }
64
+ get("/products/#{app_key}/#{sku}/reviews", request)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,64 @@
1
+ module Yotpo
2
+ module User
3
+
4
+ #
5
+ # Registers a new User with Yotpo
6
+ #
7
+ # @param params [Hash] the user details hash
8
+ # @option params [String] :email the email of the user
9
+ # @option params [String] :display_name the display name of the user
10
+ # @option params [String] :first_name the first name of the user
11
+ # @option params [String] :last_name the last name of the user
12
+ # @option params [String] :website_name the name of the web site that the user owns
13
+ # @option params [String] :domain the domain of the web site that the user owns
14
+ # @option params [String] :url the url of the web site that the user owns
15
+ # @option params [String] :support_url the support url at the users site
16
+ # @option params [String] :callback_url the beginning of the callback url at the users site
17
+ # @return the new account hash
18
+ def create_user(params)
19
+ user = {
20
+ email: params[:email],
21
+ display_name: params[:display_name],
22
+ first_name: params[:first_name],
23
+ last_name: params[:last_name],
24
+ website_name: params[:website_name],
25
+ password: params[:password],
26
+ support_url: params[:support_url],
27
+ callback_url: params[:callback_url],
28
+ url: params[:url]
29
+ }
30
+ post('/users', {user: user})
31
+ end
32
+
33
+ #
34
+ # Retrieves an oauth bearer token from Yotpo
35
+ #
36
+ # @param params [Hash] the request details
37
+ # @option params [String] :client_id the app key received at the registration
38
+ # @option params [String] :client_secret app secret received at the registration
39
+ # @return [Hash] that includes access_token and the token_type
40
+ def get_oauth_token(params)
41
+ request = {
42
+ client_id: params[:app_key],
43
+ client_secret: params[:secret],
44
+ grant_type: "client_credentials"
45
+ }
46
+
47
+ response = post('/oauth/token', request)
48
+ return response
49
+ end
50
+
51
+ #
52
+ # To allow the login into www.yotpo.com using app_key and secret
53
+ # @param params [Hash] the request params
54
+ # @option params [String] :app_key that was received when registered to www.yotpo.com
55
+ # @option params [String] :secret that was received when registered to www.yotpo.com
56
+ def get_login_url(params)
57
+ request = {
58
+ app_key: params[:app_key],
59
+ secret: params[:secret]
60
+ }
61
+ get('/users/b2blogin', request)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,134 @@
1
+ require 'active_support/notifications'
2
+ require 'faraday'
3
+ require 'typhoeus'
4
+ require 'typhoeus/adapters/faraday'
5
+ require 'faraday_middleware'
6
+ require 'yotpo/core/response_parser'
7
+ require 'yotpo/version'
8
+ require 'yotpo/api/account'
9
+ require 'yotpo/api/account_platform'
10
+ require 'yotpo/api/product'
11
+ require 'yotpo/api/purchase'
12
+ require 'yotpo/api/reminder'
13
+ require 'yotpo/api/review'
14
+ require 'yotpo/api/user'
15
+
16
+ module Yotpo
17
+ class Client
18
+ include Yotpo::Account
19
+ include Yotpo::AccountPlatform
20
+ include Yotpo::Product
21
+ include Yotpo::Reminder
22
+ include Yotpo::Review
23
+ include Yotpo::User
24
+
25
+ include Yotpo::Purchase
26
+
27
+
28
+ #
29
+ # Creates a new instance of Yotpo::Client
30
+ #
31
+ # @param url [String] The url to yotpo api (https://api.yotpo.com)
32
+ # @param parallel_requests [Integer String] The maximum parallel request to do (5)
33
+ def initialize(url = 'https://api.yotpo.com', parallel_requests = 5)
34
+ @url = url
35
+ @parallel_requests = parallel_requests
36
+ end
37
+
38
+ #
39
+ # Does a GET request to the url with the params
40
+ #
41
+ # @param url [String] the relative path in the Yotpo API
42
+ # @param params [Hash] the url params that should be passed in the request
43
+ def get(url, params = {})
44
+ preform(url, :get, params: params) do
45
+ return connection.get(url, params)
46
+ end
47
+ end
48
+
49
+ #
50
+ # Does a POST request to the url with the params
51
+ #
52
+ # @param url [String] the relative path in the Yotpo API
53
+ # @param params [Hash] the body of the request
54
+ def post(url, params)
55
+ preform(url, :post, params: params) do
56
+ return connection.post(url, params)
57
+ end
58
+ end
59
+
60
+ #
61
+ # Does a PUT request to the url with the params
62
+ #
63
+ # @param url [String] the relative path in the Yotpo API
64
+ # @param params [Hash] the body of the request
65
+ def put(url, params)
66
+ preform(url, :put, params: params) do
67
+ return connection.put(url, params)
68
+ end
69
+ end
70
+
71
+ #
72
+ # Does a DELETE request to the url with the params
73
+ #
74
+ # @param url [String] the relative path in the Yotpo API
75
+ def delete(url)
76
+ preform(url, :delete) do
77
+ return connection.delete(url)
78
+ end
79
+ end
80
+
81
+ #
82
+ # Does a parallel request to the api for all of the requests in the block
83
+ #
84
+ # @example block
85
+ # Yotpo.in_parallel do
86
+ # Yotpo.create_review(review_params)
87
+ # Yotpo.update_account(account_params)
88
+ # end
89
+ def in_parallel
90
+ connection.in_parallel do
91
+ yield
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ #
98
+ # Preforms an HTTP request and notifies the ActiveSupport::Notifications
99
+ #
100
+ # @private
101
+ # @param url [String] the url to which preform the request
102
+ # @param type [String]
103
+ def preform(url, type, params = {}, &block)
104
+ ActiveSupport::Notifications.instrument 'Yotpo', request: type, url: url, params: params do
105
+ if connection.in_parallel?
106
+ block.call
107
+ else
108
+ block.call.body
109
+ end
110
+ end
111
+ end
112
+
113
+ #
114
+ # @return an instance of Faraday initialized with all that this gem needs
115
+ def connection
116
+ @connection ||= Faraday.new(url: @url, parallel_manager: Typhoeus::Hydra.new(max_concurrency: @parallel_requests), headers: {:yotpo_api_connector => Yotpo::VERSION}) do |conn|
117
+
118
+ conn.use Yotpo::ResponseParser
119
+
120
+ # Set the response to be rashified
121
+ conn.response :rashify
122
+
123
+ # Setting request and response to use JSON/XML
124
+ conn.request :oj
125
+ conn.response :oj
126
+
127
+ # Set to use instrumentals to get time logs
128
+ conn.use :instrumentation
129
+
130
+ conn.adapter :typhoeus
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,84 @@
1
+ require 'typhoeus/adapters/faraday'
2
+ require 'faraday_middleware/response_middleware'
3
+
4
+ module Yotpo
5
+ class ResponseParser < Faraday::Response::Middleware
6
+ def call(env)
7
+ # "env" contains the request
8
+ @app.call(env).on_complete do
9
+ body = false
10
+ if env[:status] == 200
11
+ body = env[:response].body.response || env[:response].body
12
+ elsif env[:response] && env[:response].body && env[:response].body.status
13
+ body = env[:response].body.status
14
+ end
15
+ env[:body] = body
16
+ end
17
+ end
18
+ end
19
+
20
+ #
21
+ # Response middleware that decodes the response body from JSON.
22
+ #
23
+ class ParseOj < FaradayMiddleware::ResponseMiddleware
24
+ dependency do
25
+ require 'oj' unless defined?(::Oj)
26
+ end
27
+
28
+ define_parser do |body|
29
+ ::Oj.load(body, mode: :compat) unless body.strip.empty?
30
+ end
31
+ end
32
+
33
+ # Request middleware that encodes the body as JSON.
34
+ #
35
+ # Processes only requests with matching Content-type or those without a type.
36
+ # If a request doesn't have a type but has a body, it sets the Content-type
37
+ # to JSON MIME-type.
38
+ #
39
+ # Doesn't try to encode bodies that already are in string form.
40
+ class EncodeOj < Faraday::Middleware
41
+ CONTENT_TYPE = 'Content-Type'.freeze
42
+ MIME_TYPE = 'application/json'.freeze
43
+
44
+ dependency do
45
+ require 'oj' unless defined?(::Oj)
46
+ end
47
+
48
+ def call(env)
49
+ match_content_type(env) do |data|
50
+ env[:body] = encode data
51
+ end
52
+ @app.call env
53
+ end
54
+
55
+ def encode(data)
56
+ ::Oj.dump data
57
+ end
58
+
59
+ def match_content_type(env)
60
+ if process_request?(env)
61
+ env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
62
+ yield env[:body] unless env[:body].respond_to?(:to_str)
63
+ end
64
+ end
65
+
66
+ def process_request?(env)
67
+ type = request_type(env)
68
+ has_body?(env) and (type.empty? or type == MIME_TYPE)
69
+ end
70
+
71
+ def has_body?(env)
72
+ body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
73
+ end
74
+
75
+ def request_type(env)
76
+ type = env[:request_headers][CONTENT_TYPE].to_s
77
+ type = type.split(';', 2).first if type.index(';')
78
+ type
79
+ end
80
+ end
81
+ end
82
+
83
+ Faraday.register_middleware :response, oj: Yotpo::ParseOj
84
+ Faraday.register_middleware :request, oj: Yotpo::EncodeOj
@@ -0,0 +1,3 @@
1
+ module Yotpo
2
+ VERSION = '0.0.2'
3
+ end