jirafe 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Jirafe Analytics Ruby Client
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/jirafe/jirafe-ruby-client.png)](http://travis-ci.org/jirafe/jirafe-ruby-client)
4
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/jirafe/jirafe-ruby-client)
4
5
 
5
6
  This is the official [Jirafe](http://jirafe.com/) rubygem.
6
7
  It fully wraps Jirafe's [API](http://api.jirafe.com).
@@ -17,6 +17,7 @@ module Jirafe
17
17
  autoload :Configuration, "jirafe/configuration.rb"
18
18
  autoload :Error, "jirafe/error.rb"
19
19
  autoload :ResponseParser, "jirafe/response_parser.rb"
20
+ autoload :Model, "jirafe/model.rb"
20
21
  autoload :Asset, "jirafe/asset.rb"
21
22
  autoload :Tracker, "jirafe/tracker.rb"
22
23
  module Resource
@@ -2,12 +2,6 @@ module Jirafe
2
2
  module Callback
3
3
  module Events
4
4
  module DataHelper
5
- def initialize(attributes = {})
6
- attributes.each do |attr, value|
7
- self.send("#{attr}=", value) if self.respond_to?("#{attr}=")
8
- end
9
- end
10
-
11
5
  def format_amount(amount)
12
6
  return nil if amount.nil?
13
7
  "%01.4f" % amount
@@ -2,6 +2,7 @@ module Jirafe
2
2
  module Callback
3
3
  module Events
4
4
  class Item
5
+ include Jirafe::Model
5
6
  include DataHelper
6
7
  attr_accessor :sku, :name, :price, :quantity, :category
7
8
 
@@ -2,6 +2,7 @@ module Jirafe
2
2
  module Callback
3
3
  module Events
4
4
  class Order < Jirafe::Callback::Event
5
+ include Jirafe::Model
5
6
  include DataHelper
6
7
  attr_accessor :identifier, :status, :customer, :visitor_identifier, :created_at,
7
8
  :grand_total, :sub_total, :tax_amount, :shipping_amount, :discount_amount, :items
@@ -2,6 +2,7 @@ module Jirafe
2
2
  module Callback
3
3
  module Events
4
4
  class Refund < Jirafe::Callback::Event
5
+ include Jirafe::Model
5
6
  include DataHelper
6
7
  attr_accessor :identifier, :order_identifier, :created_at,
7
8
  :grand_total, :sub_total, :tax_amount, :shipping_amount,
@@ -34,11 +34,6 @@ module Jirafe
34
34
  @test = value == true
35
35
  end
36
36
 
37
- def token
38
- raise Error::MissingToken if @token.nil?
39
- @token
40
- end
41
-
42
37
  def logger
43
38
  @logger ||= (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))
44
39
  end
@@ -0,0 +1,14 @@
1
+ module Jirafe
2
+ module Model
3
+ def initialize(attributes = {})
4
+ reinitialize(attributes)
5
+ end
6
+
7
+ def reinitialize(attributes = {})
8
+ attributes.each do |attr, value|
9
+ self.send("#{attr}=", value) if self.respond_to?("#{attr}=")
10
+ end
11
+ self
12
+ end
13
+ end
14
+ end
@@ -3,6 +3,7 @@ module Jirafe
3
3
  class JirafeResource
4
4
  include HTTParty
5
5
  include Jirafe::ResponseParser
6
+ include Jirafe::Model
6
7
 
7
8
  base_uri Jirafe.config.url
8
9
  format :json
@@ -18,14 +19,7 @@ module Jirafe
18
19
  return @attributes if @attributes && attrs === {}
19
20
  attrs.each_pair do |attr_name, options|
20
21
  attr_accessor attr_name
21
- if options.include?(:identifier)
22
- define_method attr_name do
23
- ivar = instance_variable_get("@#{attr_name}")
24
- ivar.nil? ? nil : ivar.to_i
25
- end
26
- alias_method :identifier, attr_name
27
- alias_method :identifier=, "#{attr_name}="
28
- end
22
+ define_identifier(attr_name) if options.include?(:identifier)
29
23
  end
30
24
  @attributes ||= attrs
31
25
  end
@@ -54,7 +48,6 @@ module Jirafe
54
48
  def list(*parent_identifiers)
55
49
  if parent_identifiers.last.is_a?(Hash)
56
50
  options = {:query => parent_identifiers.pop }
57
- options = parent_identifiers.pop
58
51
  end
59
52
  response = get(resource_url(nil, *parent_identifiers), options_with_token(options || {}))
60
53
  check_response_for_exception(response)
@@ -68,26 +61,37 @@ module Jirafe
68
61
  options = {:query => parent_identifiers.pop }
69
62
  end
70
63
  response = get(resource_url(identifier, *parent_identifiers), options_with_token(options || {}))
71
- check_response_for_exception(response)
72
- self.new(response.parsed_response)
64
+ handle_response(self.new, response)
73
65
  end
74
66
 
75
67
  def create(resource)
76
68
  resource_id, parent_identifiers = identifiers_from_resource(resource)
77
69
  response = post(resource_url(resource_id, *parent_identifiers), options_with_token({:body => resource.attributes_for_change(:create)}))
78
- check_response_for_exception(response)
79
- resource.reinitialize(response.parsed_response)
70
+ handle_response(resource, response)
80
71
  end
81
72
 
82
73
  def update(resource)
83
74
  resource_id, parent_identifiers = identifiers_from_resource(resource)
84
75
  response = put(resource_url(resource_id, parent_identifiers), options_with_token({:body => resource.attributes_for_change(:update)}))
85
- check_response_for_exception(response)
86
- resource.reinitalize(response.parsed_response)
76
+ handle_response(resource, response)
87
77
  end
88
78
 
89
79
  private
90
80
 
81
+ def define_identifier(attribute)
82
+ define_method attribute do
83
+ ivar = instance_variable_get("@#{attribute}")
84
+ ivar.nil? ? nil : ivar.to_i
85
+ end
86
+ alias_method :identifier, attribute
87
+ alias_method :identifier=, "#{attribute}="
88
+ end
89
+
90
+ def handle_response(resource, response)
91
+ check_response_for_exception(response)
92
+ resource.reinitialize(response.parsed_response)
93
+ end
94
+
91
95
  def identifiers_from_resource(resource)
92
96
  return [] if resource.nil? || !resource.respond_to?(:identifier)
93
97
  [resource.identifier] + identifiers_from_resource(resource.parent)
@@ -95,19 +99,11 @@ module Jirafe
95
99
 
96
100
  def options_with_token(options = {})
97
101
  query_options = options.delete(:query) || {}
98
- {:query => { :token => Jirafe.config.token }.merge(query_options) }.merge(options)
99
- end
100
- end
101
-
102
- def initialize(attributes = {})
103
- reinitialize(attributes)
104
- end
105
-
106
- def reinitialize(attributes = {})
107
- attributes.each do |attr, value|
108
- self.send("#{attr}=", value) if self.respond_to?("#{attr}=")
102
+ default_query = { :query => {} }
103
+ default_query[:query][:token] = Jirafe.config.token unless Jirafe.config.token.nil?
104
+ default_query[:query].merge!(query_options)
105
+ default_query.merge(options)
109
106
  end
110
- self
111
107
  end
112
108
 
113
109
  def attributes
@@ -1,3 +1,3 @@
1
1
  module Jirafe
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -122,16 +122,6 @@ describe Jirafe::Configuration do
122
122
  describe "#token" do
123
123
  subject { instance.token }
124
124
 
125
- context "when it is not set" do
126
- before { instance.reset! }
127
-
128
- it "should raise an error" do
129
- expect {
130
- subject
131
- }.to raise_error(Jirafe::Error::MissingToken)
132
- end
133
- end
134
-
135
125
  context "when it is set" do
136
126
  before { instance.token = "mytoken" }
137
127
  after { instance.reset! }
@@ -182,7 +172,6 @@ describe Jirafe::Configuration do
182
172
  instance.reset!
183
173
  instance.url.should == "https://api.jirafe.com/v1"
184
174
  instance.callback_url.should == "https://data.jirafe.com"
185
- expect { instance.token }.to raise_error(Jirafe::Error::MissingToken)
186
175
  end
187
176
  end
188
177
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jirafe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -226,6 +226,7 @@ files:
226
226
  - lib/jirafe/callback/jirafe_callback.rb
227
227
  - lib/jirafe/configuration.rb
228
228
  - lib/jirafe/error.rb
229
+ - lib/jirafe/model.rb
229
230
  - lib/jirafe/resource/application.rb
230
231
  - lib/jirafe/resource/jirafe_resource.rb
231
232
  - lib/jirafe/resource/resources.rb