nestful 1.0.0.rc2 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,85 @@
1
+ require 'nestful'
2
+ require 'nokogiri'
3
+
4
+ module OEmbed
5
+ def self.parse(html)
6
+ base = Nokogiri::HTML(html)
7
+ link = base.css('link[type="application/json+oembed"]').first
8
+ return unless link
9
+ Response.new(Nestful.get(link['href']).decoded)
10
+ end
11
+
12
+ class Response
13
+ def initialize(attributes = {})
14
+ @attributes = attributes
15
+ end
16
+
17
+ def type
18
+ @attributes['type']
19
+ end
20
+
21
+ def html
22
+ @attributes['html']
23
+ end
24
+
25
+ def title
26
+ @attributes['title']
27
+ end
28
+
29
+ def provider_name
30
+ @attributes['provider_name']
31
+ end
32
+
33
+ def provider_url
34
+ @attributes['provider_url']
35
+ end
36
+
37
+ def width
38
+ @attributes['width']
39
+ end
40
+
41
+ def height
42
+ @attributes['height']
43
+ end
44
+
45
+ def thumbnail_url
46
+ @attributes['thumbnail_url']
47
+ end
48
+
49
+ def thumbnail_width
50
+ @attributes['thumbnail_width']
51
+ end
52
+
53
+ def thumbnail_height
54
+ @attributes['thumbnail_height']
55
+ end
56
+
57
+ def author_name
58
+ @attributes['author_name']
59
+ end
60
+
61
+ def author_url
62
+ @attributes['author_url']
63
+ end
64
+
65
+ def version
66
+ @attributes['version']
67
+ end
68
+
69
+ def video?
70
+ type == 'video'
71
+ end
72
+
73
+ def photo?
74
+ type == 'photo'
75
+ end
76
+
77
+ def link?
78
+ type == 'link'
79
+ end
80
+
81
+ def rich?
82
+ type == 'rich'
83
+ end
84
+ end
85
+ end
@@ -1,4 +1,4 @@
1
- require 'cgi'
1
+ require 'uri'
2
2
 
3
3
  module Nestful
4
4
  module Helpers extend self
@@ -6,24 +6,97 @@ module Nestful
6
6
  params.map(&:to_s).reject(&:empty?) * '/'
7
7
  end
8
8
 
9
- def to_param(value, key = nil)
9
+ def camelize(value)
10
+ value.to_s.split('_').map {|w| w.capitalize }.join
11
+ end
12
+
13
+ # Stolen from Rack:
14
+
15
+ DEFAULT_SEP = /[&;] */n
16
+
17
+ def to_param(value, prefix = nil)
10
18
  case value
11
- when Hash then value.map { |k,v| to_param(v, append_key(key,k)) }.join('&')
12
- when Array then value.map { |v| to_param(v, "#{key}[]") }.join('&')
13
- when nil then ''
19
+ when Array
20
+ value.map { |v|
21
+ to_param(v, "#{prefix}[]")
22
+ }.join("&")
23
+ when Hash
24
+ value.map { |k, v|
25
+ to_param(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
26
+ }.join("&")
27
+ when String
28
+ raise ArgumentError, "value must be a Hash" if prefix.nil?
29
+ "#{prefix}=#{escape(value)}"
14
30
  else
15
- "#{key}=#{CGI.escape(value.to_s)}"
31
+ prefix
16
32
  end
17
33
  end
18
34
 
19
- def camelize(value)
20
- value.to_s.split('_').map {|w| w.capitalize }.join
35
+ def from_param(param)
36
+ Rack::Utils.parse_nested_query(param)
37
+ (value || '').split('&').each do |res|
38
+ key, value = res.split('=')
39
+ @params[key] = value
40
+ end
41
+ end
42
+
43
+ def from_param(qs, d = nil)
44
+ params = {}
45
+
46
+ (qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
47
+ k, v = p.split('=', 2).map { |s| unescape(s) }
48
+
49
+ normalize_params(params, k, v)
50
+ end
51
+
52
+ params
53
+ end
54
+
55
+ def escape(s)
56
+ URI.encode_www_form_component(s)
57
+ end
58
+
59
+ if defined?(::Encoding)
60
+ def unescape(s, encoding = Encoding::UTF_8)
61
+ URI.decode_www_form_component(s, encoding)
62
+ end
63
+ else
64
+ def unescape(s, encoding = nil)
65
+ URI.decode_www_form_component(s, encoding)
66
+ end
21
67
  end
22
68
 
23
69
  protected
24
70
 
25
- def append_key(root_key, key)
26
- root_key.nil? ? key : "#{root_key}[#{key.to_s}]"
71
+ def normalize_params(params, name, v = nil)
72
+ name =~ %r(\A[\[\]]*([^\[\]]+)\]*)
73
+ k = $1 || ''
74
+ after = $' || ''
75
+
76
+ return if k.empty?
77
+
78
+ if after == ""
79
+ params[k] = v
80
+ elsif after == "[]"
81
+ params[k] ||= []
82
+ raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
83
+ params[k] << v
84
+ elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$)
85
+ child_key = $1
86
+ params[k] ||= []
87
+ raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
88
+ if params[k].last.kind_of?(Hash) && !params[k].last.key?(child_key)
89
+ normalize_params(params[k].last, child_key, v)
90
+ else
91
+ params[k] << normalize_params(params.class.new, child_key, v)
92
+ end
93
+ else
94
+ params[k] ||= params.class.new
95
+ raise TypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params[k].kind_of?(Hash)
96
+ params[k] = normalize_params(params[k], after, v)
97
+ end
98
+
99
+ return params
27
100
  end
28
101
  end
29
102
  end
@@ -40,10 +40,10 @@ module Nestful
40
40
  @uri = URI.parse(url)
41
41
  @uri.path = '/' if @uri.path.empty?
42
42
 
43
- @uri.query.split('&').each do |res|
44
- key, value = res.split('=')
45
- @params[key] = value
46
- end if @uri.query
43
+ if @uri.query
44
+ @params.merge!(Helpers.from_param(@uri.query))
45
+ @uri.query = nil
46
+ end
47
47
 
48
48
  @uri
49
49
  end
@@ -1,3 +1,3 @@
1
1
  module Nestful
2
- VERSION = "1.0.0.rc2"
2
+ VERSION = "1.0.1"
3
3
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nestful
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc2
5
- prerelease: 6
4
+ version: 1.0.1
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Alex MacCaw
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-10 00:00:00.000000000Z
12
+ date: 2013-04-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -23,6 +23,7 @@ files:
23
23
  - MIT-LICENSE
24
24
  - README.markdown
25
25
  - Rakefile
26
+ - examples/oembed.rb
26
27
  - examples/resource.rb
27
28
  - lib/nestful.rb
28
29
  - lib/nestful/.DS_Store
@@ -58,12 +59,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
58
59
  required_rubygems_version: !ruby/object:Gem::Requirement
59
60
  none: false
60
61
  requirements:
61
- - - ! '>'
62
+ - - ! '>='
62
63
  - !ruby/object:Gem::Version
63
- version: 1.3.1
64
+ version: '0'
64
65
  requirements: []
65
66
  rubyforge_project:
66
- rubygems_version: 1.8.15
67
+ rubygems_version: 1.8.24
67
68
  signing_key:
68
69
  specification_version: 3
69
70
  summary: Simple Ruby HTTP/REST client with a sane API