cookbook_client 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: be76326629c54e79e3dbc2fe0d253c04094c273a7fb8d114e077f02839bb008e
4
+ data.tar.gz: c695ab16526502ecaa9e719da604c6c3808116247a00292e81b5065f32a857dc
5
+ SHA512:
6
+ metadata.gz: 6e3acae49ecd29f63a798d9c037b53cb4db9ec0eb83d8e862fbe57f818eeeab55fe9ff1cd8c0044844b28f4ce3ca036c6b090d8d6f6848f8361c4063bc9b13c4
7
+ data.tar.gz: c792356c204f397902e7060442d1b5329bfca1e50a83e7032066126d272e9089559817f22bb3a6ca0e891d151f28197df197303c232f5b6ef201421d070ad507
@@ -0,0 +1,43 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'faraday'
4
+
5
+ class CookbookClient
6
+ class << self
7
+ attr_accessor :url, :email, :password
8
+ end
9
+
10
+ def self.http
11
+ if !@email.nil? && !@password.nil?
12
+ return Faraday.new url: @url do |conn|
13
+ conn.basic_auth(@email, @password)
14
+ conn.adapter :net_http
15
+ end
16
+ end
17
+ Faraday.new url: @url
18
+ end
19
+
20
+ def self.form_helpers
21
+ response = JSON.parse(http.get('/api/v1/recipes/form_helper').body)
22
+ {
23
+ cuisines: (
24
+ response['cuisines'].map { |c| [c['id'], c['name']] }
25
+ ).to_h,
26
+ recipe_types: (
27
+ response['recipe_types'].map { |rp| [rp['id'], rp['name']] }
28
+ ).to_h
29
+ }
30
+ end
31
+
32
+ def self.format_recipes(recipes:, content_class:)
33
+ recipes = [recipes] unless recipes.is_a?(Array)
34
+ helpers = form_helpers
35
+ recipes.map do |r|
36
+ r['cuisine'] = helpers[:cuisines][r['cuisine_id']]
37
+ r['recipe_type'] = helpers[:recipe_types][r['recipe_type_id']]
38
+ content_class.new info: r
39
+ end
40
+ end
41
+ end
42
+
43
+ require 'cookbook_client/recipe'
@@ -0,0 +1,57 @@
1
+ class CookbookClient::Recipe
2
+ attr_accessor :title, :cook_time, :cook_method, :ingredients, :difficulty,
3
+ :recipe_type, :cuisine, :id, :created_at, :updated_at,
4
+ :cuisine_id, :recipe_type_id, :featured, :user_id
5
+
6
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
7
+ def initialize(info:)
8
+ @title = info['title']
9
+ @cook_time = info['cook_time']
10
+ @cook_method = info['cook_method']
11
+ @ingredients = info['ingredients']
12
+ @difficulty = info['difficulty']
13
+ @recipe_type = info['recipe_type']
14
+ @cuisine = info['cuisine']
15
+ @id = info['id']
16
+ @created_at = info['created_at']
17
+ @updated_at = info['updated_at']
18
+ @cuisine_id = info['cuisine_id']
19
+ @recipe_type_id = info['recipe_type_id']
20
+ @featured = info['featured']
21
+ @user_id = info['user_id']
22
+ end
23
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
24
+
25
+ def self.all
26
+ CookbookClient.format_recipes(
27
+ recipes: JSON.parse(CookbookClient.http.get('/api/v1/recipes/all').body),
28
+ content_class: self
29
+ )
30
+ end
31
+
32
+ def self.find(id)
33
+ response = CookbookClient.http.get("/api/v1/recipes/#{id}/search")
34
+
35
+ if response.status != 200
36
+ raise StandardError, "Error #{response.status} - #{response.body}"
37
+ end
38
+
39
+ CookbookClient.format_recipes(
40
+ recipes: JSON.parse(response.body),
41
+ content_class: self
42
+ ).first
43
+ end
44
+
45
+ def self.destroy(id)
46
+ response = CookbookClient.http.delete("/api/v1/recipes/#{id}")
47
+
48
+ if response.status != 200
49
+ raise StandardError, "Error #{response.status} - #{response.body}"
50
+ end
51
+
52
+ CookbookClient.format_recipes(
53
+ recipes: JSON.parse(response.body),
54
+ content_class: self
55
+ ).first
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cookbook_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jefferson Barbosa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.15.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.15.4
27
+ description: This is a test Gem which interacts with a rails application (Cookbook)
28
+ through its API endpoints.
29
+ email: jefferson.dab@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/cookbook_client.rb
35
+ - lib/cookbook_client/recipe.rb
36
+ homepage:
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.0.2
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Coobook client
59
+ test_files: []