evoke_client 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,35 +1,95 @@
1
1
  # Evoke Client
2
2
 
3
- evoke-client is a simple rest-client utility for allowing your application to converse with the [Evoke Service](http://evoke.thumblemonks.com). The source code for [Evoke can be found on GitHub](http://github.com/thumblemonks/evoke) along with what Evoke is intended for.
3
+ The Evoke client is a simple HTTParty utility for allowing your application to converse with an [Evoke service](http://github.com/thumblemonks/evoke).
4
+
5
+ Soon, there will be a global Evoke app that your app can talk to. For now, you should run your own server.
4
6
 
5
7
  ### Usage
6
8
 
7
- # evoke = Evoke.new(:url => 'http://example.com/users/unsubscribe', :callback_at => (Time.now + 86400))
8
- # evoke.save
9
+ Essentially, the Evoke client acts like an `ActiveRecord` model. The basic methods are:
10
+
11
+ * Evoke::Callback.find(guid)
12
+ * Evoke::Callback.create\_or\_update(attributes\_hash)
13
+ * Evoke::Callback#update\_attributes(hash\_to\_merge)
14
+ * Evoke::Callback#save
15
+ * Evoke::Callback#destroy
16
+
17
+ Typically, you would simply need to call `create_or_update` from wherever you are calling out to Evoke. Most of the apps we use Evoke client with make calls similar to the following:
18
+
19
+ class SomeObject
20
+ def some_method
21
+ ...
22
+ Evoke::Callback.create_or_update(
23
+ "guid" => "some-kind-of-unique-string",
24
+ "url" => "http://example.com/users/unsubscribe",
25
+ "callback_at" => Time.now + 3600)
26
+ ...
27
+ end
28
+ end
29
+
30
+ The Evoke client will raise an `Evoke::RecordInvalid` exception if the callback could not be created or updated for some reason. This is the same for an explicit call to save. The reason for the failure will be returned in the exception as the message.
31
+
32
+ If you wanted to explicitly do what `create_or_update` is doing for you in your code, you would likely write your code like so:
9
33
 
10
- Evoke.configure(...)
11
- callback = Evoke::Callback.new(:url => 'http://example.com/users/unsubscribe', :callback_at => (Time.now + 86400))
34
+ ...
35
+ callback = Evoke::Callback.find("some-kind-of-unique-string")
36
+ if callback
37
+ callback.update_attributes("callback_at" => Time.now + 3600)
38
+ else
39
+ callback = Evoke::Callback.new(
40
+ "guid" => "some-kind-of-unique-string",
41
+ "url" => "http://example.com/users/unsubscribe",
42
+ "callback_at" => Time.now + 3600)
43
+ end
12
44
  callback.save
13
-
14
- callback = Evoke::Callback.find(guid)
45
+ ...
46
+
47
+ If you no longer need a callback, you can destroy it. Simply find the callback and then call its destroy method.
48
+
49
+ callback = Evoke::Callback.find("some-kind-of-unique-string")
15
50
  callback.destroy
16
- callback.update
17
-
18
- # What happens if save fails
19
51
 
20
52
  ### Configuration
21
53
 
22
- By default, evoke-client tries to talk to the Evoke service, generously hosted by Thumble Monks :) Because Evoke itself is open source and able to be run by you anywhere you want it to, the only real configuration parameters are for the hostname and port that you want evoke-client to talk to Evoke on. For instance, when we use Evoke in our projects, we may want to test with a local instance while doing development.
54
+ To modify host and port, just set the following somewhere after you have required in (see following sections) the Evoke client:
55
+
56
+ Evoke.configure "http://example.com:4567"
57
+
58
+ If you're using a newer version Rails, you could set it in an initializer. However, you may not want to be adding callbacks to your production instance of Evoke when doing local development. It's probably better to explicitly configure Evoke in your `development.rb` and `production.rb` files.
59
+
60
+ Then, if you're using Rails, put this is your `development.rb` and `production.rb` files:
61
+
62
+ config.gem 'evoke_client', :src => "http://gemcutter.org"
23
63
 
24
- To modify host and port, just set the following:
64
+ Otherwise, do the standard:
25
65
 
26
- Evoke.host = "example.com"
27
- Evoke.port = "4567"
28
- # Choosing 4567 because Evoke is written for Sinatra
66
+ require 'evoke_client'
67
+
68
+ #### Testing
69
+
70
+ If using Rails, put this is your `config/environment/test.rb`:
71
+
72
+ config.gem 'evoke_client', :lib => 'evoke_client/mock', :src => "http://gemcutter.org"
73
+
74
+ If not using Rails, put this in your `test_helper.rb` or whatever you call it:
75
+
76
+ require 'evoke_client/mock'
77
+
78
+ You need to make sure you have required in `evoke_client/mock` **AFTER** you have required `evoke_client`. Otherwise ... bad stuff.
79
+
80
+ Don't worry about calling `Evoke.configure` if you're requiring in the mock library. It's irrelevant. What is relevant, however, is mocking out the calls Evoke client will send to HTTParty. Evoke client has a solution, but I need to document it here and I'm not ready yet. Sorry.
81
+
82
+ DOCUMENTATION NEEDED FOR HTTMockParty
29
83
 
30
84
  ### Installation
31
85
 
32
- gem install thumblemonks-evoke_client
86
+ If you haven't done so yet, you should add the GemCutter source to your list of gem sources:
87
+
88
+ gem sources -a http://gemcutter.org
89
+
90
+ Then installing the Evoke client is as simple as:
91
+
92
+ gem install evoke_client
33
93
 
34
94
  #### Dependencies
35
95
 
data/evoke_client.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "evoke_client"
3
- s.version = "0.2.1"
3
+ s.version = "0.2.2"
4
4
  s.date = "2009-10-07"
5
5
  s.summary = "Tool for interfacing with the Evoke web service"
6
6
  s.email = %w[gus@gusg.us]
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.extra_rdoc_files = ["README.markdown"]
15
15
 
16
16
  s.add_dependency("httparty", [">= 0.4.4"])
17
+ s.add_development_dependency("riot", [">= 0.9.6"])
17
18
 
18
19
  s.files = %w[
19
20
  MIT-LICENSE
@@ -21,11 +22,13 @@ Gem::Specification.new do |s|
21
22
  evoke_client.gemspec
22
23
  lib/evoke_client.rb
23
24
  lib/evoke_client/base.rb
25
+ lib/evoke_client/mash.rb
24
26
  lib/evoke_client/mock.rb
25
27
  ]
26
28
 
27
29
  s.test_files = %w[
28
30
  Rakefile
31
+ test/callback_test.rb
29
32
  test/configuration_test.rb
30
33
  test/create_or_update_test.rb
31
34
  test/create_test.rb
@@ -1,3 +1,5 @@
1
+ require 'evoke_client/mash'
2
+
1
3
  module Evoke
2
4
  class RecordError < Exception; end
3
5
  class RecordInvalid < RecordError; end
@@ -14,6 +16,7 @@ module Evoke
14
16
  end
15
17
 
16
18
  def self.create_or_update(data)
19
+ data = Callback.stringify_keys(data)
17
20
  callback = (find(data["guid"]) || new(data)).update_attributes(data)
18
21
  callback.save
19
22
  callback
@@ -21,13 +24,13 @@ module Evoke
21
24
 
22
25
  def initialize(data)
23
26
  @new_record = determine_if_new_record(data.delete(:new_record))
24
- @data = data
27
+ @data = Callback.stringify_keys(data)
25
28
  end
26
29
 
27
30
  def new_record?; @new_record; end
28
31
 
29
32
  def update_attributes(new_data)
30
- @data = @data.merge(new_data)
33
+ @data = @data.merge(Callback.stringify_keys(new_data))
31
34
  self
32
35
  end
33
36
 
@@ -56,5 +59,9 @@ module Evoke
56
59
  def determine_if_new_record(condition)
57
60
  condition.nil? || condition
58
61
  end
62
+
63
+ def self.stringify_keys(hash)
64
+ hash.mash { |k,v| {k.to_s => v} }
65
+ end
59
66
  end # Callback
60
67
  end # Evoke
@@ -0,0 +1,9 @@
1
+ module Enumerable
2
+ # Let's you do stuff like:
3
+ #
4
+ # hashed_users = [:id, :name].mash {|attr| {attr => attr.to_s} }
5
+ # => {:id => 'id', :name => 'name'}
6
+ def mash
7
+ self.inject({}) { |a,i| a.merge( yield(i) ) }
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ require 'teststrap'
2
+
3
+ context "initializing a new callback" do
4
+ setup do
5
+ Evoke::Callback.new("url" => "foo", :guid => "papa")
6
+ end
7
+
8
+ asserts("url is accessible from a string key") { topic.url }.equals("foo")
9
+ asserts("guid is accessible from a symbolized key") { topic.guid }.equals("papa")
10
+ end
11
+
12
+ context "updating attributes of a callback" do
13
+ setup do
14
+ callback = Evoke::Callback.new("guid" => "meme", "url" => "http://foo.bar", "http_method" => "get")
15
+ callback.update_attributes("guid" => "mom", :url => "http://a.b")
16
+ callback
17
+ end
18
+
19
+ asserts("guid updated from a string key") { topic.guid }.equals("mom")
20
+ asserts("url updated form a symbolized key") { topic.url }.equals("http://a.b")
21
+ asserts("http_method is unchanged") { topic.http_method }.equals("get")
22
+ end # updating attributes of a callback
@@ -7,7 +7,7 @@ context "create or update" do
7
7
  Evoke::HTTMockParty.get('/callbacks/poster').not_found
8
8
  Evoke::HTTMockParty.post('/callbacks', :query => {"guid" => "poster"}).
9
9
  responds({"url" => "http://poster"}).created
10
- Evoke::Callback.create_or_update({"guid" => "poster"})
10
+ Evoke::Callback.create_or_update({:guid => "poster"})
11
11
  end
12
12
 
13
13
  should "post to callbacks and update itself accordingly" do
data/test/update_test.rb CHANGED
@@ -1,17 +1,5 @@
1
1
  require 'teststrap'
2
2
 
3
- context "updating attributes of a callback" do
4
- setup do
5
- callback = Evoke::Callback.new("guid" => "meme", "url" => "http://foo.bar", "http_method" => "get")
6
- callback.update_attributes("guid" => "mom", "url" => "http://a.b")
7
- callback
8
- end
9
-
10
- asserts("guid updated") { topic.guid }.equals("mom")
11
- asserts("url updated") { topic.url }.equals("http://a.b")
12
- asserts("http_method is unchanged") { topic.http_method }.equals("get")
13
- end # updating attributes of a callback
14
-
15
3
  context "updating a callback" do
16
4
 
17
5
  context "that actually exists" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evoke_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Knowlden
@@ -22,6 +22,16 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 0.4.4
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: riot
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.6
34
+ version:
25
35
  description: Tool for interfacing with the Evoke web service. See http://github.com/thumblemonks/evoke
26
36
  email:
27
37
  - gus@gusg.us
@@ -37,6 +47,7 @@ files:
37
47
  - evoke_client.gemspec
38
48
  - lib/evoke_client.rb
39
49
  - lib/evoke_client/base.rb
50
+ - lib/evoke_client/mash.rb
40
51
  - lib/evoke_client/mock.rb
41
52
  has_rdoc: true
42
53
  homepage: http://github.com/thumblemonks/evoke_client
@@ -69,6 +80,7 @@ specification_version: 3
69
80
  summary: Tool for interfacing with the Evoke web service
70
81
  test_files:
71
82
  - Rakefile
83
+ - test/callback_test.rb
72
84
  - test/configuration_test.rb
73
85
  - test/create_or_update_test.rb
74
86
  - test/create_test.rb