Class: EbayBrowseApi::EbayBrowseApiClient

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

Overview

Your code goes hereā€¦

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ru_name: ENV["RU_NAME"], client_id: ENV["CLIENT_ID"], secret_id: ENV["SECRET_ID"], ebay_marketplace_id: "EBAY_US") ⇒ EbayBrowseApiClient

Returns a new instance of EbayBrowseApiClient.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ebay_browse_api.rb', line 44

def initialize(
  ru_name: ENV["RU_NAME"],
  client_id: ENV["CLIENT_ID"],
  secret_id: ENV["SECRET_ID"],
  ebay_marketplace_id: "EBAY_US"
)
  @init_time = nil
  @expires_in = nil
  @expire_date = nil
  @ru_name = ru_name
  @client_id = client_id
  @secret_id = secret_id
  @ebay_marketplace_id = ebay_marketplace_id
  @enc = Base64.encode64("#{@client_id}:#{@secret_id}").gsub(/\n/,'')
  @search_filter_params = {}
  @access_token = nil
  @get_access_token_resp = nil
  @get_access_token_json = nil
  @search_resp = nil
  @search_json = nil
  check_ebay_credentials
end

Class Attribute Details

.get_access_token_urlObject (readonly)

Returns the value of attribute get_access_token_url.



19
20
21
# File 'lib/ebay_browse_api.rb', line 19

def get_access_token_url
  @get_access_token_url
end

.search_urlObject (readonly)

Returns the value of attribute search_url.



19
20
21
# File 'lib/ebay_browse_api.rb', line 19

def search_url
  @search_url
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#client_idObject (readonly)

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

#ebay_marketplace_idObject (readonly)

Returns the value of attribute ebay_marketplace_id.



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

def ebay_marketplace_id
  @ebay_marketplace_id
end

#encObject (readonly)

Returns the value of attribute enc.



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

def enc
  @enc
end

#expire_dateObject (readonly)

Returns the value of attribute expire_date.



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

def expire_date
  @expire_date
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



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

def expires_in
  @expires_in
end

#get_access_token_bodyObject (readonly)

Returns the value of attribute get_access_token_body.



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

def get_access_token_body
  @get_access_token_body
end

#get_access_token_headersObject (readonly)

Returns the value of attribute get_access_token_headers.



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

def get_access_token_headers
  @get_access_token_headers
end

#get_access_token_jsonObject (readonly)

Returns the value of attribute get_access_token_json.



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

def get_access_token_json
  @get_access_token_json
end

#get_access_token_optionsObject (readonly)

Returns the value of attribute get_access_token_options.



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

def get_access_token_options
  @get_access_token_options
end

#get_access_token_respObject (readonly)

Returns the value of attribute get_access_token_resp.



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

def get_access_token_resp
  @get_access_token_resp
end

#init_timeObject (readonly)

Returns the value of attribute init_time.



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

def init_time
  @init_time
end

#ru_nameObject (readonly)

Returns the value of attribute ru_name.



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

def ru_name
  @ru_name
end

#search_filter_paramsObject (readonly)

Returns the value of attribute search_filter_params.



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

def search_filter_params
  @search_filter_params
end

#search_headersObject (readonly)

Returns the value of attribute search_headers.



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

def search_headers
  @search_headers
end

#search_jsonObject (readonly)

Returns the value of attribute search_json.



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

def search_json
  @search_json
end

#search_optionsObject (readonly)

Returns the value of attribute search_options.



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

def search_options
  @search_options
end

#search_respObject (readonly)

Returns the value of attribute search_resp.



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

def search_resp
  @search_resp
end

#secret_idObject (readonly)

Returns the value of attribute secret_id.



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

def secret_id
  @secret_id
end

Class Method Details

.date_time(year: nil, month: nil, day: nil, diff_year: 0, diff_month: 0, diff_day: 0) ⇒ Object



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

def date_time(year: nil, month: nil, day: nil, diff_year: 0, diff_month: 0, diff_day: 0)
  now = DateTime.now
  DateTime.new((year || now.year) - diff_year,
               (month || now.month) - diff_month,
               (day || now.day) - diff_day).
    strftime("%Y-%m-%dT00:00:00Z")
end

.parse_json(json, **kwargs) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/ebay_browse_api.rb', line 21

def parse_json(json, **kwargs)
  begin
    ( json.is_a?(String) && !json.empty? ) ? hash = Oj.load(json, **kwargs) : hash = {}
  rescue Oj::ParseError
    hash = {}
  end
  hash
end

Instance Method Details

#add_search_param(param_name, param_value) ⇒ Object



107
108
109
110
# File 'lib/ebay_browse_api.rb', line 107

def add_search_param(param_name, param_value)
  @search_filter_params[param_name] ||= String.new
  @search_filter_params[param_name] << "#{param_value}"
end

#add_search_param_filter(param_name, param_value) ⇒ Object



112
113
114
115
116
# File 'lib/ebay_browse_api.rb', line 112

def add_search_param_filter(param_name, param_value)
  @search_filter_params[:filter] ||= String.new
  @search_filter_params[:filter] << "&" unless @search_filter_params[:filter].empty?
  @search_filter_params[:filter] << "#{param_name}:#{param_value}"
end

#clear_search_paramsObject



67
68
69
# File 'lib/ebay_browse_api.rb', line 67

def clear_search_params
  @search_filter_params.clear
end

#get_access_tokenObject



87
88
89
90
91
92
93
94
95
# File 'lib/ebay_browse_api.rb', line 87

def get_access_token
  prepare_get_access_token
  @init_time = DateTime.now
  @get_access_token_resp = Typhoeus::Request.post(EbayBrowseApiClient.get_access_token_url, @get_access_token_options)
  @get_access_token_json = EbayBrowseApiClient.parse_json(@get_access_token_resp.response_body)
  @expires_in = @get_access_token_json.fetch('expires_in',nil).to_i
  @expire_date = @init_time + @expires_in
  @access_token = @get_access_token_json.fetch('access_token',nil)
end

#prepare_get_access_tokenObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ebay_browse_api.rb', line 71

def prepare_get_access_token
  @get_access_token_headers = {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Accept' => 'application/json',
    'Accept-Charset' => 'utf-8',
    'Authorization' => "Basic #{@enc}",
    'X-EBAY-C-MARKETPLACE-ID' => @ebay_marketplace_id,
  }
  @get_access_token_body = "grant_type=client_credentials&redirect_uri=#{@ru_name}&scope=https://api.ebay.com/oauth/api_scope"
  @get_access_token_options = {
    headers: @get_access_token_headers,
    followlocation: true,
    body: @get_access_token_body
  }
end

#prepare_searchObject



97
98
99
100
101
102
103
104
105
# File 'lib/ebay_browse_api.rb', line 97

def prepare_search
  @search_headers = {
    'Accept' => 'application/json',
    'Accept-Charset' => 'utf-8',
    'Authorization' => "Bearer #{@access_token}",
    'X-EBAY-C-MARKETPLACE-ID' => @ebay_marketplace_id,
    'Content-Type' => 'application/json'
  }
end

#run_search(params = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ebay_browse_api.rb', line 118

def run_search(params = nil)
  prepare_search
  @search_filter_params = params unless params.nil?
  @search_options = {
    headers: @search_headers,
    followlocation: true,
    params: @search_filter_params
  }
  @search_resp = Typhoeus::Request.get(EbayBrowseApiClient.search_url, @search_options)
  @search_json = EbayBrowseApiClient.parse_json(@search_resp.response_body)
end