Class: Flavordb::Client

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = ENV['FLAVORDB_API_KEY'], secret = ENV['FLAVORDB_API_SECRET'], opts = {}) ⇒ Client



23
24
25
26
27
28
# File 'lib/flavordb/client.rb', line 23

def initialize(key = ENV['FLAVORDB_API_KEY'], secret = ENV['FLAVORDB_API_SECRET'], opts = {})
  @api_endpoint = opts[:api_endpoint] || default_api_endpoint
  @api_key = key
  @api_secret = secret
  self.class.default_client = self #if self.class.default_client.nil?
end

Class Attribute Details

.default_clientObject

Returns the value of attribute default_client.



16
17
18
# File 'lib/flavordb/client.rb', line 16

def default_client
  @default_client
end

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint.



13
14
15
# File 'lib/flavordb/client.rb', line 13

def api_endpoint
  @api_endpoint
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



13
14
15
# File 'lib/flavordb/client.rb', line 13

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



13
14
15
# File 'lib/flavordb/client.rb', line 13

def api_secret
  @api_secret
end

Class Method Details

.initializeObject



17
18
19
# File 'lib/flavordb/client.rb', line 17

def initialize
  @default_client = nil
end

Instance Method Details

#api_tokenObject



76
77
78
79
80
81
# File 'lib/flavordb/client.rb', line 76

def api_token
  if @api_token.nil?
    @api_token = get_oauth_token
  end
  @api_token
end

#find_businesses(params) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/flavordb/client.rb', line 38

def find_businesses(params)
  raw_businesses = get_object_data_by_path "/businesses", params
  if !raw_businesses['data'].nil?
    raw_businesses['data'].map {|b| Business.get_or_create(b)}
  else
    nil
  end
end

#find_products(params) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/flavordb/client.rb', line 47

def find_products(params)
  raw_products= get_object_data_by_path "/products", params
  if !raw_products['data'].nil?
    raw_products['data'].map {|b| Product.get_or_create(b)}
  else
    nil
  end
end

#get_business(id) ⇒ Object



56
57
58
59
# File 'lib/flavordb/client.rb', line 56

def get_business(id)
  raw_business = get_object_data_by_path("/businesses/#{id}")
  !raw_business['data'].nil? ? Business.get_or_create(raw_business['data']) : nil
end

#get_object_data_by_path(path, api_params = {}) ⇒ Object



71
72
73
74
# File 'lib/flavordb/client.rb', line 71

def get_object_data_by_path (path, api_params={})
  url = (path =~ /http:\/{2}/).nil? ? "#{api_endpoint}#{path}" : path
  authenticated_json_request :get, url, api_params
end

#get_product(id) ⇒ Object



66
67
68
69
# File 'lib/flavordb/client.rb', line 66

def get_product(id)
  raw_product = get_object_data_by_path("/products/#{id}")
  !raw_product['data'].nil? ? Product.get_or_create(raw_product['data']) : nil
end

#get_product_category(id) ⇒ Object



61
62
63
64
# File 'lib/flavordb/client.rb', line 61

def get_product_category(id)
  raw_category = get_object_data_by_path("/product_categories/#{id}")
  !raw_category['data'].nil? ? ProductCategory.get_or_create(raw_category['data']) : nil
end

#valid?Boolean



30
31
32
33
34
35
36
# File 'lib/flavordb/client.rb', line 30

def valid?
  if api_key.nil? || api_secret.nil?
    return false
  else
    !get_oauth_token.nil?
  end
end