yummly 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -51,8 +51,8 @@ The find command returns a single Yummly::Recipe object:
51
51
  ## The Recipe object
52
52
 
53
53
  The Yummly ruby wrapper returns all results as recipe objects. These objects normalize the API responses to make it
54
- easier for developers to interact with recipes from ruby. All Yummly recipe attributes have been directly mapped, and
55
- in cases where the JSON response returned an array for a specific attribute and array of related objects are returned
54
+ easier for developers to interact with recipes from ruby. All Yummly recipe attributes have been directly mapped.
55
+ In cases where the JSON response returned an array for a specific attribute, an array of appropriate objects are returned
56
56
  from the ruby Recipe object.
57
57
 
58
58
  For example, to access the thumbnail image for a recipe:
@@ -60,6 +60,8 @@ For example, to access the thumbnail image for a recipe:
60
60
  recipe = Yummly.find('French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364')
61
61
  recipe.images.first.small_url
62
62
 
63
+ (Shhhh, there's also a Recipe#thumbnail convenience method!)
64
+
63
65
  Explore the Yummly::Recipe class to see the full range of available attributes.
64
66
 
65
67
  ## Contributing
data/lib/yummly/api.rb CHANGED
@@ -6,30 +6,33 @@
6
6
  # Yummly::Api.find('French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364')
7
7
  # @example
8
8
  # Yummly::Api.search('Onion soup')
9
- class Yummly::Api
10
9
 
11
- # Retrieves a single recipe.
12
- #
13
- # @param [String] id The yummly recipe identifier.
14
- # @return [Yummly::Recipe] a instance of a recipe object
15
- # @example
16
- # recipe = Yummly::Api.find('French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364')
17
- def self.find(id)
18
- recipe_json = Yummly::Connection.get("recipe/#{id}")
19
- Yummly::Recipe.new(recipe_json)
20
- end
10
+ module Yummly
11
+ class Api
21
12
 
22
- # Searches for recipes that match the supplied search terms.
23
- #
24
- # @param [String] terms A string of terms used to search API
25
- # @param [Hash] params Additional options to pass to the search API
26
- # @return [Array] a collection of recipe objects
27
- # @example
28
- # recipes = Yummly::Api.search('Onion soup')
29
- def self.search(terms, params = {})
30
- params[:q] = terms unless params.has_key?(:q)
31
- result = Yummly::Connection.get(:recipes, params)
32
- result["matches"].collect { |recipe_json| Yummly::Recipe.new(recipe_json) }
33
- end
13
+ # Retrieves a single recipe.
14
+ #
15
+ # @param [String] id The yummly recipe identifier.
16
+ # @return [Yummly::Recipe] a instance of a recipe object
17
+ # @example
18
+ # recipe = Yummly::Api.find('French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364')
19
+ def self.find(id)
20
+ recipe_json = Yummly::Connection.get("recipe/#{id}")
21
+ Yummly::Recipe.new(recipe_json)
22
+ end
34
23
 
24
+ # Searches for recipes that match the supplied search terms.
25
+ #
26
+ # @param [String] terms A string of terms used to search API
27
+ # @param [Hash] params Additional options to pass to the search API
28
+ # @return [Array] a collection of recipe objects
29
+ # @example
30
+ # recipes = Yummly::Api.search('Onion soup')
31
+ def self.search(terms, params = {})
32
+ params[:q] = terms unless params.has_key?(:q)
33
+ result = Yummly::Connection.get(:recipes, params)
34
+ result["matches"].collect { |recipe_json| Yummly::Recipe.new(recipe_json) }
35
+ end
36
+
37
+ end
35
38
  end
@@ -13,24 +13,26 @@
13
13
  # config.app_id = "12345"
14
14
  # config.app_key = "XXXXXXXXXXXXXXXXXXXXXXXX"
15
15
  # end
16
- class Yummly::Configuration
16
+ module Yummly
17
+ class Configuration
17
18
 
18
- attr_accessor :app_key,
19
- :app_id,
20
- :use_ssl
19
+ attr_accessor :app_key,
20
+ :app_id,
21
+ :use_ssl
21
22
 
22
- # Creates a configuration object, defaulting use_ssl to false.
23
- def initialize
24
- @use_ssl = false
25
- end
23
+ # Creates a configuration object, defaulting use_ssl to false.
24
+ def initialize
25
+ @use_ssl = false
26
+ end
26
27
 
27
- # Returns true if API calls to Yummly should use SSL.
28
- #
29
- # @return [Boolean] true if API calls to Yummly should use SSL
30
- # @example
31
- # Yummly.configuration.use_ssl?
32
- def use_ssl?
33
- use_ssl
34
- end
28
+ # Returns true if API calls to Yummly should use SSL.
29
+ #
30
+ # @return [Boolean] true if API calls to Yummly should use SSL
31
+ # @example
32
+ # Yummly.configuration.use_ssl?
33
+ def use_ssl?
34
+ use_ssl
35
+ end
35
36
 
37
+ end
36
38
  end
@@ -1,29 +1,51 @@
1
1
  # This class handles the HTTP interactions with the Yummly API calls.
2
- class Yummly::Connection
2
+ module Yummly
3
+ class Connection
3
4
 
4
- attr_accessor :connection
5
+ attr_accessor :connection
5
6
 
6
- def self.get(command, params = {})
7
- params['_app_id'] = Yummly.configuration.app_id
8
- params['_app_key'] = Yummly.configuration.app_key
9
- response = self.api_connection.get("/#{self.api_version}/api/#{command}?#{Rack::Utils.build_query(params)}")
10
- JSON.parse(response.body)
11
- end
7
+ def self.get(command, params = {})
8
+ response = self.api_connection.get(build_uri(command, params))
9
+ self.parse_response(response)
10
+ end
12
11
 
13
- def self.api_connection
14
- Faraday.new(:url => "#{self.protocol}://api.yummly.com") do |faraday|
15
- faraday.request :url_encoded # form-encode POST params
16
- faraday.response :logger # log requests to STDOUT
17
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
12
+ def self.api_connection
13
+ Faraday.new(:url => "#{self.protocol}://api.yummly.com") do |faraday|
14
+ faraday.request :url_encoded # form-encode POST params
15
+ faraday.response :logger # log requests to STDOUT
16
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
17
+ end
18
18
  end
19
- end
20
19
 
21
- def self.protocol
22
- Yummly.configuration.use_ssl? ? 'https' : 'http'
23
- end
20
+ def self.parse_response(response)
21
+ case response.status
22
+ when 409 then
23
+ raise Yummly::PermissionError, response.body
24
+ when 404 then
25
+ nil
26
+ when 200 then
27
+ JSON.parse(response.body)
28
+ end
29
+ end
24
30
 
25
- def self.api_version
26
- Yummly::API_VERSION
27
- end
31
+ def build_uri(command, params)
32
+ query_string = build_params_query_string(params)
33
+ "/#{self.api_version}/api/#{command}?#{query_string}"
34
+ end
28
35
 
36
+ def self.build_params_query_string(params)
37
+ params['_app_id'] = Yummly.configuration.app_id
38
+ params['_app_key'] = Yummly.configuration.app_key
39
+ Rack::Utils.build_query(params)
40
+ end
41
+
42
+ def self.protocol
43
+ Yummly.configuration.use_ssl? ? 'https' : 'http'
44
+ end
45
+
46
+ def self.api_version
47
+ Yummly::API_VERSION
48
+ end
49
+
50
+ end
29
51
  end
data/lib/yummly/flavor.rb CHANGED
@@ -1,20 +1,22 @@
1
1
  # Contains the flavor scores for a recipe, each on a range from 0 to 1.
2
- class Yummly::Flavor
2
+ module Yummly
3
+ class Flavor
3
4
 
4
- attr_accessor :spiciness,
5
- :bitterness,
6
- :sweetness,
7
- :savoriness,
8
- :saltiness,
9
- :sourness
5
+ attr_accessor :spiciness,
6
+ :bitterness,
7
+ :sweetness,
8
+ :savoriness,
9
+ :saltiness,
10
+ :sourness
10
11
 
11
- def initialize(values)
12
- @spiciness = values["Piquant"]
13
- @bitterness = values["Bitter"]
14
- @sweetness = values["Sweet"]
15
- @savoriness = values["Meaty"]
16
- @sourness = values["Sour"]
17
- @saltiness = values["Salty"]
18
- end
12
+ def initialize(values)
13
+ @spiciness = values["Piquant"]
14
+ @bitterness = values["Bitter"]
15
+ @sweetness = values["Sweet"]
16
+ @savoriness = values["Meaty"]
17
+ @sourness = values["Sour"]
18
+ @saltiness = values["Salty"]
19
+ end
19
20
 
21
+ end
20
22
  end
data/lib/yummly/image.rb CHANGED
@@ -1,19 +1,21 @@
1
1
  # The image that should be displayed along with the recipe if images are available. Comes in 3 different sizes.
2
- class Yummly::Image
2
+ module Yummly
3
+ class Image
3
4
 
4
- # small (90×60)
5
- attr_accessor :small_url
5
+ # small (90×60)
6
+ attr_accessor :small_url
6
7
 
7
- # medium (180×120)
8
- attr_accessor :medium_url
8
+ # medium (180×120)
9
+ attr_accessor :medium_url
9
10
 
10
- # large (360×240)
11
- attr_accessor :large_url
11
+ # large (360×240)
12
+ attr_accessor :large_url
12
13
 
13
- def initialize(values)
14
- @large_url = values["hostedLargeUrl"]
15
- @medium_url = values["hostedMediumUrl"]
16
- @small_url = values["hostedSmallUrl"]
17
- end
14
+ def initialize(values)
15
+ @large_url = values["hostedLargeUrl"]
16
+ @medium_url = values["hostedMediumUrl"]
17
+ @small_url = values["hostedSmallUrl"]
18
+ end
18
19
 
20
+ end
19
21
  end
@@ -1,25 +1,27 @@
1
1
  # The nutritional composition of the recipe, in the form of a list of nutrients and their amounts, per serving. Yummly
2
2
  # will return nutrition estimates only for those recipes where we are reasonably confident in their accuracy. These are
3
3
  # only estimates and you should be clear about that in what you tell your users.
4
- class Yummly::NutritionEstimate
4
+ module Yummly
5
+ class NutritionEstimate
5
6
 
6
- # Nutrition attribute’s search parameter name.
7
- attr_accessor :attribute
7
+ # Nutrition attribute’s search parameter name.
8
+ attr_accessor :attribute
8
9
 
9
- # Display name of this nutrition attribute.
10
- attr_accessor :description
10
+ # Display name of this nutrition attribute.
11
+ attr_accessor :description
11
12
 
12
- # Nutrition attribute value for this recipe.
13
- attr_accessor :value
13
+ # Nutrition attribute value for this recipe.
14
+ attr_accessor :value
14
15
 
15
- # Implied unit of measure as a Yummly::Unit object.
16
- attr_accessor :unit
16
+ # Implied unit of measure as a Yummly::Unit object.
17
+ attr_accessor :unit
17
18
 
18
- def initialize(values)
19
- @attribute = values["attribute"]
20
- @description = values["description"]
21
- @value = values["value"]
22
- @unit = Yummly::Unit.new(values["unit"])
23
- end
19
+ def initialize(values)
20
+ @attribute = values["attribute"]
21
+ @description = values["description"]
22
+ @value = values["value"]
23
+ @unit = Yummly::Unit.new(values["unit"])
24
+ end
24
25
 
26
+ end
25
27
  end
data/lib/yummly/recipe.rb CHANGED
@@ -4,104 +4,106 @@
4
4
  # When a response attribute is an array, collections of related objects will be created for convenience. For example,
5
5
  # the "images" attribute on the response maps to the #images method which returns a collection of Yummlly::Image
6
6
  # objects.
7
- class Yummly::Recipe
7
+ module Yummly
8
+ class Recipe
8
9
 
9
- attr_accessor :response
10
+ attr_accessor :response
10
11
 
11
- def initialize(recipe_json)
12
- @response = recipe_json
13
- end
12
+ def initialize(recipe_json)
13
+ @response = recipe_json
14
+ end
14
15
 
15
- # Nutrition attribute’s search parameter name.
16
- def attribute
17
- response["attribute"]
18
- end
16
+ # Nutrition attribute’s search parameter name.
17
+ def attribute
18
+ response["attribute"]
19
+ end
19
20
 
20
- def attributes
21
- response["attributes"]
22
- end
21
+ def attributes
22
+ response["attributes"]
23
+ end
23
24
 
24
- def attribution
25
- response["attribution"]
26
- end
25
+ def attribution
26
+ response["attribution"]
27
+ end
27
28
 
28
- def description
29
- response["description"]
30
- end
29
+ def description
30
+ response["description"]
31
+ end
31
32
 
32
- # @return [Yummly::Flavor] instance of a Yummly::Flavor object
33
- def flavor
34
- @flavor ||= Yummly::Flavor.new(response["flavors"])
35
- end
33
+ # @return [Yummly::Flavor] instance of a Yummly::Flavor object
34
+ def flavor
35
+ @flavor ||= Yummly::Flavor.new(response["flavors"])
36
+ end
36
37
 
37
- # @return [String] the Yummly id for this recipe.
38
- def id
39
- response["id"]
40
- end
38
+ # @return [String] the Yummly id for this recipe.
39
+ def id
40
+ response["id"]
41
+ end
41
42
 
42
- # @return [Array] collection of Yummly::Image objects.
43
- def images
44
- @images ||= images_node.collect { |image| Yummly::Image.new(image) }
45
- end
43
+ # @return [Array] collection of Yummly::Image objects.
44
+ def images
45
+ @images ||= images_node.collect { |image| Yummly::Image.new(image) }
46
+ end
46
47
 
47
- # @return [Array] collection of strings of ingredients.
48
- def ingredients
49
- response["ingredients"] || ingredient_lines
50
- end
48
+ # @return [Array] collection of strings of ingredients.
49
+ def ingredients
50
+ response["ingredients"] || ingredient_lines
51
+ end
51
52
 
52
- # @return [Array] collection of strings of ingredients.
53
- def ingredient_lines
54
- response["ingredientLines"]
55
- end
53
+ # @return [Array] collection of strings of ingredients.
54
+ def ingredient_lines
55
+ response["ingredientLines"]
56
+ end
56
57
 
57
- # Returns the name of the recipe, automatically finding it using the correct response node.
58
- # @return [String] name of the recipe
59
- def name
60
- response["name"] || recipe_name
61
- end
58
+ # Returns the name of the recipe, automatically finding it using the correct response node.
59
+ # @return [String] name of the recipe
60
+ def name
61
+ response["name"] || recipe_name
62
+ end
62
63
 
63
- # @return [Integer] number of servings this recipe provides.
64
- def number_of_servings
65
- response["numberOfServings"]
66
- end
64
+ # @return [Integer] number of servings this recipe provides.
65
+ def number_of_servings
66
+ response["numberOfServings"]
67
+ end
67
68
 
68
- # The nutritional composition of the recipe, in the form of a list of nutrients and their amounts, per serving. We
69
- # will return nutrition estimates only for those recipes where we are reasonably confident in their accuracy. These
70
- # are only estimates and you should be clear about that in what you tell your users.
71
- # @return [Array] collection of Yummly::NutritionEstimate objects.
72
- def nutrition_estimates
73
- @nutrition_estimates ||= response["nutritionEstimates"].collect { |ne| Yummly::NutritionEstimate.new(ne) }
74
- end
69
+ # The nutritional composition of the recipe, in the form of a list of nutrients and their amounts, per serving. We
70
+ # will return nutrition estimates only for those recipes where we are reasonably confident in their accuracy. These
71
+ # are only estimates and you should be clear about that in what you tell your users.
72
+ # @return [Array] collection of Yummly::NutritionEstimate objects.
73
+ def nutrition_estimates
74
+ @nutrition_estimates ||= response["nutritionEstimates"].collect { |ne| Yummly::NutritionEstimate.new(ne) }
75
+ end
75
76
 
76
- def rating
77
- response["rating"]
78
- end
77
+ def rating
78
+ response["rating"]
79
+ end
79
80
 
80
- def recipe_name
81
- response["recipeName"]
82
- end
81
+ def recipe_name
82
+ response["recipeName"]
83
+ end
83
84
 
84
- def total_time
85
- response["totalTime"]
86
- end
85
+ def total_time
86
+ response["totalTime"]
87
+ end
87
88
 
88
- def total_time_in_seconds
89
- response["totalTimeInSeconds"]
90
- end
89
+ def total_time_in_seconds
90
+ response["totalTimeInSeconds"]
91
+ end
91
92
 
92
- def thumbnail
93
- if images
94
- images.first.small_url
93
+ def thumbnail
94
+ if images
95
+ images.first.small_url
96
+ end
95
97
  end
96
- end
97
98
 
98
- private
99
+ private
99
100
 
100
- # The search and find API calls populate different image attribute nodes in their respective responses. This method
101
- # determines which one has been populated and returns a populated node for the images collection method to use. This
102
- # allows the developer using this API to only use one method to access images.
103
- def images_node
104
- response["images"] || response["smallImageUrls"].collect { |url| { "hostedSmallUrl" => url } }
105
- end
101
+ # The search and find API calls populate different image attribute nodes in their respective responses. This method
102
+ # determines which one has been populated and returns a populated node for the images collection method to use. This
103
+ # allows the developer using this API to only use one method to access images.
104
+ def images_node
105
+ response["images"] || response["smallImageUrls"].collect { |url| {"hostedSmallUrl" => url} }
106
+ end
106
107
 
108
+ end
107
109
  end
data/lib/yummly/source.rb CHANGED
@@ -1,19 +1,21 @@
1
1
  # Contains details about the origin of the recipe.
2
- class Yummly::Source
2
+ module Yummly
3
+ class Source
3
4
 
4
- # The original source url of this recipe.
5
- attr_accessor :recipe_url
5
+ # The original source url of this recipe.
6
+ attr_accessor :recipe_url
6
7
 
7
- # The url of the source site of this recipe.
8
- attr_accessor :site_url
8
+ # The url of the source site of this recipe.
9
+ attr_accessor :site_url
9
10
 
10
- # The display name of the source site.
11
- attr_accessor :display_name
11
+ # The display name of the source site.
12
+ attr_accessor :display_name
12
13
 
13
- def initialize(values)
14
- @recipe_url = values["sourceRecipeUrl"]
15
- @site_url = values["sourceSiteUrl"]
16
- @display_name= values["sourceDisplayName"]
17
- end
14
+ def initialize(values)
15
+ @recipe_url = values["sourceRecipeUrl"]
16
+ @site_url = values["sourceSiteUrl"]
17
+ @display_name= values["sourceDisplayName"]
18
+ end
18
19
 
20
+ end
19
21
  end
data/lib/yummly/unit.rb CHANGED
@@ -1,23 +1,25 @@
1
1
  # The implied unit of measure for a nutrition estimate.
2
- class Yummly::Unit
2
+ module Yummly
3
+ class Unit
3
4
 
4
- # Single unit display name for unit of measure.
5
- attr_accessor :name
5
+ # Single unit display name for unit of measure.
6
+ attr_accessor :name
6
7
 
7
- # Single unit abbreviation for unit of measure.
8
- attr_accessor :abbreviation
8
+ # Single unit abbreviation for unit of measure.
9
+ attr_accessor :abbreviation
9
10
 
10
- # Plural display name for unit of measure.
11
- attr_accessor :plural
11
+ # Plural display name for unit of measure.
12
+ attr_accessor :plural
12
13
 
13
- # Plural unit abbreviation for unit of measure.
14
- attr_accessor :plural_abbreviation
14
+ # Plural unit abbreviation for unit of measure.
15
+ attr_accessor :plural_abbreviation
15
16
 
16
- def initialize(values)
17
- @name = values["name"]
18
- @abbreviation = values["abbreviation"]
19
- @plural = values["plural"]
20
- @plural_abbreviation = values["pluralAbbreviation"]
21
- end
17
+ def initialize(values)
18
+ @name = values["name"]
19
+ @abbreviation = values["abbreviation"]
20
+ @plural = values["plural"]
21
+ @plural_abbreviation = values["pluralAbbreviation"]
22
+ end
22
23
 
24
+ end
23
25
  end
@@ -1,3 +1,3 @@
1
1
  module Yummly
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/yummly.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require "yummly/api"
2
- require "yummly/connection"
3
2
  require "yummly/configuration"
3
+ require "yummly/connection"
4
4
  require "yummly/flavor"
5
5
  require "yummly/image"
6
6
  require "yummly/nutrition_estimate"
@@ -31,4 +31,7 @@ module Yummly
31
31
  Yummly::Api.find(recipe_id)
32
32
  end
33
33
  end
34
+
35
+ class Error < StandardError; end
36
+ class PermissionError < Error; end
34
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yummly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-19 00:00:00.000000000 Z
12
+ date: 2013-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  requirements: []
93
93
  rubyforge_project:
94
- rubygems_version: 1.8.25
94
+ rubygems_version: 1.8.24
95
95
  signing_key:
96
96
  specification_version: 3
97
97
  summary: Ruby wrapper to the Yummly API