quick_response 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +63 -1
- data/lib/quick_response/base.rb +3 -3
- data/lib/quick_response/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# QuickResponse
|
2
2
|
|
3
|
-
QuickResponse is a **QR Code Generator**. It doesn't rely on any image manipulation library. Instead it's thin wrapper around the **Google Chart API**.
|
3
|
+
QuickResponse is a **QR Code Generator**. It doesn't rely on any image manipulation library. Instead it's a thin wrapper around the **Google Chart API**.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -60,4 +60,66 @@ Available types are:
|
|
60
60
|
* Geo (Takes two arguments `longitude` and `lattitude`)
|
61
61
|
* Maps (Take an address as first argument and return a link to Google Maps)
|
62
62
|
|
63
|
+
## Write Your Own Type
|
63
64
|
|
65
|
+
First you need to know what a type is. Basically a type is a class that inherit from QuickResponse::Base and defines a `to_format` instance method. Simple isn't it?
|
66
|
+
|
67
|
+
### Requirements
|
68
|
+
|
69
|
+
In order to work the `to_format` must accept an unlimited number of arguments and return a string.
|
70
|
+
|
71
|
+
### Real Life Examples
|
72
|
+
|
73
|
+
Let's take a look at the `Call:Class`:
|
74
|
+
|
75
|
+
module QuickResponse
|
76
|
+
class Call < ::QuickResponse::Base
|
77
|
+
format "tel:(.*)", :limit => 1
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
Here the `format` class method will generate dynamically a to_format method with the right behavior for us.
|
82
|
+
|
83
|
+
But what is it doing exactly? It's gonna check if the supplied argument match the format. If yes it will just return it untouched. If not it will replace the "(.\*)" sequence by the argument.
|
84
|
+
As a result of this, these two lines will output the same result
|
85
|
+
|
86
|
+
QuickResponse.call("+1234567890")
|
87
|
+
=> #<QuickResponse::Call @content="tel:+1234567890", @size=256>
|
88
|
+
|
89
|
+
QuickResponse.call("tel:+1234567890")
|
90
|
+
=> #<QuickResponse::Call @content="tel:+1234567890", @size=256>
|
91
|
+
|
92
|
+
This is particularly useful with the `Url:Class` because if you had already supply http or https it won't add the protocol again.
|
93
|
+
|
94
|
+
module QuickResponse
|
95
|
+
class Url < ::QuickResponse::Base
|
96
|
+
format "http[s]?:\/{2}(.*)", :output => "http://(.*)", :join => "/"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
### The `format` Options
|
101
|
+
|
102
|
+
* join(String): The join between the arguments
|
103
|
+
* limit(Integer): The number of arguments the class accepts
|
104
|
+
* output(String): If your first argument is too complex and can't be replaced. (i.e. `Url:Class`)
|
105
|
+
* query(Boolean): If set to true the arguments will be escaped.
|
106
|
+
|
107
|
+
### A `Search` Type
|
108
|
+
|
109
|
+
Let's say we want to create a new type which lets us pass a query to a search engine:
|
110
|
+
|
111
|
+
# WARNING: This class is not included in the gem.
|
112
|
+
module QuickResponse
|
113
|
+
def self.search(query)
|
114
|
+
::QuickResponse::Search.new(query)
|
115
|
+
end
|
116
|
+
|
117
|
+
class Search < ::QuickResponse::Base
|
118
|
+
format "http://www.google.com/search?q=(.*)", :limit => 1, :query => true
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
We can now do:
|
123
|
+
|
124
|
+
QuickResponse.search("Robin Clart")
|
125
|
+
=> #<QuickResponse::Search @content="http://www.google.com/search?q=Robin+Clart", @size=256>
|
data/lib/quick_response/base.rb
CHANGED
@@ -10,12 +10,12 @@ module QuickResponse
|
|
10
10
|
# Extract options
|
11
11
|
options = args.last.is_a?(Hash) ? DEFAULT_OPTIONS.merge(args.pop) : DEFAULT_OPTIONS
|
12
12
|
|
13
|
-
@content = to_format(args)
|
13
|
+
@content = to_format(*args)
|
14
14
|
@size = options[:size]
|
15
15
|
end
|
16
16
|
|
17
17
|
def content=(*args)
|
18
|
-
@content = to_format(args)
|
18
|
+
@content = to_format(*args)
|
19
19
|
end
|
20
20
|
|
21
21
|
def image_url
|
@@ -56,7 +56,7 @@ module QuickResponse
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def self.format(format, options = {})
|
59
|
-
define_method :to_format do
|
59
|
+
define_method :to_format do |*args|
|
60
60
|
args = args.slice(0, options[:limit]) if options[:limit]
|
61
61
|
args = args.map { |a| CGI.escape(a) } if options[:query]
|
62
62
|
output = options[:output] || format
|