Class: Tastyrb::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tastyrb.rb

Defined Under Namespace

Modules: Connection

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject

Returns the value of attribute api_key.



14
15
16
# File 'lib/tastyrb.rb', line 14

def api_key
  @api_key
end

#api_key_paramObject

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_callbackObject

Returns the value of attribute jsonp_callback.



14
15
16
# File 'lib/tastyrb.rb', line 14

def jsonp_callback
  @jsonp_callback
end

#limitObject

Returns the value of attribute limit.



14
15
16
# File 'lib/tastyrb.rb', line 14

def limit
  @limit
end

#metaObject (readonly)

Returns the value of attribute meta.



15
16
17
# File 'lib/tastyrb.rb', line 15

def meta
  @meta
end

#resourcesObject (readonly)

Returns the value of attribute resources.



15
16
17
# File 'lib/tastyrb.rb', line 15

def resources
  @resources
end

#word_separatorObject

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_uriObject



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_pathObject



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, options)
  params = prepare_options options
  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']
    meta = JSON.parse(response.body)['meta']
    @meta = Response.new(meta)

    if result.size == 0
      raise ResponseCodeError, "404: No document found"
    elsif result.size == 1 && (options.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

#nextObject



72
73
74
# File 'lib/tastyrb.rb', line 72

def next
  get_from_meta(meta.next)
end

#previousObject



76
77
78
# File 'lib/tastyrb.rb', line 76

def previous
  get_from_meta(meta.previous)
end