urtak 0.9.7 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,4 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
- spec/fixtures/*
5
+ spec/fixtures/*
6
+ *.log
@@ -12,7 +12,7 @@ module Urtak
12
12
  end
13
13
 
14
14
  class Api
15
- attr_accessor :options
15
+ attr_accessor :options, :logger
16
16
 
17
17
  def initialize(user_options = {})
18
18
  @options = {
@@ -26,31 +26,40 @@ module Urtak
26
26
  }.merge(user_options.select{|k,v|!v.nil?})
27
27
  end
28
28
 
29
+ def enable_logger(path)
30
+ require 'logger'
31
+ RestClient.log = @logger = Logger.new(path)
32
+ end
33
+
34
+ def log(level, message)
35
+ @logger.send(level, message) if @logger
36
+ end
37
+
29
38
  # ACCOUNTS
30
39
  def get_account(id)
31
- Response.new(fire(:get, "accounts/#{id}"))
40
+ create_response(fire(:get, "accounts/#{id}"))
32
41
  end
33
42
 
34
43
  def create_account(attributes)
35
- Response.new(fire(:post, "accounts", {:account => attributes}))
44
+ create_response(fire(:post, "accounts", {:account => attributes}))
36
45
  end
37
46
 
38
47
  # PUBLICATIONS
39
48
  def get_publication(key = options[:publication_key])
40
- Response.new(fire(:get, "publications/#{key}"))
49
+ create_response(fire(:get, "publications/#{key}"))
41
50
  end
42
51
 
43
52
  def create_publication(attributes)
44
- Response.new(fire(:post, "publications", {:publication => attributes}))
53
+ create_response(fire(:post, "publications", {:publication => attributes}))
45
54
  end
46
55
 
47
56
  def update_publication(key, attributes)
48
- Response.new(fire(:put, "publications/#{key}", {:publication => attributes}))
57
+ create_response(fire(:put, "publications/#{key}", {:publication => attributes}))
49
58
  end
50
59
 
51
60
  # URTAKS
52
61
  def list_urtaks(options = {})
53
- Response.new(fire(:get, "urtaks", options))
62
+ create_response(fire(:get, "urtaks", options))
54
63
  end
55
64
 
56
65
  def get_urtak(property, value, options = {})
@@ -62,11 +71,11 @@ module Urtak
62
71
  path = "urtaks/hash/#{Digest::SHA1.hexdigest(value)}"
63
72
  end
64
73
 
65
- Response.new(fire(:get, path, options))
74
+ create_response(fire(:get, path, options))
66
75
  end
67
76
 
68
77
  def create_urtak(urtak_attributes, questions=[])
69
- Response.new(fire(:post, 'urtaks', {:urtak => urtak_attributes.merge(:questions => questions)}))
78
+ create_response(fire(:post, 'urtaks', {:urtak => urtak_attributes.merge(:questions => questions)}))
70
79
  end
71
80
 
72
81
  def update_urtak(property, attributes)
@@ -80,7 +89,7 @@ module Urtak
80
89
  path = "urtaks/hash/#{Digest::SHA1.hexdigest(value)}"
81
90
  end
82
91
 
83
- Response.new(fire(:put, path, {:urtak => attributes}))
92
+ create_response(fire(:put, path, {:urtak => attributes}))
84
93
  end
85
94
 
86
95
  # URTAK QUESTIONS
@@ -93,7 +102,7 @@ module Urtak
93
102
  path = "urtaks/hash/#{Digest::SHA1.hexdigest(value)}/questions"
94
103
  end
95
104
 
96
- Response.new(fire(:get, path, options))
105
+ create_response(fire(:get, path, options))
97
106
  end
98
107
 
99
108
  def get_urtak_question(property, value, id, options = {})
@@ -105,7 +114,7 @@ module Urtak
105
114
  path = "urtaks/hash/#{Digest::SHA1.hexdigest(value)}/questions/#{id}"
106
115
  end
107
116
 
108
- Response.new(fire(:get, path, options))
117
+ create_response(fire(:get, path, options))
109
118
  end
110
119
 
111
120
  def create_urtak_question(property, value, question)
@@ -117,7 +126,7 @@ module Urtak
117
126
  path = "urtaks/hash/#{Digest::SHA1.hexdigest(value)}/questions"
118
127
  end
119
128
 
120
- Response.new(fire(:post, path, {:question => question}))
129
+ create_response(fire(:post, path, {:question => question}))
121
130
  end
122
131
 
123
132
  def approve_urtak_question(property, value, id)
@@ -129,7 +138,7 @@ module Urtak
129
138
  path = "urtaks/hash/#{Digest::SHA1.hexdigest(value)}/questions/#{id}/approve"
130
139
  end
131
140
 
132
- Response.new(fire(:put, path))
141
+ create_response(fire(:put, path))
133
142
  end
134
143
 
135
144
  def reject_urtak_question(property, value, id)
@@ -141,7 +150,7 @@ module Urtak
141
150
  path = "urtaks/hash/#{Digest::SHA1.hexdigest(value)}/questions/#{id}/reject"
142
151
  end
143
152
 
144
- Response.new(fire(:put, path))
153
+ create_response(fire(:put, path))
145
154
  end
146
155
 
147
156
  def spam_urtak_question(property, value, id)
@@ -153,7 +162,7 @@ module Urtak
153
162
  path = "urtaks/hash/#{Digest::SHA1.hexdigest(value)}/questions/#{id}/spam"
154
163
  end
155
164
 
156
- Response.new(fire(:put, path))
165
+ create_response(fire(:put, path))
157
166
  end
158
167
 
159
168
  def ham_urtak_question(property, value, id)
@@ -165,20 +174,41 @@ module Urtak
165
174
  path = "urtaks/hash/#{Digest::SHA1.hexdigest(value)}/questions/#{id}/ham"
166
175
  end
167
176
 
168
- Response.new(fire(:put, path))
177
+ create_response(fire(:put, path))
169
178
  end
170
179
 
171
180
  # QUESTIONS
172
181
  def list_questions(options={})
173
- Response.new(fire(:get, "questions", options))
182
+ create_response(fire(:get, "questions", options))
174
183
  end
175
184
 
176
185
  def get_question(id)
177
- Response.new(fire(:get, "questions/#{id}"))
186
+ create_response(fire(:get, "questions/#{id}"))
178
187
  end
179
188
 
180
189
 
181
190
  private
191
+ def create_response(raw)
192
+ response = Response.new(raw)
193
+
194
+ # Now entering the Republic of Hackistan
195
+ if self.logger
196
+ relevant_headers = ["content-type", "location", "content-length", "etag"]
197
+ log_response = []
198
+ log_response << "\n"
199
+ log_response << "HTTP/1.1 #{response.code} #{response.raw.net_http_res.message}"
200
+ response.raw.net_http_res.each_header do |h,v|
201
+ next unless relevant_headers.include?(h)
202
+ log_response << "#{h.capitalize}: #{v}"
203
+ end
204
+ log_response << (response.format.eql?("JSON") ? JSON.pretty_generate(response.body) : "")
205
+ log_response << "\n"
206
+ log(:debug, log_response.join("\n"))
207
+ end
208
+
209
+ return response
210
+ end
211
+
182
212
  def fire(method, path, data={})
183
213
  if method == :get
184
214
  RestClient.get("#{options[:api_base]}/#{path}",
@@ -229,18 +259,21 @@ module Urtak
229
259
  end
230
260
 
231
261
  class Response
232
- attr_accessor :raw, :body
262
+ attr_accessor :raw, :body, :format
233
263
 
234
264
  def initialize(response)
235
- @raw = response
265
+ @raw = response
266
+ @format = nil
236
267
 
237
268
  # TODO case statement
238
- if response.headers[:content_type].nil?
269
+ if response.headers[:content_type].nil? || response.body.empty?
239
270
  @body = response
240
271
  elsif response.headers[:content_type].match(/json/)
241
272
  @body = JSON.parse(response)
273
+ @format = "JSON"
242
274
  elsif response.headers[:content_type].match(/xml/)
243
275
  raise Urtak::Errors::Unimplemented
276
+ @format = "XML"
244
277
  else
245
278
  @body = response
246
279
  end
@@ -1,3 +1,3 @@
1
1
  module Urtak
2
- VERSION = "0.9.7"
2
+ VERSION = "0.9.8"
3
3
  end
@@ -2,8 +2,6 @@ def is_ruby_19?
2
2
  RUBY_VERSION == '1.9.1' or RUBY_VERSION == '1.9.2'
3
3
  end
4
4
 
5
- Encoding.default_internal = Encoding.default_external = "ASCII-8BIT" if is_ruby_19?
6
-
7
5
  require 'rubygems'
8
6
  require 'rspec'
9
7
  require 'vcr'
@@ -10,6 +10,9 @@ describe Urtak::Api do
10
10
 
11
11
  before(:each) do
12
12
  @client = Urtak::Api.new(@settings)
13
+ @client.enable_logger(File.expand_path(File.join(File.dirname(__FILE__), 'client.log')))
14
+ # @client.logger.level = Logger::INFO
15
+ @client.logger.level = Logger::DEBUG
13
16
  end
14
17
 
15
18
  it "should form http requests with proper accept headers"
@@ -38,7 +41,6 @@ describe Urtak::Api do
38
41
  context "publications" do
39
42
  it "should create a publication" do
40
43
  VCR.use_cassette('create_publication') do
41
- @client = Urtak::Api.new(@settings)
42
44
  publication = {
43
45
  :name => "Fun with VCRs",
44
46
  :domains => "knossos.local"
@@ -51,7 +53,6 @@ describe Urtak::Api do
51
53
 
52
54
  it "should find a publication" do
53
55
  VCR.use_cassette('get_publication', :match_requests_on => [:method, :host, :path]) do
54
- @client = Urtak::Api.new(@settings)
55
56
  response = @client.get_publication(@settings[:publication_key])
56
57
  response.class.should eq(Urtak::Response)
57
58
  response.code.should eq(200)
@@ -60,7 +61,6 @@ describe Urtak::Api do
60
61
 
61
62
  it "should update a publication" do
62
63
  VCR.use_cassette('update_publication') do
63
- @client = Urtak::Api.new(@settings)
64
64
  publication = {
65
65
  :name => "Fun with VCRs",
66
66
  :domains => "funwithvcrs.com"
@@ -74,7 +74,6 @@ describe Urtak::Api do
74
74
  context "urtaks" do
75
75
  it "should create an urtak" do
76
76
  VCR.use_cassette('create_urtak') do
77
- @client = Urtak::Api.new(@settings)
78
77
  @post_id = Digest::SHA1.hexdigest("#{Time.now.to_i}")
79
78
  urtak = {
80
79
  :title => "200 Fun Things to Do with Cassette Tape",
@@ -94,7 +93,6 @@ describe Urtak::Api do
94
93
 
95
94
  it "should list urtaks" do
96
95
  VCR.use_cassette('list_urtaks', :match_requests_on => [:method, :host, :path]) do
97
- @client = Urtak::Api.new(@settings)
98
96
  response = @client.list_urtaks
99
97
  response.code.should eq(200)
100
98
  response.body['urtaks']['urtak'].class.should eq(Array)
@@ -103,12 +101,10 @@ describe Urtak::Api do
103
101
 
104
102
  it "should find an urtak by id" do
105
103
  VCR.use_cassette('list_urtaks', :match_requests_on => [:method, :host, :path]) do
106
- @client = Urtak::Api.new(@settings)
107
104
  response = @client.list_urtaks
108
105
  @urtak = response.body['urtaks']['urtak'].last
109
106
 
110
107
  VCR.use_cassette('get_urtak_by_id', :match_requests_on => [:method, :host, :path]) do
111
- @client = Urtak::Api.new(@settings)
112
108
  response = @client.get_urtak(:id, @urtak['id'])
113
109
  response.code.should eq(200)
114
110
  end
@@ -117,12 +113,10 @@ describe Urtak::Api do
117
113
 
118
114
  it "should find an urtak by permalink hash" do
119
115
  VCR.use_cassette('list_urtaks', :match_requests_on => [:method, :host, :path]) do
120
- @client = Urtak::Api.new(@settings)
121
116
  response = @client.list_urtaks
122
117
  @urtak = response.body['urtaks']['urtak'].last
123
118
 
124
119
  VCR.use_cassette('get_urtak_by_permalink', :match_requests_on => [:method, :host, :path]) do
125
- @client = Urtak::Api.new(@settings)
126
120
  response = @client.get_urtak(:permalink, @urtak['permalink'])
127
121
  response.code.should eq(200)
128
122
  end
@@ -131,12 +125,10 @@ describe Urtak::Api do
131
125
 
132
126
  it "should find an urtak by post_id" do
133
127
  VCR.use_cassette('list_urtaks', :match_requests_on => [:method, :host, :path]) do
134
- @client = Urtak::Api.new(@settings)
135
128
  response = @client.list_urtaks
136
129
  @urtak = response.body['urtaks']['urtak'].last
137
130
 
138
131
  VCR.use_cassette('get_urtak_by_post_id', :match_requests_on => [:method, :host, :path]) do
139
- @client = Urtak::Api.new(@settings)
140
132
  response = @client.get_urtak(:post_id, @urtak['post_id'])
141
133
  response.code.should eq(200)
142
134
  end
@@ -145,13 +137,10 @@ describe Urtak::Api do
145
137
 
146
138
  it "should update an urtak" do
147
139
  VCR.use_cassette('list_urtaks', :match_requests_on => [:method, :host, :path]) do
148
- @client = Urtak::Api.new(@settings)
149
140
  response = @client.list_urtaks
150
141
  @urtak = response.body['urtaks']['urtak'].last
151
142
 
152
143
  VCR.use_cassette('update_urtak') do
153
- @client = Urtak::Api.new(@settings)
154
-
155
144
  urtak = {
156
145
  :title => "200 really fun things to do with a cassette tape",
157
146
  :post_id => @urtak['post_id'],
@@ -169,7 +158,6 @@ describe Urtak::Api do
169
158
  context "urtak-questions" do
170
159
  before(:each) do
171
160
  VCR.use_cassette('list_urtaks', :match_requests_on => [:method, :host, :path]) do
172
- @client = Urtak::Api.new(@settings)
173
161
  response = @client.list_urtaks
174
162
  @urtak = response.body['urtaks']['urtak'].last
175
163
  end
@@ -261,7 +249,6 @@ describe Urtak::Api do
261
249
  context "questions" do
262
250
  it "should list questions" do
263
251
  VCR.use_cassette('list_questions', :match_requests_on => [:method, :host, :path]) do
264
- @client = Urtak::Api.new(@settings)
265
252
  response = @client.list_questions
266
253
  if ENV['ADMIN']
267
254
  response.code.should eq(200)
@@ -273,8 +260,6 @@ describe Urtak::Api do
273
260
 
274
261
  it "should find a question" do
275
262
  VCR.use_cassette('list_questions', :match_requests_on => [:method, :host, :path]) do
276
- @client = Urtak::Api.new(@settings)
277
-
278
263
  VCR.use_cassette('get_question', :match_requests_on => [:method, :host, :path]) do
279
264
  if ENV['ADMIN']
280
265
  @question = @client.list_questions.body['questions']['question'].last
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: urtak
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.7
4
+ version: 0.9.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-01 00:00:00.000000000Z
12
+ date: 2012-03-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70275140141100 !ruby/object:Gem::Requirement
16
+ requirement: &2154780820 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70275140141100
24
+ version_requirements: *2154780820
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70275140140300 !ruby/object:Gem::Requirement
27
+ requirement: &2154780360 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70275140140300
35
+ version_requirements: *2154780360
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: fakeweb
38
- requirement: &70275140139520 !ruby/object:Gem::Requirement
38
+ requirement: &2154779920 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70275140139520
46
+ version_requirements: *2154779920
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: vcr
49
- requirement: &70275140139080 !ruby/object:Gem::Requirement
49
+ requirement: &2154779400 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70275140139080
57
+ version_requirements: *2154779400
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rest-client
60
- requirement: &70275140138640 !ruby/object:Gem::Requirement
60
+ requirement: &2154778900 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70275140138640
68
+ version_requirements: *2154778900
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: json
71
- requirement: &70275140138200 !ruby/object:Gem::Requirement
71
+ requirement: &2154778240 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70275140138200
79
+ version_requirements: *2154778240
80
80
  description: Ruby Client for the Urtak REST API
81
81
  email:
82
82
  - kunal@urtak.com
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  version: '0'
114
114
  requirements: []
115
115
  rubyforge_project: urtak
116
- rubygems_version: 1.8.10
116
+ rubygems_version: 1.8.9
117
117
  signing_key:
118
118
  specification_version: 3
119
119
  summary: Ruby Client for the Urtak REST API