Module: Harvest::Behavior::Crud

Instance Method Summary collapse

Instance Method Details

#all(query_options = {}) ⇒ Array<Harvest::BaseModel>

Retrieves all items

Returns:

  • (Array<Harvest::BaseModel>)

    an array of models depending on where you’re calling it from (e.g. [Harvest::Client] from Harvest::Base#clients)



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/harvest/behavior/crud.rb', line 6

def all(query_options = {})
  response = request(:get, credentials, api_model.api_path, query: query_options)
  response_parsed = api_model.to_json(response.parsed_response)

  if response_parsed['total_pages'] > 1
    counter = response_parsed['page']

    while counter <= response_parsed['total_pages'] do
      counter += 1
      query_options = query_options.merge!({ 'page' => counter })

      response_page = request(:get, credentials, api_model.api_path,
        query: query_options)
      page_result = api_model.to_json(response_page.parsed_response)
      response_parsed[api_model.json_root.pluralize.to_s]
        .concat(page_result[api_model.json_root.pluralize.to_s])
    end
  end

  api_model.parse(response_parsed)
end

#create(model) ⇒ Harvest::BaseModel

Creates an item

Parameters:

  • model (Harvest::BaseModel)

    the item you want to create

Returns:

  • (Harvest::BaseModel)

    the created model depending on where you’re calling it from (e.g. Harvest::Client from Harvest::Base#clients)



47
48
49
50
51
52
# File 'lib/harvest/behavior/crud.rb', line 47

def create(model)
  model = api_model.wrap(model)
  response = request(:post, credentials, api_model.api_path, body: model.to_json)
  model = api_model.parse(response.parsed_response).first
  find(model.id)
end

#delete(model) ⇒ Integer #delete(id) ⇒ Integer #delete(id) ⇒ Integer

Deletes an item

Overloads:

  • #delete(model) ⇒ Integer

    Parameters:

    • model (Harvest::BaseModel)

      the item you want to delete

  • #delete(id) ⇒ Integer

    Parameters:

    • id (Integer)

      the id of the item you want to delete

  • #delete(id) ⇒ Integer

    Parameters:

    • id (String)

      the String version of the id of the item you want to delete

Returns:

  • (Integer)

    the id of the item deleted



73
74
75
76
77
# File 'lib/harvest/behavior/crud.rb', line 73

def delete(model)
  model = api_model.wrap(model)
  response = request(:delete, credentials, "#{api_model.api_path}/#{model.id}")
  model.id
end

#find(id) ⇒ Harvest::BaseModel #find(id) ⇒ Harvest::BaseModel #find(model) ⇒ Harvest::BaseModel

Retrieves an item by id

Overloads:

  • #find(id) ⇒ Harvest::BaseModel

    Parameters:

    • the (Integer)

      id of the item you want to retreive

  • #find(id) ⇒ Harvest::BaseModel

    Parameters:

    • id (String)

      the String version of the id

  • #find(model) ⇒ Harvest::BaseModel

    Parameters:

    • id (Harvest::BaseModel)

      you can pass a model and it will return a refreshed version

Returns:

  • (Harvest::BaseModel)

    the model depends on where you’re calling it from (e.g. Harvest::Client from Harvest::Base#clients)



37
38
39
40
41
42
# File 'lib/harvest/behavior/crud.rb', line 37

def find(id, query_options = {})
  raise 'ID is required' unless id

  response = request(:get, credentials, "#{api_model.api_path}/#{id}", query: query_options)
  api_model.parse(response.parsed_response).first
end

#update(model) ⇒ Harvest::BaseModel

Updates an item

Parameters:

  • model (Harvest::BaseModel)

    the model you want to update

Returns:

  • (Harvest::BaseModel)

    the created model depending on where you’re calling it from (e.g. Harvest::Client from Harvest::Base#clients)



57
58
59
60
61
62
# File 'lib/harvest/behavior/crud.rb', line 57

def update(model)
  model = api_model.wrap(model)
  response = request(:put, credentials, "#{api_model.api_path}/#{model.id}", body: model.to_json)
  model = api_model.parse(response.parsed_response).first
  find(model.id)
end