Skip to content

Commit

Permalink
Code formatting changes (guided by Rubocop)
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Lindahl committed Mar 27, 2015
1 parent 3391c0c commit 81c148d
Show file tree
Hide file tree
Showing 40 changed files with 348 additions and 228 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
insert_final_newline = true
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
vendor
vendor
2 changes: 1 addition & 1 deletion .irbrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require 'bundler'
Bundler.require
require 'awesome_print'
require 'awesome_print'
45 changes: 45 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
AllCops:
Exclude:
- '*.gemspec'

Metrics/ClassLength:
Max: 120

Metrics/LineLength:
Max: 100

Metrics/MethodLength:
Max: 15

Style/AccessModifierIndentation:
Enabled: false

Style/ClassVars:
Enabled: false

Style/ConstantName:
Enabled: false

Style/Documentation:
Enabled: false

Style/NegatedIf:
Enabled: false

Style/RaiseArgs:
Enabled: false

Style/SpaceAfterColon:
Enabled: false

Style/SpaceBeforeBlockBraces:
Enabled: false

Style/SpaceInsideBlockBraces:
Enabled: false

Style/SpaceInsideParens:
Enabled: false

Style/TrivialAccessors:
Enabled: false
2 changes: 1 addition & 1 deletion Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ end
appraise 'faraday-09' do
gem 'faraday', '~> 0.9.0'
gem 'faraday_middleware', '~> 0.9.0'
end
end
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Initializing an API client is really easy:
class MyApiClient
# Arbitrary example
def self.api
@api ||= Frenetic.new( url:'http://example.com/api' )
@api ||= Frenetic.new(url:'http://example.com/api')
end
end
```
Expand All @@ -152,7 +152,7 @@ At the bare minimum, Frenetic only needs to know what the URL of your API is.
Configuring Frenetic can be done during instantiation:

```ruby
Frenetic.new( url:'http://example.com', api_token:'123bada55k3y' )
Frenetic.new(url:'http://example.com', api_token:'123bada55k3y')
```

Or with a block:
Expand All @@ -168,7 +168,7 @@ end
Or both...

```ruby
f = Frenetic.new( url:'http://example.com' )
f = Frenetic.new(url:'http://example.com')
f.configure do |cfg|
cfg.api_token = '123bada55key'
end
Expand All @@ -184,13 +184,13 @@ middleware.
To use Basic Auth, simply configure Frenetic with a `username` and `password`:

```ruby
Frenetic.new( url:url, username:'user', password:'password' )
Frenetic.new(url:url, username:'user', password:'password')
```

If your API uses an App ID and API Key pair, you can pass those as well:

```ruby
Frenetic.new( url:url, app_id:'123abcSHA1', api_key:'bada55SHA1k3y' )
Frenetic.new(url:url, app_id:'123abcSHA1', api_key:'bada55SHA1k3y')
```

The `app_id` and `api_key` values are simply aliases to `username` and
Expand All @@ -201,7 +201,7 @@ The `app_id` and `api_key` values are simply aliases to `username` and
To use Token Auth, simply configure Frenetic with your token:

```ruby
Frenetic.new( url:url, api_token:'bada55SHA1t0k3n' )
Frenetic.new(url:url, api_token:'bada55SHA1t0k3n')
```


Expand All @@ -214,7 +214,7 @@ If configured to do so, Frenetic will autotmatically cache API responses.
##### Rack::Cache

```ruby
Frenetic.new( url:url, cache: :rack )
Frenetic.new(url:url, cache: :rack)
```

Passing in a cache option of `:rack` will cause Frenetic to use Faraday's
Expand Down Expand Up @@ -246,14 +246,14 @@ By default, Frenetic is configured to use Faraday's default adapter (usually
Net::HTTP). You can change this with the `adapter` option:

```ruby
Frenetic.new( url:url, adapter: :patron )
Frenetic.new(url:url, adapter: :patron)
```

Frenetic accepts any of the [Faraday adapter shortcuts][adapters], or an instance
of the adapter itself:

```ruby
Frenetic.new( url:url, adapter:Faraday::Adapter::Patron )
Frenetic.new(url:url, adapter:Faraday::Adapter::Patron)
```


Expand All @@ -263,7 +263,7 @@ If you have no control over the API, you can explicitly tell Frenetic how long
to cache the API description for:

```ruby
Frenetic.new( url:url, default_root_cache_age:1.hour )
Frenetic.new(url:url, default_root_cache_age:1.hour)
```


Expand All @@ -273,7 +273,7 @@ Frenetic.new( url:url, default_root_cache_age:1.hour )
Frenetic will yield its internal Faraday connection during initialization:

```ruby
Frenetic.new( url:url ) do |builder|
Frenetic.new(url:url) do |builder|
# `builder` is the Faraday Connection instance with which you can
# add additional Faraday Middlewares or tweak the configuration.
end
Expand All @@ -293,7 +293,7 @@ A Frenetic instance supports any HTTP verb that [Faraday][faraday] has
impletented. This includes GET, POST, PUT, PATCH, and DELETE.

```ruby
api = Frenetic.new( url:url )
api = Frenetic.new(url:url)

api.get '/my_things/1'
# { 'id' => 1, 'name' => 'My Thing', '_links' => { 'self' { 'href' => '/api/my_things/1' } } }
Expand All @@ -314,8 +314,8 @@ class Order < Frenetic::Resource
api_client { MyAPI }

# TODO: Write a better example for this.
def self.find_all_by_name( name )
api.get( search_url(name) ) and response.success?
def self.find_all_by_name(name)
api.get(search_url(name)) and response.success?
end
end
```
Expand Down Expand Up @@ -411,7 +411,7 @@ stub out all of the HTTP requests with something like WebMock or VCR, or you can
use Frenetic in `test_mode`

```ruby
Frenetic.new( url:url, test_mode:true )
Frenetic.new(url:url, test_mode:true)
# ...or...
api = Frenetic.new(url:url)
api.config.test_mode = true
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
23 changes: 13 additions & 10 deletions lib/frenetic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Frenetic
include ActiveSupport::Configurable
include BrieflyMemoizable

MaxAge = /max-age=(?<max_age>\d+)/

config_accessor :adapter
config_accessor :api_token
config_accessor :cache
Expand Down Expand Up @@ -49,12 +51,12 @@ class Frenetic
url: nil,
username: nil
}
self.config.merge!(@@defaults)
config.merge!(@@defaults)

# PENDING: [ActiveSupport4] Remove merge with class defaults
def initialize(cfg = {})
self.config.merge!(cfg.reverse_merge(self.class.config))
yield self.config if block_given?
config.merge!(cfg.reverse_merge(self.class.config))
yield config if block_given?
end

def connection
Expand All @@ -74,10 +76,10 @@ def configure
#
# If no Cache-Control header is returned, then the results are not memoized.
def description
if response = get(config.url.to_s) and response.success?
@description_age = cache_control_age(response.headers)
response.body
end
response = get(config.url.to_s)
return unless response.success?
@description_age = cache_control_age(response.headers)
response.body
end
briefly_memoize :description

Expand All @@ -92,11 +94,12 @@ def reset_connection!
private

def cache_control_age(headers)
if cache_age = headers['Cache-Control']
age = cache_age.match(%r{max-age=(?<max_age>\d+)})[:max_age]
cache_age = headers['Cache-Control']
if cache_age
age = cache_age.match(MaxAge)[:max_age]
Time.now + age.to_i
else
config.default_root_cache_age
end
end
end
end
16 changes: 9 additions & 7 deletions lib/frenetic/briefly_memoizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ module BrieflyMemoizable
extend ActiveSupport::Concern

module ClassMethods
def briefly_memoize( symbol )
def briefly_memoize(symbol)
original_method = "_unmemoized_#{symbol}".to_sym
memoized_ivar = "@#{symbol}"
age_ivar = "@#{symbol}_age"
memoized_ivar = "@#{symbol}"
age_ivar = "@#{symbol}_age"

class_eval <<-EOS
# rubocop:disable Metrics/LineLength
class_eval <<-CODE
if method_defined?(:#{original_method}) # if method_defined?(:_unmemoized_mime_type)
raise "Already memoized #{symbol}" # raise "Already memoized mime_type"
fail "Already memoized #{symbol}" # fail "Already memoized mime_type"
end # end
alias #{original_method} #{symbol} # alias _unmemoized_mime_type mime_type
Expand All @@ -27,8 +28,9 @@ def #{symbol}(*args) # def m
def reload_#{symbol}! # def reload_mime_type!
#{memoized_ivar} = nil # @mime_type = nil
end # end
EOS
CODE
# rubocop:enable Metrics/LineLength
end
end
end
end
end
9 changes: 4 additions & 5 deletions lib/frenetic/concerns/collection_rest_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ class Frenetic
module CollectionRestMethods
extend ActiveSupport::Concern

def get( id )
if response = api.get( member_url(id) ) and response.success?
@resource_class.new response.body
end
def get(id)
response = api.get(member_url(id))
@resource_class.new(response.body) if response.success?
end
end
end
end
21 changes: 12 additions & 9 deletions lib/frenetic/concerns/hal_linked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,29 @@ def links
@params['_links']
end

def member_url( params = {} )
def member_url(params = {})
resource = @resource_type || self.class.to_s.demodulize.underscore
link = links[resource] || links['self'] or raise HypermediaError, %Q{No Hypermedia GET Url found for the resource "#{resource}"}
HypermediaLinkSet.new( link ).href params
link = links[resource] || links['self']
fail MissingResourceUrl.new(resource) if !link
HypermediaLinkSet.new(link).href params
end

module ClassMethods
def links
api.description['_links']
end

def member_url( params = {} )
link = links[namespace] or raise HypermediaError, %Q{No Hypermedia GET Url found for the resource "#{namespace}"}
HypermediaLinkSet.new( link ).href params
def member_url(params = {})
link = links[namespace]
fail MissingResourceUrl.new(namespace) if !link
HypermediaLinkSet.new(link).href params
end

def collection_url
link = links[namespace.pluralize] or raise HypermediaError, %Q{No Hypermedia GET Url found for the resource "#{namespace.pluralize}"}
HypermediaLinkSet.new( link ).href
link = links[namespace.pluralize]
fail MissingResourceUrl.new(namespace.pluralize) if !link
HypermediaLinkSet.new(link).href
end
end
end
end
end
17 changes: 7 additions & 10 deletions lib/frenetic/concerns/member_rest_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ module MemberRestMethods
extend ActiveSupport::Concern

module ClassMethods
def find( params )
params = { id:params } unless params.is_a? Hash
def find(params)
params = { id:params } unless params.is_a?(Hash)
return as_mock(params) if test_mode?
if response = api.get( member_url(params) ) and response.success?
new response.body
end
response = api.get(member_url(params))
new(response.body) if response.success?
end

def all
return [] if test_mode?

if response = api.get( collection_url ) and response.success?
Frenetic::ResourceCollection.new self, response.body
end
response = api.get(collection_url)
Frenetic::ResourceCollection.new(self, response.body) if response.success?
end
end
end
end
end
Loading

0 comments on commit 81c148d

Please sign in to comment.