Class: BigcommerceAPI::Base

Inherits:
Object
  • Object
show all
Extended by:
BigcommerceAPI
Includes:
HTTParty
Defined in:
lib/bigcommerce_api/base.rb

Direct Known Subclasses

Resource, Result, Store

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bigcommerce_api/base.rb', line 16

def initialize(params={})
  # for the time being, accept old school API params
  session_options = self.class.default_options
  if params[:username] and params[:api_key]
      session_options[:username] = params[:username]
      session_options[:password] = params[:api_key]
    # default to Oauth
  else
    session_options[:headers]['X-Auth-Client'] = params[:client_id]
    session_options[:headers]['X-Auth-Token']  = params[:access_token]
  end

  # if we're using Oauth, we're probably grabbing :store_hash
  # accept :store_url for legacy purposes
  session_options[:base_uri] = if params[:store_url]
                                 "#{params[:store_url]}/api/v2"
                               else
                                 "https://api.bigcommerce.com/stores/#{params[:store_hash]}/v2"
                               end
  session_options[:base_uri] = HTTParty.normalize_base_uri(session_options[:base_uri])

  Thread.current["BigcommerceAPI"] = session_options
end

Class Method Details

.clean!(hash) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/bigcommerce_api/base.rb', line 86

def clean!(hash)
  hash.each do |k, v|
    if v.is_a? Hash
      clean!(v)
    else
      hash.delete(k) if v.nil? or v == ''
    end
  end
end

.date_adjust(params) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bigcommerce_api/base.rb', line 102

def date_adjust(params)
  [:date_created, :date_modified, :date_last_imported, :date_shipped, :min_date_created, :max_date_created, :min_date_modified, :max_date_modified, :min_date_last_imported, :max_date_last_imported].each do |date|
    [date, date.to_s].each do |d|
      if params[d] and !params[d].nil? and params[d] != ''
        if params[d].is_a?(String)
          params[d] = DateTime.parse(params[d])
        end
        # params[d] = CGI::escape(to_rfc2822(params[d]))
        params[d] = to_rfc2822(params[d])
      end
    end
  end
  return params
end

.default_optionsObject



8
9
10
11
12
13
14
# File 'lib/bigcommerce_api/base.rb', line 8

def self.default_options
  Thread.current["BigcommerceAPI"] ||= {
    parser: HTTParty::Parser,
    format: :json,
    headers: { "Accept" => "application/json", "Content-Type" => "application/json" }
  }
end

.to_rfc2822(datetime) ⇒ Object

Returns the date formatted as RFC 2822 string



98
99
100
# File 'lib/bigcommerce_api/base.rb', line 98

def to_rfc2822(datetime)
  datetime.strftime("%a, %d %b %Y %H:%M:%S %z")
end

Instance Method Details

#attributes(strip_empty = false) ⇒ Object

this grabs all of the FIRST LEVEL attributes it ignores hashed and constructed nested attributes, since Big Commerce won’t let us set those anyway



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bigcommerce_api/base.rb', line 68

def attributes(strip_empty=false)
  hash = {}
  self.instance_variables.each {|var| hash[var.to_s.delete("@")] = self.instance_variable_get(var) if (var.to_s['_hash'].nil? and var.to_s['_resource'].nil? and var.to_s[self.resource + '_type'].nil?) }
  # Strip out our extra attributes
  hash.delete('errors')
  hash.delete('attributes_were')

  # Clean up the date
  hash = BigcommerceAPI::Resource.date_adjust(hash)

  # Strip empty fields
  BigcommerceAPI::Resource.clean!(hash) if strip_empty
  hash.delete('id') if strip_empty

  return hash
end

#storeObject Also known as: info, information, settings



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bigcommerce_api/base.rb', line 49

def store
  begin
    response = self.class.get('/store')
    if response
      return Store.new(response.parsed_response)
    else
      return nil
    end
  rescue SocketError => each
    false
  end
end

#timeObject



40
41
42
43
44
45
46
47
# File 'lib/bigcommerce_api/base.rb', line 40

def time
  begin
    response = self.class.get('/time')
    response.parsed_response['time']
  rescue SocketError => each
    false
  end
end