Class: BigcommerceOrderAgent::Client::AbstractClient

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

Direct Known Subclasses

Customer, Order

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store_hash, client_id, access_token) ⇒ AbstractClient

Returns a new instance of AbstractClient.



8
9
10
11
12
13
14
15
16
17
# File 'lib/client/abstract_client.rb', line 8

def initialize(store_hash, client_id, access_token)
    @endpoint = 'https://api.bigcommerce.com'
    @headers = {
        'X-Auth-Client' => client_id,
        'X-Auth-Token' => access_token,
        'Content-Type' => 'application/json',
        'Accept' => 'application/json'
    }
    @store_hash = store_hash
end

Class Method Details

.uri_baseObject



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

def self.uri_base
    @uri_base
end

Instance Method Details

#clientObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/client/abstract_client.rb', line 27

def client
    if !@client
        @client = Faraday.new({ url: @endpoint, headers: @headers }) do |conn|
            conn.use Faraday::Response::RaiseError
            conn.response :logger, nil, { headers: true, bodies: true }
            conn.response :json, :content_type => 'application/json'
            conn.adapter Faraday.default_adapter
        end
    end

    return @client
end

#index(params = {}) ⇒ Object



58
59
60
61
# File 'lib/client/abstract_client.rb', line 58

def index(params = {})
    response = client.get(uri, params)
    return response.body['data']
end

#uri(params = {}, path = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/client/abstract_client.rb', line 40

def uri(params = {}, path = nil)
    params[:api_version] = 'v2' unless params[:api_version].present?
    u = "/stores/#{@store_hash}/#{uri_base}"

    if path
      u = u + "/#{path}"
    end

    params.each do |key,val|
      u = u.gsub(":#{key.to_s}", val.to_s)
    end

    # remove params that weren't provided
    u = u.gsub(/(\/\:[^\/]+)/, '')

    return u
end

#uri_baseObject



23
24
25
# File 'lib/client/abstract_client.rb', line 23

def uri_base
    self.class.uri_base
end