Skip to content

Commit

Permalink
Fix linter offences
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Apr 29, 2017
1 parent efc7b22 commit f6b0b15
Show file tree
Hide file tree
Showing 22 changed files with 82 additions and 72 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Version 0.6.0
* Includes the start of a basic library of pre-built reports (require 'garb/reports')

Version 0.5.1

* Brings back hash filters and symbol operators after agreed upon SymbolOperator

Version 0.5.0
Expand All @@ -82,7 +82,7 @@ Version 0.5.0
* The method of passing a hash to filters no longer works, at all

Version 0.4.0

* Changes the api for filters and sort making it consistent with metrics/dimensions
* If you wish to clear the defaults defined on a class, you may use clear_(filters/sort/metrics/dimensions)
* To make a custom class using Garb::Resource, you must now extend instead of include the module
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Important Changes
It works only with version 3 of Google API.

Please read CHANGELOG.

Description
-----------

Expand All @@ -39,7 +39,7 @@ Single User Login
Garb::Session.api_key = api_key # required for 2-step authentication
Garb::Session.login(username, password)
```

OAuth Access Token
------------------

Expand Down Expand Up @@ -143,7 +143,7 @@ does_not_contain => '!~',
substring => '=@',
not_substring => '!@'
```

Given the previous Exits example report in shorthand, we can add an option for filter:

```ruby
Expand Down Expand Up @@ -175,7 +175,7 @@ OPEN / READ TIMEOUT
Garb.open_timeout = 3
Garb.read_timeout = 3
```

TODOS
-----

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require 'rake/testtask'
$:.unshift File.expand_path('../lib', __FILE__)
require 'garb'

task :default => :test
task default: :test

Rake::TestTask.new do |t|
t.libs << 'test'
Expand Down
4 changes: 2 additions & 2 deletions garb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
gem.email = ['tony.pitale@viget.com', 'sija@sija.pl']
gem.homepage = 'http://github.com/Sija/garb'
gem.license = 'MIT'

gem.summary = 'Google Analytics API Ruby Wrapper'
gem.description = ''

Expand All @@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
gem.name = 'garb'
gem.require_paths = ['lib']
gem.version = Garb::VERSION

gem.add_dependency 'activesupport', '>= 2.2'
gem.add_dependency 'multi_json', '>= 1.3'
end
4 changes: 2 additions & 2 deletions lib/garb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class << self
end

def use_fibers=(val)
if val and (!defined?(EM) || !defined?(Fiber))
if val && (!defined?(EM) || !defined?(Fiber))
raise ArgumentError, 'Eventmachine and Fibers required (Ruby 1.9+ only)'
end
require 'em-net-http' if val
Expand Down Expand Up @@ -78,7 +78,7 @@ def log(str, level = :debug)
def to_google_analytics(thing)
return thing.to_google_analytics if thing.respond_to? :to_google_analytics

"#{$1}ga:#{$2}" if "#{thing.to_s.camelize(:lower)}" =~ /^(-)?(.*)$/
"#{$1}ga:#{$2}" if thing.to_s.camelize(:lower) =~ /^(-)?(.*)$/
end
alias to_ga to_google_analytics

Expand Down
1 change: 1 addition & 0 deletions lib/garb/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def ga_attribute(*keys)
end

private

def define_a_method_for(key, custom_key = nil)
custom_key ||= key.to_s.camelize(:lower)
define_method(key) do
Expand Down
10 changes: 5 additions & 5 deletions lib/garb/core_ext/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
class String
def camelize(first_letter = :upper)
case first_letter
when :upper then Garb::Inflector.camelize(self, true)
when :lower then Garb::Inflector.camelize(self, false)
when :upper then Garb::Inflector.camelize(self, true)
when :lower then Garb::Inflector.camelize(self, false)
end
end
alias camelcase camelize
Expand Down Expand Up @@ -33,9 +33,9 @@ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
def underscore(camel_cased_word)
word = camel_cased_word.to_s.dup
word.gsub!(/::/, '/')
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
word.tr!("-", "_")
word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
word.tr!('-', '_')
word.downcase!
word
end
Expand Down
32 changes: 16 additions & 16 deletions lib/garb/core_ext/symbol.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
module SymbolOperatorMethods
OPERATORS = {
:eql => '==',
:not_eql => '!=',
:gt => '>',
:gte => '>=',
:lt => '<',
:lte => '<=',
:matches => '==',
eql: '==',
not_eql: '!=',
gt: '>',
gte: '>=',
lt: '<',
lte: '<=',
matches: '==',

:does_not_match => '!=',
:contains => '=~',
:does_not_contain => '!~',
:substring => '=@',
:not_substring => '!@',
does_not_match: '!=',
contains: '=~',
does_not_contain: '!~',
substring: '=@',
not_substring: '!@',

:desc => '-',
:descending => '-'
}
desc: '-',
descending: '-'
}.freeze
SLUGS = OPERATORS.keys.freeze

def to_google_analytics
t = Garb.to_google_analytics @field || @target
o = OPERATORS[@operator]

[:desc, :descending].include?(@operator) ? "#{o}#{t}" : "#{t}#{o}"
%i[desc descending].include?(@operator) ? "#{o}#{t}" : "#{t}#{o}"
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/garb/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Garb
class Error < StandardError; end
class MissingCertFileError < Error; end
class AuthError < Error; end

class ClientError < Error
attr_reader :code, :message, :errors, :uri

Expand All @@ -13,6 +14,7 @@ def to_s
"#{code ? "[#{code}] #{message}" : message} : #{uri}"
end
end

class BadRequestError < ClientError; end
class InvalidCredentialsError < ClientError; end
class InsufficientPermissionsError < ClientError; end
Expand Down
14 changes: 9 additions & 5 deletions lib/garb/filter_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ def initialize(parameters)
end

def to_params
value = array_to_params(self.parameters)
value.empty? ? {} : {'filters' => value}
value = array_to_params(parameters)
value.empty? ? {} : { 'filters' => value }
end

private

def array_to_params(arr)
arr.map do |param|
case param
Expand All @@ -22,13 +23,16 @@ def array_to_params(arr)
end

def hash_to_params(hsh)
hsh.map do |k, v|
hsh = hsh.map do |k, v|
next unless k.is_a?(SymbolOperatorMethods)

escaped_v = v.to_s.gsub(/([,;])/) { |c| '\\' + c }
escaped_k = k.to_google_analytics.gsub(/([<>=])/) { |c| CGI.escape(c) }
escaped_v = v.to_s.gsub(/([,;])/) { |c| '\\' + c }

"#{escaped_k}#{CGI.escape(escaped_v)}"
end.join('%3B') # Hash AND (no duplicate keys), escape char for ';' fixes oauth
end
# Hash AND (no duplicate keys), escape char for ';' fixes oauth
hsh.join('%3B')
end
end
end
2 changes: 1 addition & 1 deletion lib/garb/management/feed.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Garb
module Management
class Feed
BASE_URL = 'https://www.googleapis.com/analytics/v3/management'
BASE_URL = 'https://www.googleapis.com/analytics/v3/management'.freeze

attr_reader :request

Expand Down
2 changes: 1 addition & 1 deletion lib/garb/management/segment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Segment
include PathAttribute

attr_reader :session
ga_attribute :name, :definition, :id => 'segmentId'
ga_attribute :name, :definition, id: 'segmentId'

def self.all(session = Session)
feed = Feed.new(session, '/segments')
Expand Down
21 changes: 11 additions & 10 deletions lib/garb/model.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Garb
module Model
MONTH = 2592000
URL = 'https://www.googleapis.com/analytics/v3/data/ga'
MONTH = (60 * 60 * 24 * 30).freeze
URL = 'https://www.googleapis.com/analytics/v3/data/ga'.freeze

def self.extended(base)
ProfileReports.add_report_method(base)
Expand Down Expand Up @@ -63,14 +63,15 @@ def all_results(profile, options = {})
end

private

def send_request_for_data(profile, params)
request = Request::Data.new(profile.session, URL, params)
response = request.send_request
response.body
end

def build_params(param_set)
param_set.inject({}) { |p,i| p.merge i }.reject { |_,v| v.nil? }
param_set.inject({}) { |p, i| p.merge i }.reject { |_, v| v.nil? }
end

def parse_filters(options)
Expand All @@ -79,23 +80,23 @@ def parse_filters(options)

def parse_segment(options)
# dirty hack to support dynamic segments
if options.has_key?(:segment_id)
if options.key?(:segment_id)
segment = "gaid::#{options[:segment_id]}"
elsif options.has_key?(:dynamic_segment)
elsif options.key?(:dynamic_segment)
filters = FilterParameters.new(options[:dynamic_segment])
segment = "dynamic::#{filters.to_params['filters']}"
end
{'segment' => segment}
{ 'segment' => segment }
end

def parse_sort(options)
sort = ReportParameter.new(:sort)
sort << options[:sort] if options.has_key?(:sort)
sort << options[:sort] if options.key?(:sort)
sort
end

def parse_sampling_level(options)
{ 'samplingLevel' => options.has_key?(:sampling_level) ? options[:sampling_level].to_s : 'default' }
{ 'samplingLevel' => (options[:sampling_level] || :default).to_s }
end

def build_default_params(profile, start_date, end_date)
Expand All @@ -108,8 +109,8 @@ def build_default_params(profile, start_date, end_date)

def build_page_params(options)
params = {}
params['max-results'] = options[:limit] if options.has_key? :limit
params['start-index'] = options[:offset] if options.has_key? :offset
params['max-results'] = options[:limit] if options.key? :limit
params['start-index'] = options[:offset] if options.key? :offset
params
end

Expand Down
2 changes: 1 addition & 1 deletion lib/garb/profile_reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.add_report_method(klass)
# demodulize leaves potential to redefine
# these methods given different namespaces
method_name = klass.name.to_s.demodulize.underscore
return unless method_name.length > 0
return if method_name.empty?

class_eval <<-CODE
def #{method_name}(opts = {}, &block)
Expand Down
5 changes: 2 additions & 3 deletions lib/garb/report_parameter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Garb
class ReportParameter

attr_reader :elements

def initialize(name)
Expand All @@ -18,8 +17,8 @@ def <<(element)
end

def to_params
value = self.elements.map { |param| Garb.to_google_analytics(param) }.join(',')
value.empty? ? {} : {self.name => value}
value = elements.map { |param| Garb.to_google_analytics(param) }.join(',')
value.empty? ? {} : { name => value }
end
end
end
2 changes: 1 addition & 1 deletion lib/garb/report_response.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Garb
class ReportResponse

def initialize(response_body, instance_klass = OpenStruct)
@response_body = response_body
@instance_klass = instance_klass
Expand All @@ -24,6 +23,7 @@ def sampled?
end

private

def keys
@keys ||= column_headers.map { |header| Garb.from_ga header['name'] }
end
Expand Down
2 changes: 1 addition & 1 deletion lib/garb/request/authentication.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Garb
module Request
class Authentication
URL = 'https://www.google.com/accounts/ClientLogin'
URL = 'https://www.google.com/accounts/ClientLogin'.freeze

def initialize(email, password, opts = {})
@email = email
Expand Down
Loading

0 comments on commit f6b0b15

Please sign in to comment.