gmc 0.1.0

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: 464eb8b61fb26f7aa3406420982b8d0de03a255b33dc09a9dd45064d9c63975c
4
+ data.tar.gz: 591f5c94d155770d8e3bdabc3c04657136affd6de8d9d18ffbffd29f521e068e
5
+ SHA512:
6
+ metadata.gz: 4790b9416355260d75ee1d4428a46d3be156b34f02a906973ecb2101b19d3a062d3c34f87d4f7e5e01b111aba4acb6fba14103066c14ec96885f79e9b02b3b40
7
+ data.tar.gz: c17250aa389968bf9f14a75ccf953a4a1f96ba60cbc42bfdb40fbff4897b46708f56b225e4111dc4c4a28af78d5ffe30bdbd96e5780f50247644e1ebe92445aa
data/lib/gmc/client.rb ADDED
@@ -0,0 +1,87 @@
1
+ module Gmc
2
+ class Client
3
+ def initialize(config_arg = {})
4
+ @config = Gmc.config.reverse_duplicate_merge(config_arg)
5
+ end
6
+
7
+ def create(**args)
8
+ body = {}
9
+ body[:offerId] = args[:id]
10
+ body[:title] = args[:title]
11
+ body[:description] = args[:description]
12
+ body[:link] = args[:link]
13
+ body[:imageLink] = args[:image_link]
14
+ body[:contentLanguage] = args[:content_language] || @config.language
15
+ body[:targetCountry] = args[:target_country] || @config.country
16
+ body[:brand] = args[:brand]
17
+ body[:feedLabel] = args[:feed_label] || @config.country
18
+ body[:channel] = args[:channel] || 'online'
19
+ body[:availability] = args[:availability] || 'in stock'
20
+ body[:condition] = args[:condition] || 'new'
21
+ body[:price] = {}
22
+ body[:price][:currency] = args[:currency] || @config.currency
23
+ body[:price][:value] = args[:price]
24
+
25
+ call('/products', body, 'POST')
26
+ end
27
+
28
+ def update(**args)
29
+ body = {}
30
+ body[:title] = args[:title]
31
+ body[:description] = args[:description]
32
+ body[:link] = args[:link]
33
+ body[:imageLink] = args[:image_link]
34
+ body[:availability] = args[:availability] || 'in stock'
35
+ body[:condition] = args[:condition] || 'new'
36
+ body[:price] = {}
37
+ body[:price][:currency] = args[:currency] || @config.currency
38
+ body[:price][:value] = args[:price]
39
+
40
+ call("/products/#{args[:id]}", body, 'PATCH')
41
+ end
42
+
43
+ def destroy(id)
44
+ call("/products/#{id}", 'DELETE')
45
+ end
46
+
47
+ def product(id)
48
+ call("/products/#{id}", nil)
49
+ end
50
+
51
+ def products
52
+ call("/products", nil)
53
+ end
54
+
55
+ private
56
+
57
+ def call(endpoint, body, method_name = 'GET')
58
+ url = URI("#{@config.base_url}/#{@config.merchant_id}#{endpoint}?key=#{@config.api_key}")
59
+ https = Net::HTTP.new(url.host, url.port)
60
+ https.use_ssl = true
61
+
62
+ case method_name
63
+ when 'POST'
64
+ request = Net::HTTP::Post.new(url)
65
+ request.body = JSON.dump(body)
66
+ when 'PATCH'
67
+ request = Net::HTTP::Patch.new(url)
68
+ request.body = JSON.dump(body)
69
+ when 'DELETE'
70
+ request = Net::HTTP::Delete.new(url)
71
+ else
72
+ request = Net::HTTP::Get.new(url)
73
+ end
74
+
75
+ request['Authorization'] = "Bearer #{@config.access_token}"
76
+ request['Accept'] = 'application/json'
77
+ request['Content-Type'] = 'application/json'
78
+ response_formate(https.request(request))
79
+ end
80
+
81
+ def response_formate(res)
82
+ OpenStruct.new({ code: res.code, data: JSON.parse(res.read_body) })
83
+ rescue StandardError => e
84
+ OpenStruct.new({ code: res.code, data: res.read_body, exception_message: e.to_s })
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,32 @@
1
+ module Gmc
2
+ class Configuration
3
+ attr_accessor :merchant_id
4
+ attr_accessor :currency
5
+ attr_accessor :access_token
6
+ attr_accessor :api_key
7
+ attr_accessor :language
8
+ attr_accessor :country
9
+ attr_accessor :base_url
10
+
11
+ def self.setup
12
+ new.tap do |instance|
13
+ yield(instance) if block_given?
14
+ end
15
+ end
16
+
17
+ def reverse_duplicate_merge(hash)
18
+ dup.tap do |instance|
19
+ hash.each do |option, value|
20
+ instance.public_send("#{option}=", value)
21
+ end
22
+ end
23
+ end
24
+
25
+ def initialize
26
+ @base_url = "https://shoppingcontent.googleapis.com/content/v2.1"
27
+ @country = 'AU'
28
+ @currency = 'AUD'
29
+ @language = 'en'
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Gmc
2
+ VERSION = "0.1.0"
3
+ end
data/lib/gmc.rb ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "rbconfig"
6
+ require "securerandom"
7
+ require "set"
8
+ require "socket"
9
+ require "uri"
10
+ require "forwardable"
11
+ require_relative "gmc/version"
12
+ require "gmc/client"
13
+ require "gmc/configuration"
14
+
15
+ module Gmc
16
+ class Error < StandardError; end
17
+ @config = Gmc::Configuration.setup
18
+
19
+ class << self
20
+ extend Forwardable
21
+
22
+ attr_reader :config
23
+
24
+ # User configurable options
25
+ def_delegators :@config, :merchant_id, :merchant_id=
26
+ def_delegators :@config, :currency, :currency=
27
+ def_delegators :@config, :language, :language=
28
+ def_delegators :@config, :country, :country=
29
+ def_delegators :@config, :access_token, :access_token=
30
+ def_delegators :@config, :api_key, :api_key=
31
+ def_delegators :@config, :base_url, :base_url=
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gmc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Varun Kothari
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: APIs to upload product details on the merchant center.
14
+ email:
15
+ - varunkothari007@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/gmc.rb
21
+ - lib/gmc/client.rb
22
+ - lib/gmc/configuration.rb
23
+ - lib/gmc/version.rb
24
+ homepage: https://throughouttechnologies.com/contact_us
25
+ licenses:
26
+ - MIT
27
+ metadata:
28
+ homepage_uri: https://throughouttechnologies.com/contact_us
29
+ source_code_uri: https://github.com/vik007/gmc
30
+ changelog_uri: https://throughouttechnologies.com/contact_us
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '2.5'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.2.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: GMC is Google merchant API for Product
50
+ test_files: []