parix/gw2 — pull request #3 by nocash (2014-09-08 06:32:11+00:00)
Title: consolidation, refactoring and restructuring

Hello. I found this repo while looking for an GW2 API wrapper and did some work on it over the weekend. It's mostly consolidation, refactoring and restructuring with a few tweaks and added methods. I didn't make any major changes to the public interface, but the diff is pretty large due to the number of files touched by the refactoring. Because of this, I've included a summary of the more significant changes below. Let me know if you have any questions or feedback.

---

**Created [`ApiRequest`][] class** to DRY up use of `HTTPS` and `JSON` modules and make a place for shared methods. Converted endpoint modules to classes (`Item`, `Event`, etc.) so they could extend `ApiRequest`. Created the [`ApiRequest.get`][] method to encapsulate requests and parsing of responses to further DRY endpoint methods (`Map.all`, `Map.floor`, etc.).

[`ApiRequest`]: https://github.com/nocash/gw2/blob/develop/consolidation-refactor/lib/gw2/api_request.rb
[`ApiRequest.get`]: https://github.com/nocash/gw2/blob/develop/consolidation-refactor/lib/gw2/api_request.rb#L9-L13

**Switched from using `Net::HTTP` directly** to using `OpenURI.read` (a `Net::HTTP` wrapper) in order to [simplify code][simple-open-uri] and improve readability.

[simple-open-uri]: https://github.com/nocash/gw2/commit/d6fccf560a3b6a2e1004f4cf1c27b0e849e3a2fe#diff-f4622df51fc25757e2df7e9c38e8a4e4L26

**Consolidated API endpoint methods** into respective class files (i.e. definitions for both `Item.all` and `Item.details` can be found in [`lib/gw2/item.rb`][] instead of separately in `lib/gw2/item/items.rb` and `lib/gw2/item/item_details.rb`. This provides a cleaner and more complete view of each class' capabilities and responsibilities.

[`lib/gw2/item.rb`]: https://github.com/nocash/gw2/blob/develop/consolidation-refactor/lib/gw2/item.rb

**Consolidated specs into fewer files** to maintain parity with the code and for the same reasons as above.

**Created a [shared example][] for API request/response parsing** and [rewrote specs][] to use it. All of the tests (both before and after the refactor) are basically checking the same three things: 1) that a URI was created correctly, 2) a web request was made to the URI, and 3) the returned JSON data was correctly parsed and returned. What that JSON data consists of really doesn't matter for this purpose, so the detailed (and sometimes [very large][big-data]) sets of test data aren't necessary. In the future it could be good to add acceptance tests using VCR to capture and test against actual response data.

[shared example]: https://github.com/nocash/gw2/blob/develop/consolidation-refactor/spec/support/api_response_parser_examples.rb
[rewrote specs]: https://github.com/nocash/gw2/commit/42ae8919723f69101bd5aa1134d5b8cf3e729885?diff=split
[big-data]: https://github.com/parix/gw2/blob/bf4377d875639a9288160eacd204b292dd440ce2/spec/wvw/match_details_spec.rb#L7-L93

**Created [`ApiHelper`][] spec helper** and the `#stub_endpoint` method in order to simplify request stubs and DRY up code.

[`ApiHelper`]: https://github.com/nocash/gw2/blob/develop/consolidation-refactor/spec/support/api_helper.rb

**Extracted code to build API URIs into [`HTTPS.endpoint_uri`][]** to simplify code, improve readability and allow it to be reused in spec helpers.

[`HTTPS.endpoint_uri`]: https://github.com/nocash/gw2/blob/develop/consolidation-refactor/lib/gw2/https.rb#L7-L10

**Configured Rspec to [automatically include][] files under `spec/support`** to simplify the creation of additional helpers or shared examples.

[automatically include]: https://github.com/nocash/gw2/blob/79dbb20ea11a9033c5364372225742ce2370455e/spec/spec_helper.rb#L5

**Made various small changes to [`.gitignore`][] file:**

- Added entry for `coverage` directory.
- Added entry for `.ruby-gemset` file.
- Removed entry for `.DS_Store.` This file is specific to a developer's environment and is not generated by or related to the project. It should be placed in the `~/.gitignore` file instead.
- Appended slashes to directory entries. Not strictly necessary, but removes ambiguity from entries.
- Sorted entries alphabetically. This makes it easier to scan the file and remove duplicate entries. While not very beneficial for this particular project, I think it's a good habit.

[`.gitignore`]: https://github.com/nocash/gw2/blob/develop/consolidation-refactor/.gitignore

**Made various small changes to [Gemfile][]:**

- Added pry-byebug to Gemfile to assist with development and debugging.
- Sorted Gemfile entries alphabetically (see similar comment for `.gitignore` above for rationale).

[Gemfile]: https://github.com/nocash/gw2/blob/develop/consolidation-refactor/Gemfile

**Bumped version from "1.2.0" to "[1.2.1][]".** Despite a lot of refactoring there were no significant or compatibility-breaking changes to any public interface.

[1.2.1]: https://github.com/nocash/gw2/commit/5d6bae4da6d36d9d6257cdd27a4415c676a98b11