prx_client 0.2.2 → 0.3.0

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: 132351a149412a73c95725da8f6bf242fb792a91
4
- data.tar.gz: 1f5e1c92cc109f0837bc8f63e0142b27d42a06e1
3
+ metadata.gz: c7988641f39e2cfad35055585508a8413745176f
4
+ data.tar.gz: 94bf0842283ca1155792cb107eeee55eb787b9cf
5
5
  SHA512:
6
- metadata.gz: 1c42a9531611a380a33112dc38f9a77fa5b0b1faba6d26760a2b8fe38ecd834819f05a3eb83b1c236f0fd18fd837f2b7d20cf7e3f92aabaee6be575463ab834f
7
- data.tar.gz: 5201923083bec40d37013788680c53195830c85ebb79255b6218db42d1e13059c002fe461fe2c6fac7aa3109b7e5663e3e945d32940922ddc455c1db84968ba0
6
+ metadata.gz: dd49376ad7c10236b49b301f108eb58bbfa2e5e857e7d090879e643718770ce73a98e3abb7b7375cacb7bf0d8137fbcecaab8728d660eeef098394121742ad2f
7
+ data.tar.gz: 3ab7782e4d901d37ec64b8a0c0b59c801a4c1812dec552a5aca15556ec7429fa4c270fbf8ff83012804044da212eadfd10d75eb7d92ea863969d84eaf2cef9de
@@ -1,5 +1,5 @@
1
1
  module PRX
2
2
  module Client
3
- VERSION = "0.2.2"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
data/lib/prx/client.rb CHANGED
@@ -1,17 +1,20 @@
1
1
  require 'prx/model'
2
2
  require 'prx/representer'
3
3
  require 'faraday'
4
- require 'faraday_middleware'
4
+ require 'prx/middleware/json'
5
+
6
+ Faraday::Request.register_middleware(:prx_json => PRX::Middleware::Json)
5
7
 
6
8
  module PRX
7
9
  module Client
8
-
10
+
9
11
  class << self
10
12
 
11
13
  include PRX::Model
12
14
 
13
15
  attr_accessor :key, :secret, :scheme, :host, :port, :version, :token
14
16
 
17
+
15
18
  def request(opts={})
16
19
  # puts "PRX::Client::request - opts: #{opts.inspect}"
17
20
  path = opts.delete(:path) || ''
@@ -38,7 +41,7 @@ module PRX
38
41
  OAuth2::Client.new(key, secret, {:site=>site}) do |b|
39
42
  b.request :multipart
40
43
  b.request :url_encoded
41
- b.request :json
44
+ b.request :prx_json
42
45
 
43
46
  # b.response :logger
44
47
 
@@ -0,0 +1,63 @@
1
+ require 'faraday'
2
+ require 'faraday/middleware'
3
+
4
+ # Request middleware that encodes the body as JSON.
5
+ #
6
+ # Processes only requests with matching Content-type or those without a type.
7
+ # If a request doesn't have a type but has a body, it sets the Content-type
8
+ # to JSON MIME-type.
9
+ #
10
+ # Doesn't try to encode bodies that already are in string form.
11
+
12
+ module PRX
13
+ module Middleware
14
+ class Json < Faraday::Middleware
15
+
16
+ CONTENT_TYPE = 'Content-Type'.freeze unless defined? CONTENT_TYPE
17
+
18
+ class << self
19
+ attr_accessor :mime_type
20
+ end
21
+
22
+ self.mime_type = 'application/json'.freeze
23
+
24
+ dependency do
25
+ require 'json' unless defined?(::JSON)
26
+ end
27
+
28
+ def call(env)
29
+ match_content_type(env) do |data|
30
+ env.body = encode(data)
31
+ end
32
+ @app.call env
33
+ end
34
+
35
+ def encode(data)
36
+ ::JSON.dump(data)
37
+ end
38
+
39
+ def match_content_type(env)
40
+ if process_request?(env)
41
+ env.request_headers[CONTENT_TYPE] ||= self.class.mime_type
42
+ yield(env.body) unless env.body.respond_to?(:to_str)
43
+ end
44
+ end
45
+
46
+ def process_request?(env)
47
+ type = request_type(env)
48
+ has_body?(env) and (type.empty? or type == self.class.mime_type)
49
+ end
50
+
51
+ def request_type(env)
52
+ type = env.request_headers[CONTENT_TYPE].to_s
53
+ type = type.split(';', 2).first if type.index(';')
54
+ type
55
+ end
56
+
57
+ def has_body?(env)
58
+ body = env.body and !(body.respond_to?(:to_str) and body.empty?)
59
+ end
60
+
61
+ end
62
+ end
63
+ end
data/prx_client.gemspec CHANGED
@@ -16,15 +16,11 @@ Gem::Specification.new do |s|
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
- # s.add_runtime_dependency "iconv"
20
- # s.add_runtime_dependency 'yajl-ruby'
21
- # s.add_runtime_dependency "i18n"
22
- # s.add_runtime_dependency "json"
19
+ s.add_runtime_dependency "json"
23
20
  s.add_runtime_dependency "activesupport"
24
21
  s.add_runtime_dependency "excon"
25
22
  s.add_runtime_dependency "oauth2"
26
23
  s.add_runtime_dependency 'faraday'
27
- s.add_runtime_dependency 'faraday_middleware'
28
24
  s.add_runtime_dependency "roar"
29
25
 
30
26
  # specify any dependencies here; for example:
data/test/test_client.rb CHANGED
@@ -28,7 +28,7 @@ class PRX::TestClient < Test::Unit::TestCase
28
28
 
29
29
  def test_me
30
30
  stub_request(:get, "http://development.prx.org:3001/me").
31
- with(:headers => {'Authorization'=>'Bearer iW7qgZSQq4ryQRks1G57X7B1appJLOWeF4yZz6jn', 'Host'=>'development.prx.org:3001', 'User-Agent'=>'Faraday v0.8.9'}).
31
+ with(:headers => {'Authorization'=>'Bearer iW7qgZSQq4ryQRks1G57X7B1appJLOWeF4yZz6jn', 'Host'=>'development.prx.org:3001'}).
32
32
  to_return(:status => 200, :body => @user_info.to_json, :headers => {})
33
33
 
34
34
  response = PRX::Model::User.me
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prx_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kuklewicz
@@ -11,7 +11,7 @@ cert_chain: []
11
11
  date: 2014-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activesupport
14
+ name: json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - '>='
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: excon
28
+ name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '>='
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: oauth2
42
+ name: excon
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - '>='
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: faraday
56
+ name: oauth2
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - '>='
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: faraday_middleware
70
+ name: faraday
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - '>='
@@ -162,6 +162,7 @@ files:
162
162
  - Rakefile
163
163
  - lib/prx/client.rb
164
164
  - lib/prx/client/version.rb
165
+ - lib/prx/middleware/json.rb
165
166
  - lib/prx/model.rb
166
167
  - lib/prx/model/account.rb
167
168
  - lib/prx/model/audio_file.rb