Skip to content

Instantly share code, notes, and snippets.

@SeanLF
Last active November 23, 2017 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanLF/b3ab048b8c22f6cef318 to your computer and use it in GitHub Desktop.
Save SeanLF/b3ab048b8c22f6cef318 to your computer and use it in GitHub Desktop.
This code needs some loving - review the Ruby version and tell us what you would improve.
class ShopifyAPIClient
attr_accessor :shop_id
def initialize
@params = Hash.new
@params[:basic_auth] = {username: ENV['SECRET_API_KEY'], password: ''}
@api_url = "https://www.shopify.this.is.a.sample.com/"
end
def orders
orders_json = some_http_library.get("#{@api_url + @shop_id}/orders", @params)
return JSON.parse(orders_json)
end
def products
products_json = some_http_library.get("#{@api_url + @shop_id}/products", @params)
return JSON.parse(products_json)
end
def product(id)
product_json = some_http_library.get("#{@api_url + @shop_id}/products/#{id}", @params)
return JSON.parse(product_json)
end
end
@SeanLF
Copy link
Author

SeanLF commented Jan 19, 2016

  1. Change shop_id from an attribute reader to an attribute accessor. Remove the shop_id setter method (set_shop_id).
  2. Use an environment variable to store the secret API key to remove it from the source code. (ENV["SECRET_API_KEY"])
  3. Add an initialize method.
  4. In the initialize method, declare a params intance variable and set it to Hash.new. Set the params[:basic_auth] with the username and password. Declare an api_url instance variable and set it to "https://www.shopify.this.is.a.sample.com/".
  5. The last statement in a method in Ruby is implicitly returned. The return keyword can be omitted.
  6. Remove the first two lines from the last three methods.
  7. Use string interpolation to form the HTTP GET request URL.
  8. We will need to append @ to the params, api_url and shop_id variables when they are used outside of the initialize method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment