Skip to content

Commit

Permalink
Fixing rubocop issues
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Jul 8, 2022
1 parent dddd015 commit 6a92afe
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion web/app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def create

logger.info("Failed to create products: #{error}")
ensure
render(json: {success: success, error: error}, status: status_code)
render(json: { success: success, error: error }, status: status_code)
end
end
2 changes: 2 additions & 0 deletions web/app/services/application_service.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationService
def self.call(*args, **kwargs, &block)
new(*args, **kwargs, &block).call
Expand Down
7 changes: 5 additions & 2 deletions web/app/services/product_creator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ProductCreator < ApplicationService
attr_reader :count

Expand All @@ -12,21 +14,22 @@ class ProductCreator < ApplicationService
QUERY

def initialize(count:, session: ShopifyAPI::Context.active_session)
super()
@count = count
@session = session
end

def call
client = ShopifyAPI::Clients::Graphql::Admin.new(session: @session)

for i in 1..count
(1..count).each do |_i|
client.query(
query: CREATE_PRODUCTS_MUTATION,
variables: {
input: {
title: random_title,
variants: [{ price: random_price }],
}
},
}
)
end
Expand Down
10 changes: 5 additions & 5 deletions web/test/controllers/products_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ProductsControllerTestTest < ActionDispatch::IntegrationTest

test "handles creating products" do
token = JWT.encode(JWT_PAYLOAD, ShopifyAPI::Context.api_secret_key, "HS256")
stub = stub_request(:post, "https://regular-shop.myshopify.com/admin/api/2022-04/graphql.json")
stub_request(:post, "https://regular-shop.myshopify.com/admin/api/2022-04/graphql.json")
.with(headers: { "X-Shopify-Access-Token" => "access-token" })
.to_return(status: 200, body: JSON.dump({}))

Expand All @@ -43,23 +43,23 @@ class ProductsControllerTestTest < ActionDispatch::IntegrationTest
headers: { "HTTP_AUTHORIZATION": "Bearer #{token}" }

assert_response :success
assert_equal({"success" => true, "error" => nil}, JSON.parse(@response.body))
assert_equal({ "success" => true, "error" => nil }, JSON.parse(@response.body))
end
end

test "handles product creation errors" do
token = JWT.encode(JWT_PAYLOAD, ShopifyAPI::Context.api_secret_key, "HS256")
stub = stub_request(:post, "https://regular-shop.myshopify.com/admin/api/2022-04/graphql.json")
stub_request(:post, "https://regular-shop.myshopify.com/admin/api/2022-04/graphql.json")
.with(headers: { "X-Shopify-Access-Token" => "access-token" })
.to_return(status: 400, body: JSON.dump({errors: "Something went wrong"}))
.to_return(status: 400, body: JSON.dump({ errors: "Something went wrong" }))

ShopifyAPI::Utils::SessionUtils.stub(:load_current_session, SESSION) do
get "/api/products/create",
xhr: true,
headers: { "HTTP_AUTHORIZATION": "Bearer #{token}" }

assert_response :bad_request
assert_equal({"success" => false, "error" => "Something went wrong"}, JSON.parse(@response.body))
assert_equal({ "success" => false, "error" => "Something went wrong" }, JSON.parse(@response.body))
end
end

Expand Down

0 comments on commit 6a92afe

Please sign in to comment.