Class: Tastyrb::Client
- Inherits:
-
Object
- Object
- Tastyrb::Client
- Defined in:
- lib/tastyrb.rb
Defined Under Namespace
Modules: Connection
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_key_param ⇒ Object
Returns the value of attribute api_key_param.
-
#jsonp_callback ⇒ Object
Returns the value of attribute jsonp_callback.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
-
#word_separator ⇒ Object
Returns the value of attribute word_separator.
Instance Method Summary collapse
- #base_uri ⇒ Object
- #base_uri=(uri) ⇒ Object
- #base_uri_path ⇒ Object
- #get(method, options) ⇒ Object
-
#initialize(base_uri = nil, api_key = nil, jsonp_callback = nil) ⇒ Client
constructor
A new instance of Client.
- #next ⇒ Object
- #previous ⇒ Object
Constructor Details
#initialize(base_uri = nil, api_key = nil, jsonp_callback = nil) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 |
# File 'lib/tastyrb.rb', line 17 def initialize(base_uri=nil, api_key=nil, jsonp_callback=nil) @resources = [] @api_key = api_key @api_key_param = 'api_key' @jsonp_callback = jsonp_callback @word_separator = '_' set_base_uri(base_uri) end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/tastyrb.rb', line 14 def api_key @api_key end |
#api_key_param ⇒ Object
Returns the value of attribute api_key_param.
14 15 16 |
# File 'lib/tastyrb.rb', line 14 def api_key_param @api_key_param end |
#jsonp_callback ⇒ Object
Returns the value of attribute jsonp_callback.
14 15 16 |
# File 'lib/tastyrb.rb', line 14 def jsonp_callback @jsonp_callback end |
#limit ⇒ Object
Returns the value of attribute limit.
14 15 16 |
# File 'lib/tastyrb.rb', line 14 def limit @limit end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
15 16 17 |
# File 'lib/tastyrb.rb', line 15 def @meta end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
15 16 17 |
# File 'lib/tastyrb.rb', line 15 def resources @resources end |
#word_separator ⇒ Object
Returns the value of attribute word_separator.
14 15 16 |
# File 'lib/tastyrb.rb', line 14 def word_separator @word_separator end |
Instance Method Details
#base_uri ⇒ Object
30 31 32 |
# File 'lib/tastyrb.rb', line 30 def base_uri @base_uri end |
#base_uri=(uri) ⇒ Object
26 27 28 |
# File 'lib/tastyrb.rb', line 26 def base_uri=(uri) set_base_uri(uri) end |
#base_uri_path ⇒ Object
34 35 36 |
# File 'lib/tastyrb.rb', line 34 def base_uri_path URI.parse(@base_uri).path end |
#get(method, options) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/tastyrb.rb', line 38 def get(method, ) params = if params[:callback] Connection.format :plain else Connection.format :json end response = Connection.get "#{@base_uri}#{path_for(method)}", :query => params if response.code == 200 # JSONP return response.body if Connection.format == :plain # JSON result = JSON.parse(response.body)['objects'] = JSON.parse(response.body)['meta'] @meta = Response.new() if result.size == 0 raise ResponseCodeError, "404: No document found" elsif result.size == 1 && (.keys.join(' ') =~ /(^|[\s\b_])id(?=\b|__)(?!__(in|i?(contains|(starts|ends)with)))/) != nil # if we asked for an item by id and only one was returned, return just the object, # else return a list always Response.new(result[0]) else result.map {|object| Response.new(object)} end else raise ResponseCodeError, "#{response.code}: #{response.body}" end end |
#next ⇒ Object
72 73 74 |
# File 'lib/tastyrb.rb', line 72 def next (.next) end |
#previous ⇒ Object
76 77 78 |
# File 'lib/tastyrb.rb', line 76 def previous (.previous) end |