Skip to content

Commit

Permalink
Add tests and fix various things
Browse files Browse the repository at this point in the history
  • Loading branch information
sshaw committed Oct 24, 2024
1 parent fa3459c commit 9479fc3
Show file tree
Hide file tree
Showing 23 changed files with 1,258 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .env.test.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Used by VCR-based tests
BC_ACCESS_TOKEN=
BC_STORE_HASH=
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
/tmp/

# Used by dotenv library to load environment variables.
# .env
.env*
!.env*.example

# Ignore Byebug command history file.
.byebug_history
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ p attribute.meta.total # 1
- Call the appropriate HTTP verb method passing the endpoint's path (the portion **after** the API's `v3` URL) and parameters
1. If the method's return value should not be an `Array` call `unwrap(result)` before returning

## Testing

Tests use [VCR](https://github.com/vcr/vcr). If you need to re-record cassettes or create new ones a BigCommerce
account is with API access is required. See `.env.test.example`.

To re-record certain tests you must import fixture data into your store. See `etc/customers.csv`. These records can be deleted once
the VCR cassettes are recorded and you are done with development. The IDs they create are assumed by the tests which may present
a problem. Open an issue if so.

Any records that are created by the tests are deleted. Well, a delete is attempted in an `after` block, if something goes wrong with the test
the record(s) may remain in your store.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
Expand Down
2 changes: 2 additions & 0 deletions big_commerce-management_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ Gem::Specification.new do |spec|

spec.add_dependency "class2", ">= 0.6.0"
spec.add_development_dependency "vcr"
spec.add_development_dependency "webmock"
spec.add_development_dependency "dotenv"
end
1 change: 1 addition & 0 deletions lib/big_commerce/management_api/classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
{
"first_name": "string",
"last_name": "string",
"company": "string",
"address1": "Addr1",
"address2": "",
"city": "string",
Expand Down
17 changes: 16 additions & 1 deletion lib/big_commerce/management_api/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def get(options = {})
def upsert(*attributes)
attributes.flatten!

PUT(PATH, attributes.map(&:to_h))
PUT(PATH, attributes)
end
end

Expand Down Expand Up @@ -147,6 +147,21 @@ def initialize(*argz)
@metafields = Metafields.new(*argz)
end

def create(*customers)
customers.flatten!

POST(PATH, customers.map(&:to_h))
end

def delete(*ids)
ids.flatten!

DELETE(
PATH,
with_in_param({:id => ids}, :id)
)
end

def get(options = {})
GET(
PATH,
Expand Down
35 changes: 21 additions & 14 deletions lib/big_commerce/management_api/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def request_id
method.tr!("-", "_")

define_method(method) do
value = headers[header]
value = @headers[header]
# Net::HTTP returns as an array
value.is_a?(Array) ? value[0] : value
end
Expand All @@ -51,21 +51,31 @@ def initialize(data, headers)

def error_message(data)
#
# Looks for a structure like this and picks best message:
# Looks for a structure like one of these and picks best message:
#
# {"data"=>[],
# "errors"=>
# A:
#
# {"errors"=>
# [{"status"=>409,
# "title"=>"Cannot have multiple segments with the same name",
# "type"=>
# "https://developer.bigcommerce.com/api-docs/getting-started/api-status-codes",
# "errors"=>{}}]
#
#
# B:
#
# {"status"=>422,
# "title"=>"Set customer attribute values failed.",
# "type"=>
# "https://developer.bigcommerce.com/api-docs/getting-started/api-status-codes",
# "errors"=>{"0.data"=>"missing attribute value"}}
#
return data unless data.is_a?(Hash)

return data unless data.is_a?(Hash) && data["errors"]
data = data["errors"][0] if data["errors"].is_a?(Array)

data = data["errors"][0]
if data["errors"] && data["errors"].any?
if data["errors"].any?
errors = data["errors"].map { |property, message| "#{property}: #{message.chomp(".")}" }
errors.join(", ")
else
Expand Down Expand Up @@ -147,14 +157,14 @@ def DELETE(path, data = {})
path = endpoint(path)
path << query_string(data) if data && data.any?

request(Net::HTTP::Delete.new(path))
request(Net::HTTP::Delete.new(path), data)
end

def GET(path, data = {})
path = endpoint(path)
path << query_string(data) if data && data.any?

request(Net::HTTP::Get.new(path))
request(Net::HTTP::Get.new(path), data)
end

def POST(path, data = {})
Expand Down Expand Up @@ -206,15 +216,12 @@ def query_string(params)
value = params[name]

if value.is_a?(Array)
value = value.join(",")
params[name] = value.join(",")
elsif value.respond_to?(:strftime)
value = value.strftime("%Y-%m-%dT%H:%M:%S%z")
params[name] = value.strftime("%Y-%m-%dT%H:%M:%S%z")
end

params[name] = value
end

# TODO: do they want form or URL encoding?
sprintf("?%s", URI.encode_www_form(params))
end

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9479fc3

Please sign in to comment.