Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jstotz committed Feb 26, 2018
1 parent ebfe17e commit f3f9701
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 136 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -12,4 +12,5 @@ end

group :development do
gem 'pry'
gem 'byebug'
end
9 changes: 5 additions & 4 deletions Gemfile.lock
Expand Up @@ -2,7 +2,6 @@ PATH
remote: .
specs:
springboard-retail (4.1.1)
addressable (~> 2.2.8)
hashie
json (>= 1.7.4)
patron (= 0.4.18)
Expand All @@ -11,6 +10,7 @@ GEM
remote: https://rubygems.org/
specs:
addressable (2.2.8)
byebug (9.0.6)
codeclimate-test-reporter (1.0.8)
simplecov (<= 0.13)
coderay (1.1.0)
Expand All @@ -24,8 +24,8 @@ GEM
safe_yaml (~> 1.0.0)
diff-lcs (1.2.5)
docile (1.1.5)
hashie (3.5.5)
json (2.0.3)
hashie (3.5.7)
json (2.1.0)
method_source (0.8.2)
mime-types (2.4.3)
multi_json (1.11.0)
Expand Down Expand Up @@ -71,6 +71,7 @@ PLATFORMS
ruby

DEPENDENCIES
byebug
codeclimate-test-reporter (~> 1.0.0)
coveralls
pry
Expand All @@ -80,4 +81,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
1.14.4
1.15.1
70 changes: 70 additions & 0 deletions Guardfile
@@ -0,0 +1,70 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

# Note: The cmd option is now required due to the increasing number of ways
# rspec may be run, below are examples of the most common uses.
# * bundler: 'bundle exec rspec'
# * bundler binstubs: 'bin/rspec'
# * spring: 'bin/rspec' (This will use spring if running and you have
# installed the spring binstubs per the docs)
# * zeus: 'zeus rspec' (requires the server to be started separately)
# * 'just' rspec: 'rspec'

guard :rspec, cmd: "bundle exec rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)

# Feel free to open issues for suggestions and improvements

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)

# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)

# Rails files
rails = dsl.rails(view_extensions: %w(erb haml slim))
dsl.watch_spec_files_for(rails.app_files)
dsl.watch_spec_files_for(rails.views)

watch(rails.controllers) do |m|
[
rspec.spec.call("routing/#{m[1]}_routing"),
rspec.spec.call("controllers/#{m[1]}_controller"),
rspec.spec.call("acceptance/#{m[1]}")
]
end

# Rails config changes
watch(rails.spec_helper) { rspec.spec_dir }
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }

# Capybara features specs
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }

# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
end
end
25 changes: 9 additions & 16 deletions lib/springboard/client.rb
Expand Up @@ -30,7 +30,7 @@ class Client
DEFAULT_CONNECT_TIMEOUT = 10

##
# @return [Addressable::URI] The client's base URI
# @return [URI] The client's base URI
attr_reader :base_uri

##
Expand All @@ -40,7 +40,7 @@ class Client
# @option opts [String] :token Springboard API Token
def initialize(base_uri, opts={})
@base_uri = URI.parse(base_uri)
configure_session(base_uri, opts)
configure_session(opts)
end

##
Expand Down Expand Up @@ -80,7 +80,7 @@ def auth(opts={})
unless opts[:username] && opts[:password]
raise "Must specify :username and :password"
end
body = ::Addressable::URI.form_encode \
body = ::URI.encode_www_form \
:auth_key => opts[:username],
:password => opts[:password]
response = post '/auth/identity/callback', body,
Expand Down Expand Up @@ -178,9 +178,9 @@ def each_page(uri)
uri = URI.parse(uri)
total_pages = nil
page = 1
uri.query_values = {'per_page' => DEFAULT_PER_PAGE}.merge(uri.query_values || {})
uri = URIHelpers.reverse_merge_query_params(uri, 'per_page' => DEFAULT_PER_PAGE)
while total_pages.nil? or page <= total_pages
uri.merge_query_values! 'page' => page
uri = URIHelpers.merge_query_params(uri, 'page' => page)
response = get!(uri)
yield response
total_pages ||= response['pages']
Expand Down Expand Up @@ -208,7 +208,7 @@ def each(uri)
# @return [Integer] The subordinate resource count
def count(uri)
uri = URI.parse(uri)
uri.merge_query_values! 'page' => 1, 'per_page' => 1
uri = URIHelpers.merge_query_params(uri, 'page' => 1, 'per_page' => 1)
get!(uri)['total']
end

Expand All @@ -219,7 +219,7 @@ def prepare_request_body(body)
end

def make_request(method, uri, headers=false, body=false)
args = [prepare_uri(uri).to_s]
args = [uri.to_s]
args.push prepare_request_body(body) unless body === false
args.push headers unless headers === false
new_response session.__send__(method, *args)
Expand All @@ -234,18 +234,11 @@ def raise_on_fail(response)
response
end

def prepare_uri(uri)
uri = URI.parse(uri)
uri.path = uri.path.gsub(/^#{base_uri.path}/, '')
uri
end

def new_response(patron_response)
Response.new patron_response, self
end

def configure_session(base_url, opts)
session.base_url = base_url
def configure_session(opts)
session.headers['Content-Type'] = 'application/json'
session.headers['Authorization'] = "Bearer #{opts[:token]}" if opts[:token]
session.handle_cookies
Expand All @@ -260,4 +253,4 @@ def configure_session(base_url, opts)
require 'springboard/client/resource'
require 'springboard/client/response'
require 'springboard/client/body'
require 'springboard/client/uri'
require 'springboard/client/uri_helpers'
18 changes: 8 additions & 10 deletions lib/springboard/client/resource.rb
Expand Up @@ -19,9 +19,9 @@ class Resource
##
# The resource's URI.
#
# @return [Addressable::URI]
# @return [URI]
attr_reader :uri

##
# The underlying Springboard Client.
#
Expand All @@ -30,10 +30,10 @@ class Resource

##
# @param [Springboard::Client] client
# @param [Addressable::URI, #to_s] uri
# @param [URI, #to_s] uri
def initialize(client, uri)
@client = client
@uri = URI.join('/', uri.to_s)
@uri = URI.join(client.base_uri, client.base_uri.path + '/', uri)
end

##
Expand Down Expand Up @@ -115,8 +115,8 @@ def post!(body, headers=false); call_client(:post!, body, headers); end
# Returns a new subordinate resource with the given sub-path.
#
# @return [Resource]
def [](uri)
clone(self.uri.subpath(uri))
def [](subpath)
clone(URI.join(uri, uri.path + '/', subpath))
end

##
Expand Down Expand Up @@ -147,11 +147,9 @@ def [](uri)
# @return [Hash]
def query(params=nil)
if params
uri = self.uri.dup
uri.merge_query_values!(params)
clone(uri)
clone(URIHelpers.merge_query_params(uri, params))
else
self.uri.query_values || {}
URIHelpers.query_to_hash(uri)
end
end

Expand Down
96 changes: 0 additions & 96 deletions lib/springboard/client/uri.rb

This file was deleted.

51 changes: 51 additions & 0 deletions lib/springboard/client/uri_helpers.rb
@@ -0,0 +1,51 @@
require 'uri'

module Springboard
class Client
##
# A wrapper around URI
module URIHelpers
##
# Returns a new URI object with the given Hash of query string parameters
# merged into the existing query string parameters (if any).
#
# @return [URI]
def self.merge_query_params(uri, params)
new_uri = uri.dup
params = query_to_hash(uri).merge(stringify_keys(params))
new_uri.query = URI.encode_www_form(params)
new_uri
end

##
# Returns a new URI object with the existing Hash of query string
# parameters merged into the given Hash. The given hash is treated alias_method
# default params.
#
# @return [URI]
def self.reverse_merge_query_params(uri, default_params)
new_uri = uri.dup
params = stringify_keys(default_params).merge(query_to_hash(uri))
new_uri.query = URI.encode_www_form(params)
new_uri
end

##
# Returns a Hash of the query string parameters in the URI. If the URI
# has no query string parameters, returns an empty Hash.
#
# @return [Hash]
def self.query_to_hash(uri)
uri.query ? CGI.parse(uri.query) : {}
end

private

def self.stringify_keys(hash)
hash.each_with_object({}) do |(key, value), hash|
hash[key.to_s] = value.is_a?(Hash) ? stringify_keys(value) : value
end
end
end
end
end

0 comments on commit f3f9701

Please sign in to comment.