delighted 1.0.0.beta → 1.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4bc320d60146375054cb83726a1bd7df4624d149
4
- data.tar.gz: 6d467dc9ac6662f2a9ae2f045008276bfc1eaa08
3
+ metadata.gz: 3328aa5e8d5da9e0aa18d14b249638e8fa18fbab
4
+ data.tar.gz: ed423baff436f425bdbc72a531bd337ae890b84a
5
5
  SHA512:
6
- metadata.gz: ef65c316539d20f8487489b08cb9da594dfe55fe386da5a5132a42759c73b6777eee60cba895009cf314da1f1ab8eaa7ce88dd89f7f7cc13ac9e55059e8b046b
7
- data.tar.gz: bcfd565b6dec672678975cd4e0d1b138d03566bb1977c6242ebf5712813bd77ac83c38608422a8b95424a4c9d961cb04ac38b5edf2e97ba54b0f3e475856e1f7
6
+ metadata.gz: f63f19f6995a497ff70d0cd0265cfaef551cc4617a2640ff88e2803095c0f71b35f1609fc8cb91759920335f61abff8d608ef0d78f3e18bb4f5bb13fb23272a5
7
+ data.tar.gz: cc0421d791025dfcc08564b5efcc1bc46095790cc7eae3faee3812b9e11a2e34ba66eb85b9e8a0831787535b0f55847070e1f99fcfa63ba0bfcba758bf492517
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Delighted API client for Ruby [![Build Status](https://travis-ci.org/delighted/delighted-ruby.png)](https://travis-ci.org/delighted/delighted-ruby)
2
2
 
3
3
 
4
- Official Ruby client for the [Delighted API](https://delightedapp.com).
4
+ Official Ruby client for the [Delighted](https://delightedapp.com) API.
5
5
 
6
6
  For installation and usage instructions, please [sign in to your Delighted account](https://delightedapp.com/signin) and follow the API documentation under Settings.
7
7
 
data/lib/delighted.rb CHANGED
@@ -3,13 +3,13 @@ require 'uri'
3
3
  require 'cgi'
4
4
  require 'multi_json'
5
5
  require 'set'
6
+ require 'thread'
6
7
 
7
8
  require 'delighted/utils'
8
9
  require 'delighted/json'
9
10
 
10
11
  require 'delighted/enumerable_resource_collection'
11
12
  require 'delighted/resource'
12
- require 'delighted/operations'
13
13
  require 'delighted/operations/all'
14
14
  require 'delighted/operations/create'
15
15
  require 'delighted/operations/retrieve'
@@ -19,7 +19,21 @@ require 'delighted/resources/person'
19
19
  require 'delighted/resources/survey_responses'
20
20
 
21
21
  require 'delighted/errors'
22
- require 'delighted/resource_interface'
23
22
  require 'delighted/http_response'
24
23
  require 'delighted/http_adapter'
25
24
  require 'delighted/client'
25
+
26
+ module Delighted
27
+ @mutex = Mutex.new
28
+
29
+ class << self
30
+ attr_accessor :api_key, :api_base_url, :http_adapter
31
+ attr_writer :shared_client
32
+
33
+ def shared_client
34
+ @mutex.synchronize do
35
+ @shared_client ||= Client.new(:api_key => api_key, :api_base_url => api_base_url, :http_adapter => http_adapter)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -4,19 +4,11 @@ module Delighted
4
4
  DEFAULT_HTTP_ADAPTER = HTTPAdapter.new
5
5
 
6
6
  def initialize(opts = {})
7
- @api_key = opts[:api_key] or raise ArgumentError, "You must pass an :api_key."
7
+ @api_key = opts[:api_key] or raise ArgumentError, "You must provide an API key by setting Delighted.api_key = '123abc' or passing { :api_key => '123abc' } when instantiating Delighted::Client.new"
8
8
  @api_base_url = opts[:api_base_url] || DEFAULT_API_BASE_URL
9
9
  @http_adapter = opts[:http_adapter] || DEFAULT_HTTP_ADAPTER
10
10
  end
11
11
 
12
- Resource.resources.each do |resource|
13
- class_eval <<-END, __FILE__, __LINE__
14
- def #{resource.interface_name}
15
- @#{resource.interface_name} ||= ResourceInterface.new(self, #{resource.name})
16
- end
17
- END
18
- end
19
-
20
12
  def get_json(path, params = {})
21
13
  headers = default_headers.dup.merge('Accept' => 'application/json')
22
14
 
@@ -2,20 +2,16 @@ module Delighted
2
2
  module Operations
3
3
  module All
4
4
  def self.included(klass)
5
- klass.extend(AuxiliaryClassMethods)
6
- Operations.add_operations(klass, OperationClassMethods)
5
+ klass.extend(ClassMethods)
7
6
  end
8
7
 
9
- module OperationClassMethods
10
- def all(client, opts = {})
8
+ module ClassMethods
9
+ def all(opts = {}, client = Delighted.shared_client)
10
+ opts = Utils.serialize_values(opts)
11
11
  json = client.get_json(path, opts)
12
12
  EnumerableResourceCollection.new(json.map { |attributes| new(attributes) })
13
13
  end
14
14
  end
15
-
16
- module AuxiliaryClassMethods
17
- # pagination, enumerable stuff etc.
18
- end
19
15
  end
20
16
  end
21
17
  end
@@ -2,12 +2,13 @@ module Delighted
2
2
  module Operations
3
3
  module Create
4
4
  def self.included(klass)
5
- Operations.add_operations(klass, OperationClassMethods)
5
+ klass.extend(ClassMethods)
6
6
  end
7
7
 
8
- module OperationClassMethods
9
- def create(client, attributes = {})
10
- params = Utils.hash_removing_key(attributes, :id)
8
+ module ClassMethods
9
+ def create(attributes = {}, client = Delighted.shared_client)
10
+ params = Utils.hash_without_key(attributes, :id)
11
+ params = Utils.serialize_values(params)
11
12
  json = client.post_json(path, params)
12
13
  new(json)
13
14
  end
@@ -3,22 +3,19 @@ module Delighted
3
3
  module Retrieve
4
4
  def self.included(klass)
5
5
  if klass.singleton_resource?
6
- Operations.add_operations(klass, Singleton::OperationClassMethods)
6
+ klass.extend(Singleton::ClassMethods)
7
7
  else
8
- klass.extend(Pluralton::AuxiliaryClassMethods)
9
- Operations.add_operations(klass, Pluralton::OperationClassMethods)
8
+ klass.extend(Pluralton::ClassMethods)
10
9
  end
11
10
  end
12
11
 
13
12
  module Pluralton
14
- module OperationClassMethods
15
- def retrieve(client, id)
13
+ module ClassMethods
14
+ def retrieve(id, client = Delighted.shared_client)
16
15
  json = client.get_json(path(id))
17
16
  new(json)
18
17
  end
19
- end
20
18
 
21
- module AuxiliaryClassMethods
22
19
  def path(id = nil)
23
20
  id ? "#{@path}/#{id}" : @path
24
21
  end
@@ -26,8 +23,9 @@ module Delighted
26
23
  end
27
24
 
28
25
  module Singleton
29
- module OperationClassMethods
30
- def retrieve(client, opts = {})
26
+ module ClassMethods
27
+ def retrieve(opts = {}, client = Delighted.shared_client)
28
+ opts = Utils.serialize_values(opts)
31
29
  json = client.get_json(path, opts)
32
30
  new(json)
33
31
  end
@@ -1,14 +1,6 @@
1
1
  module Delighted
2
2
  class Resource
3
3
  class << self
4
- def interface_name=(interface_name)
5
- @interface_name = interface_name
6
- end
7
-
8
- def interface_name
9
- @interface_name
10
- end
11
-
12
4
  def path=(path)
13
5
  @path = path
14
6
  end
@@ -24,18 +16,6 @@ module Delighted
24
16
  def singleton_resource?
25
17
  !!@singleton_resource
26
18
  end
27
-
28
- def operations
29
- @operations ||= Set.new
30
- end
31
-
32
- def resources
33
- @resources ||= Set.new
34
- end
35
-
36
- def inherited(klass)
37
- resources << klass
38
- end
39
19
  end
40
20
 
41
21
  undef :id if method_defined?(:id)
@@ -45,7 +25,7 @@ module Delighted
45
25
  def initialize(attributes = {})
46
26
  @id = attributes[:id]
47
27
  define_id_reader if @id
48
- @attributes = Utils.hash_removing_key(attributes, :id)
28
+ @attributes = Utils.hash_without_key(attributes, :id)
49
29
  define_attribute_accessors(@attributes.keys)
50
30
  end
51
31
 
@@ -1,6 +1,5 @@
1
1
  module Delighted
2
2
  class Metrics < Resource
3
- self.interface_name = "metrics"
4
3
  self.path = "/metrics"
5
4
  self.singleton_resource = true
6
5
 
@@ -1,6 +1,5 @@
1
1
  module Delighted
2
2
  class Person < Resource
3
- self.interface_name = "people"
4
3
  self.path = "/people"
5
4
 
6
5
  include Operations::Create
@@ -1,6 +1,5 @@
1
1
  module Delighted
2
2
  class SurveyResponse < Resource
3
- self.interface_name = "survey_responses"
4
3
  self.path = "/survey_responses"
5
4
 
6
5
  include Operations::Create
@@ -4,7 +4,7 @@ module Delighted
4
4
  class << object; self; end
5
5
  end
6
6
 
7
- def self.hash_removing_key(hash, key)
7
+ def self.hash_without_key(hash, key)
8
8
  hash.reject { |k,v| k == key }.inject({}) { |memo,(k,v)| memo[k] = v; memo }
9
9
  end
10
10
 
@@ -38,6 +38,22 @@ module Delighted
38
38
  object
39
39
  end
40
40
  end
41
+
42
+ def self.serialize_values(object)
43
+ case object
44
+ when Time, Date
45
+ object.to_i
46
+ when Hash
47
+ object.inject({}) { |memo,(k,v)|
48
+ memo[k] = serialize_values(v)
49
+ memo
50
+ }
51
+ when Array
52
+ object.map { |v| serialize_values(v) }
53
+ else
54
+ object
55
+ end
56
+ end
41
57
  end
42
58
  end
43
59
 
@@ -1,3 +1,3 @@
1
1
  module Delighted
2
- VERSION = "1.0.0.beta"
2
+ VERSION = "1.0.0.beta2"
3
3
  end
@@ -14,8 +14,7 @@ class Delighted::MetricsTest < Delighted::TestCase
14
14
  response = Delighted::HTTPResponse.new(200, {}, Delighted::JSON.dump({ :nps => 10 }))
15
15
  mock_http_adapter.expects(:request).with(:get, uri, headers).once.returns(response)
16
16
 
17
- client = Delighted::Client.new(:api_key => '123abc', :http_adapter => mock_http_adapter)
18
- metrics = client.metrics.retrieve
17
+ metrics = Delighted::Metrics.retrieve
19
18
  assert_kind_of Delighted::Metrics, metrics
20
19
  assert_equal({ :nps => 10 }, metrics.to_hash)
21
20
  assert_equal 10, metrics.nps
@@ -31,8 +30,7 @@ class Delighted::PeopleTest < Delighted::TestCase
31
30
  response = Delighted::HTTPResponse.new(200, {}, Delighted::JSON.dump({ :id => '123', :email => 'foo@bar.com' }))
32
31
  mock_http_adapter.expects(:request).with(:post, uri, headers, data).once.returns(response)
33
32
 
34
- client = Delighted::Client.new(:api_key => '123abc', :http_adapter => mock_http_adapter)
35
- person = client.people.create(:email => 'foo@bar.com')
33
+ person = Delighted::Person.create(:email => 'foo@bar.com')
36
34
  assert_kind_of Delighted::Person, person
37
35
  assert_equal({ :email => 'foo@bar.com' }, person.to_hash)
38
36
  assert_equal 'foo@bar.com', person.email
@@ -48,8 +46,7 @@ class Delighted::SurveyResponseTest < Delighted::TestCase
48
46
  response = Delighted::HTTPResponse.new(200, {}, Delighted::JSON.dump({ :id => '456', :person => '123', :score => 10 }))
49
47
  mock_http_adapter.expects(:request).with(:post, uri, headers, data).once.returns(response)
50
48
 
51
- client = Delighted::Client.new(:api_key => '123abc', :http_adapter => mock_http_adapter)
52
- survey_response = client.survey_responses.create(:person => '123', :score => 10)
49
+ survey_response = Delighted::SurveyResponse.create(:person => '123', :score => 10)
53
50
  assert_kind_of Delighted::SurveyResponse, survey_response
54
51
  assert_equal({ :person => '123', :score => 10 }, survey_response.to_hash)
55
52
  assert_equal '123', survey_response.person
@@ -63,8 +60,7 @@ class Delighted::SurveyResponseTest < Delighted::TestCase
63
60
  response = Delighted::HTTPResponse.new(200, {}, Delighted::JSON.dump([{ :id => '123', :comment => 'One' }, { :id => '456', :comment => 'Two' }]))
64
61
  mock_http_adapter.expects(:request).with(:get, uri, headers).once.returns(response)
65
62
 
66
- client = Delighted::Client.new(:api_key => '123abc', :http_adapter => mock_http_adapter)
67
- survey_responses = client.survey_responses.all
63
+ survey_responses = Delighted::SurveyResponse.all
68
64
  assert_kind_of Delighted::EnumerableResourceCollection, survey_responses
69
65
  assert_kind_of Delighted::SurveyResponse, survey_responses[0]
70
66
  assert_equal({ :comment => 'One' }, survey_responses[0].to_hash)
data/test/test_helper.rb CHANGED
@@ -5,6 +5,11 @@ require 'mocha/setup'
5
5
  class Delighted::TestCase < Minitest::Test
6
6
  include Mocha
7
7
 
8
+ def setup
9
+ super
10
+ Delighted.shared_client = Delighted::Client.new(:api_key => '123abc', :http_adapter => mock_http_adapter)
11
+ end
12
+
8
13
  def mock_http_adapter
9
14
  @mock ||= mock
10
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delighted
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta
4
+ version: 1.0.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Dodwell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-06 00:00:00.000000000 Z
11
+ date: 2013-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -87,12 +87,10 @@ files:
87
87
  - lib/delighted/http_adapter.rb
88
88
  - lib/delighted/http_response.rb
89
89
  - lib/delighted/json.rb
90
- - lib/delighted/operations.rb
91
90
  - lib/delighted/operations/all.rb
92
91
  - lib/delighted/operations/create.rb
93
92
  - lib/delighted/operations/retrieve.rb
94
93
  - lib/delighted/resource.rb
95
- - lib/delighted/resource_interface.rb
96
94
  - lib/delighted/resources/metrics.rb
97
95
  - lib/delighted/resources/person.rb
98
96
  - lib/delighted/resources/survey_responses.rb
@@ -1,8 +0,0 @@
1
- module Delighted
2
- module Operations
3
- def self.add_operations(klass, operation_module)
4
- klass.extend(operation_module)
5
- operation_module.instance_methods.each { |operation| klass.operations << operation }
6
- end
7
- end
8
- end
@@ -1,19 +0,0 @@
1
- module Delighted
2
- class ResourceInterface
3
- def initialize(client, resource)
4
- @resource = resource # more helpful inspect
5
-
6
- Utils.eigenclass(self).instance_eval do
7
- resource.operations.each do |operation|
8
- define_method(operation) do |*args, &block|
9
- resource.send(operation, *args.unshift(client), &block)
10
- end
11
- end
12
- end
13
- end
14
-
15
- def to_s
16
- inspect
17
- end
18
- end
19
- end