Skip to content

Commit

Permalink
Change Snitcher::API::Client to take the key as a param.
Browse files Browse the repository at this point in the history
The API key was a required parameter of the client and hiding it inside
of an options hash makes it possible to create a client without knowing
you have to set the api key. I believe that a required parameter should
be just that, a parameter and not an option in a hash.
  • Loading branch information
gaffneyc committed Oct 16, 2015
1 parent 8a91fad commit 8af709c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ require "snitcher/api"
agent = Snitcher::API.get_key("jane@example.com", "password")
```

Returns a the `api_key` key string.
Returns an authentication key to access the api.

### Setup

Initialize the API client directly with your api_key:
Initialize the API client directly with your api key:

```ruby
require "snitcher/api"

client = Snitcher::API::Client.new(api_key: "FOO")
client = Snitcher::API::Client.new("my_awesome_key")
```

### Listing Snitches
Expand Down
34 changes: 15 additions & 19 deletions lib/snitcher/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ class Snitcher::API::Client

# Public: Create a new Client
#
# options:
# api_key: Access key available at https://deadmanssnitch.com/account/keys
# api_endpoint: String URL of the DMS API connecting to
# timeout: Number of seconds to wait at most while making a request.
# key - Access key available at https://deadmanssnitch.com/account/keys.
#
# options
# endpoint - String URL of the DMS API connecting to.
# timeout - Number of seconds to wait at most when making a request.
#
# Example
#
# Initialize API client for user with api_key "abc123"
# @client = Snitcher::API::Client.new({api_key: "abc123"})
# => #<Snitcher::API::Client:0x007fa3750af418 @api_key=abc123,
# @api_endpoint=#<URI::HTTPS https://api.deadmanssnitch.com/v1/>>
# Initialize API client for user with api key "abc123"
# @client = Snitcher::API::Client.new("abc123")
#
def initialize(options = {})
api_endpoint = options[:api_endpoint] || DEFAULT_ENDPOINT
def initialize(key, options = {})
endpoint = options[:endpoint] || DEFAULT_ENDPOINT

@api_key = options[:api_key]
@api_endpoint = URI.parse(api_endpoint).freeze
@timeout = options.fetch(:timeout, 5.0)
@key = key
@endpoint = URI.parse(endpoint).freeze
@timeout = options.fetch(:timeout, 5.0)
end

# Public: List snitches on the account
Expand Down Expand Up @@ -315,14 +314,11 @@ def execute_request(request, options={})
open_timeout: @timeout,
read_timeout: @timeout,
ssl_timeout: @timeout,
use_ssl: @api_endpoint.scheme == "https",
use_ssl: @endpoint.scheme == "https",
}

host = @api_endpoint.host
port = @api_endpoint.port

Net::HTTP.start(host, port, http_options) do |http|
request.basic_auth(@api_key, "")
Net::HTTP.start(@endpoint.host, @endpoint.port, http_options) do |http|
request.basic_auth(@key, "")
request["User-Agent"] = user_agent

# All requests (with bodies) are made using JSON.
Expand Down
4 changes: 2 additions & 2 deletions spec/api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
describe Snitcher::API::Client do
let(:api_key) { "_caeEiZXnEyEzXXYVh2NhQ" }
let(:api_endpoint) { "#{scheme}#{api_url}/" }
let(:options) { { api_key: api_key, api_endpoint: api_endpoint } }
let(:client) { Snitcher::API::Client.new(options) }
let(:options) { { endpoint: api_endpoint } }
let(:client) { Snitcher::API::Client.new(api_key, options) }

let(:api_url) { "api.dms.dev:3000/v1" }
let(:stub_url) { /api\.dms\.dev/ }
Expand Down

0 comments on commit 8af709c

Please sign in to comment.